summaryrefslogtreecommitdiffstats
path: root/cpu/mpc8xx/video.c
blob: ef91165602df3fcad7a1902eef8a7edb74d55484 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
/*
 * (C) Copyright 2000
 * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
 * (C) Copyright 2002
 * Wolfgang Denk, wd@denx.de
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

/* #define DEBUG */

/************************************************************************/
/* ** HEADER FILES							*/
/************************************************************************/

#include <stdarg.h>
#include <common.h>
#include <config.h>
#include <version.h>
#include <i2c.h>
#include <linux/types.h>
#include <devices.h>

#ifdef CONFIG_VIDEO

DECLARE_GLOBAL_DATA_PTR;

/************************************************************************/
/* ** DEBUG SETTINGS							*/
/************************************************************************/

#if 0
#define VIDEO_DEBUG_COLORBARS	/* Force colorbars output */
#endif

/************************************************************************/
/* ** VIDEO MODE SETTINGS						*/
/************************************************************************/

#if 0
#define VIDEO_MODE_EXTENDED		/* Allow screen size bigger than visible area */
#define VIDEO_MODE_NTSC
#endif

#define VIDEO_MODE_PAL

#if 0
#define VIDEO_BLINK			/* This enables cursor blinking (under construction) */
#endif

#define VIDEO_INFO			/* Show U-Boot information */
#define VIDEO_INFO_X		VIDEO_LOGO_WIDTH+8
#define VIDEO_INFO_Y		16

/************************************************************************/
/* ** VIDEO ENCODER CONSTANTS						*/
/************************************************************************/

#ifdef CONFIG_VIDEO_ENCODER_AD7176

#include <video_ad7176.h>	/* Sets encoder data, mode, and visible and active area */

#define VIDEO_I2C		1
#define VIDEO_I2C_ADDR		CONFIG_VIDEO_ENCODER_AD7176_ADDR
#endif

#ifdef CONFIG_VIDEO_ENCODER_AD7177

#include <video_ad7177.h>	/* Sets encoder data, mode, and visible and active area */

#define VIDEO_I2C		1
#define VIDEO_I2C_ADDR		CONFIG_VIDEO_ENCODER_AD7177_ADDR
#endif

#ifdef CONFIG_VIDEO_ENCODER_AD7179

#include <video_ad7179.h>	/* Sets encoder data, mode, and visible and active area */

#define VIDEO_I2C		1
#define VIDEO_I2C_ADDR		CONFIG_VIDEO_ENCODER_AD7179_ADDR
#endif

/************************************************************************/
/* ** VIDEO MODE CONSTANTS						*/
/************************************************************************/

#ifdef VIDEO_MODE_EXTENDED
#define VIDEO_COLS	VIDEO_ACTIVE_COLS
#define VIDEO_ROWS	VIDEO_ACTIVE_ROWS
#else
#define VIDEO_COLS	VIDEO_VISIBLE_COLS
#define VIDEO_ROWS	VIDEO_VISIBLE_ROWS
#endif

#define VIDEO_PIXEL_SIZE	(VIDEO_MODE_BPP/8)
#define VIDEO_SIZE		(VIDEO_ROWS*VIDEO_COLS*VIDEO_PIXEL_SIZE)	/* Total size of buffer */
#define VIDEO_PIX_BLOCKS	(VIDEO_SIZE >> 2)	/* Number of ints */
#define VIDEO_LINE_LEN		(VIDEO_COLS*VIDEO_PIXEL_SIZE)	/* Number of bytes per line */
#define VIDEO_BURST_LEN		(VIDEO_COLS/8)

#ifdef VIDEO_MODE_YUYV
#define VIDEO_BG_COL	0x80D880D8	/* Background color in YUYV format */
#else
#define VIDEO_BG_COL	0xF8F8F8F8	/* Background color in RGB format */
#endif

/************************************************************************/
/* ** FONT AND LOGO DATA						*/
/************************************************************************/

#include <video_font.h>			/* Get font data, width and height */

#ifdef CONFIG_VIDEO_LOGO
#include <video_logo.h>			/* Get logo data, width and height */

#define VIDEO_LOGO_WIDTH	DEF_U_BOOT_LOGO_WIDTH
#define VIDEO_LOGO_HEIGHT	DEF_U_BOOT_LOGO_HEIGHT
#define VIDEO_LOGO_ADDR		&u_boot_logo
#endif

/************************************************************************/
/* ** VIDEO CONTROLLER CONSTANTS					*/
/************************************************************************/

/* VCCR - VIDEO CONTROLLER CONFIGURATION REGISTER */

#define VIDEO_VCCR_VON	0		/* Video controller ON */
#define VIDEO_VCCR_CSRC	1		/* Clock source */
#define VIDEO_VCCR_PDF	13		/* Pixel display format */
#define VIDEO_VCCR_IEN	11		/* Interrupt enable */

/* VSR - VIDEO STATUS REGISTER */

#define VIDEO_VSR_CAS	6		/* Active set */
#define VIDEO_VSR_EOF	0		/* End of frame */

/* VCMR - VIDEO COMMAND REGISTER */

#define VIDEO_VCMR_BD	0		/* Blank display */
#define VIDEO_VCMR_ASEL	1		/* Active set selection */

/* VBCB - VIDEO BACKGROUND COLOR BUFFER REGISTER */

#define VIDEO_BCSR4_RESET_BIT	21	/* BCSR4 - Extern video encoder reset */
#define VIDEO_BCSR4_EXTCLK_BIT	22	/* BCSR4 - Extern clock enable */
#define VIDEO_BCSR4_VIDLED_BIT	23	/* BCSR4 - Video led disable */

/************************************************************************/
/* ** CONSOLE CONSTANTS							*/
/************************************************************************/

#ifdef	CONFIG_VIDEO_LOGO
#define CONSOLE_ROWS		((VIDEO_ROWS - VIDEO_LOGO_HEIGHT) / VIDEO_FONT_HEIGHT)
#define VIDEO_LOGO_SKIP		(VIDEO_COLS - VIDEO_LOGO_WIDTH)
#else
#define CONSOLE_ROWS		(VIDEO_ROWS / VIDEO_FONT_HEIGHT)
#endif

#define CONSOLE_COLS		(VIDEO_COLS / VIDEO_FONT_WIDTH)
#define CONSOLE_ROW_SIZE	(VIDEO_FONT_HEIGHT * VIDEO_LINE_LEN)
#define CONSOLE_ROW_FIRST	(video_console_address)
#define CONSOLE_ROW_SECOND	(video_console_address + CONSOLE_ROW_SIZE)
#define CONSOLE_ROW_LAST	(video_console_address + CONSOLE_SIZE - CONSOLE_ROW_SIZE)
#define CONSOLE_SIZE		(CONSOLE_ROW_SIZE * CONSOLE_ROWS)
#define CONSOLE_SCROLL_SIZE	(CONSOLE_SIZE - CONSOLE_ROW_SIZE)

/*
 * Simple color definitions
 */
#define CONSOLE_COLOR_BLACK	 0
#define CONSOLE_COLOR_RED	 1
#define CONSOLE_COLOR_GREEN	 2
#define CONSOLE_COLOR_YELLOW	 3
#define CONSOLE_COLOR_BLUE	 4
#define CONSOLE_COLOR_MAGENTA	 5
#define CONSOLE_COLOR_CYAN	 6
#define CONSOLE_COLOR_GREY	13
#define CONSOLE_COLOR_GREY2	14
#define CONSOLE_COLOR_WHITE	15	/* Must remain last / highest */

/************************************************************************/
/* ** BITOPS MACROS							*/
/************************************************************************/

#define HISHORT(i)	((i >> 16)&0xffff)
#define LOSHORT(i)	(i & 0xffff)
#define HICHAR(s)	((i >> 8)&0xff)
#define LOCHAR(s)	(i & 0xff)
#define HI(c)		((c >> 4)&0xf)
#define LO(c)		(c & 0xf)
#define SWAPINT(i)	(HISHORT(i) | (LOSHORT(i) << 16))
#define SWAPSHORT(s)	(HICHAR(s) | (LOCHAR(s) << 8))
#define SWAPCHAR(c)	(HI(c) | (LO(c) << 4))
#define BITMASK(b)	(1 << (b))
#define GETBIT(v,b)	(((v) & BITMASK(b)) > 0)
#define SETBIT(v,b,d)	(v = (((d)>0) ? (v) | BITMASK(b): (v) & ~BITMASK(b)))

/************************************************************************/
/* ** STRUCTURES							*/
/************************************************************************/

typedef struct {
	unsigned char V, Y1, U, Y2;
} tYUYV;

/* This structure is based on the Video Ram in the MPC823. */
typedef struct VRAM {
	unsigned	hx:2,		/* Horizontal sync */
			vx:2,		/* Vertical sync */
			fx:2,		/* Frame */
			bx:2,		/* Blank */
			res1:6,		/* Reserved */
			vds:2,		/* Video Data Select */
			inter:1,	/* Interrupt */
			res2:2,		/* Reserved */
			lcyc:11,	/* Loop/video cycles */
			lp:1,		/* Loop start/end */
			lst:1;		/* Last entry */
} VRAM;

/************************************************************************/
/* ** VARIABLES								*/
/************************************************************************/

static int
	video_panning_range_x = 0,	/* Video mode invisible pixels x range */
	video_panning_range_y = 0,	/* Video mode invisible pixels y range */
	video_panning_value_x = 0,	/* Video mode x panning value (absolute) */
	video_panning_value_y = 0,	/* Video mode y panning value (absolute) */
	video_panning_factor_x = 0,	/* Video mode x panning value (-127 +127) */
	video_panning_factor_y = 0,	/* Video mode y panning value (-127 +127) */
	console_col = 0,		/* Cursor col */
	console_row = 0,		/* Cursor row */
	video_palette[16];		/* Our palette */

static const int video_font_draw_table[] =
	{ 0x00000000, 0x0000ffff, 0xffff0000, 0xffffffff };

static char
	video_color_fg = 0,		/* Current fg color index (0-15) */
	video_color_bg = 0,		/* Current bg color index (0-15) */
	video_enable = 0;		/* Video has been initialized? */

static void
	*video_fb_address,		/* Frame buffer address */
	*video_console_address;		/* Console frame buffer start address */

/************************************************************************/
/* ** MEMORY FUNCTIONS (32bit)						*/
/************************************************************************/

static void memsetl (int *p, int c, int v)
{
	while (c--)
		*(p++) = v;
}

static void memcpyl (int *d, int *s, int c)
{
	while (c--)
		*(d++) = *(s++);
}

/************************************************************************/
/* ** VIDEO DRAWING AND COLOR FUNCTIONS					*/
/************************************************************************/

static int video_maprgb (int r, int g, int b)
{
#ifdef VIDEO_MODE_YUYV
	unsigned int pR, pG, pB;
	tYUYV YUYV;
	unsigned int *ret = (unsigned int *) &YUYV;

	/* Transform (0-255) components to (0-100) */

	pR = r * 100 / 255;
	pG = g * 100 / 255;
	pB = b * 100 / 255;

	/* Calculate YUV values (0-255) from RGB beetween 0-100 */

	YUYV.Y1 = YUYV.Y2 = 209 * (pR + pG + pB) / 300 + 16;
	YUYV.U	= pR - (pG * 3 / 4) - (pB / 4) + 128;
	YUYV.V	= pB - (pR / 4) - (pG * 3 / 4) + 128;
	return *ret;
#endif
#ifdef VIDEO_MODE_RGB
	return ((r >> 3) << 11) | ((g > 2) << 6) | (b >> 3);
#endif
}

static void video_setpalette (int color, int r, int g, int b)
{
	color &= 0xf;

	video_palette[color] = video_maprgb (r, g, b);

	/* Swap values if our panning offset is odd */
	if (video_panning_value_x & 1)
		video_palette[color] = SWAPINT (video_palette[color]);
}

static void video_fill (int color)
{
	memsetl (video_fb_address, VIDEO_PIX_BLOCKS, color);
}

static void video_setfgcolor (int i)
{
	video_color_fg = i & 0xf;
}

static void video_setbgcolor (int i)
{
	video_color_bg = i & 0xf;
}

static int video_pickcolor (int i)
{
	return video_palette[i & 0xf];
}

/* Absolute console plotting functions */

#ifdef VIDEO_BLINK
static void video_revchar (int xx, int yy)
{
	int rows;
	u8 *dest;

	dest = video_fb_address + yy * VIDEO_LINE_LEN + xx * 2;

	for (rows = VIDEO_FONT_HEIGHT; rows--; dest += VIDEO_LINE_LEN) {
		switch (VIDEO_FONT_WIDTH) {
		case 16:
			((u32 *) dest)[6] ^= 0xffffffff;
			((u32 *) dest)[7] ^= 0xffffffff;
			/* FALL THROUGH */
		case 12:
			((u32 *) dest)[4] ^= 0xffffffff;
			((u32 *) dest)[5] ^= 0xffffffff;
			/* FALL THROUGH */
		case 8:
			((u32 *) dest)[2] ^= 0xffffffff;
			((u32 *) dest)[3] ^= 0xffffffff;
			/* FALL THROUGH */
		case 4:
			((u32 *) dest)[0] ^= 0xffffffff;
			((u32 *) dest)[1] ^= 0xffffffff;
		}
	}
}
#endif

static void video_drawchars (int xx, int yy, unsigned char *s, int count)
{
	u8 *cdat, *dest, *dest0;
	int rows, offset, c;
	u32 eorx, fgx, bgx;

	offset = yy * VIDEO_LINE_LEN + xx * 2;
	dest0 = video_fb_address + offset;

	fgx = video_pickcolor (video_color_fg);
	bgx = video_pickcolor (video_color_bg);

	if (xx & 1) {
		fgx = SWAPINT (fgx);
		bgx = SWAPINT (bgx);
	}

	eorx = fgx ^ bgx;

	switch (VIDEO_FONT_WIDTH) {
	case 4:
	case 8:
		while (count--) {
			c = *s;
			cdat = video_fontdata + c * VIDEO_FONT_HEIGHT;
			for (rows = VIDEO_FONT_HEIGHT, dest = dest0;
			     rows--;
			     dest += VIDEO_LINE_LEN) {
				u8 bits = *cdat++;

				((u32 *) dest)[0] =
					(video_font_draw_table[bits >> 6] & eorx) ^ bgx;
				((u32 *) dest)[1] =
					(video_font_draw_table[bits >> 4 & 3] & eorx) ^ bgx;
				if (VIDEO_FONT_WIDTH == 8) {
					((u32 *) dest)[2] =
						(video_font_draw_table[bits >> 2 & 3] & eorx) ^ bgx;
					((u32 *) dest)[3] =
						(video_font_draw_table[bits & 3] & eorx) ^ bgx;
				}
			}
			dest0 += VIDEO_FONT_WIDTH * 2;
			s++;
		}
		break;
	case 12:
	case 16:
		while (count--) {
			cdat = video_fontdata + (*s) * (VIDEO_FONT_HEIGHT << 1);
			for (rows = VIDEO_FONT_HEIGHT, dest = dest0; rows--;
				 dest += VIDEO_LINE_LEN) {
				u8 bits = *cdat++;

				((u32 *) dest)[0] =
					(video_font_draw_table[bits >> 6] & eorx) ^ bgx;
				((u32 *) dest)[1] =
					(video_font_draw_table[bits >> 4 & 3] & eorx) ^ bgx;
				((u32 *) dest)[2] =
					(video_font_draw_table[bits >> 2 & 3] & eorx) ^ bgx;
				((u32 *) dest)[3] =
					(video_font_draw_table[bits & 3] & eorx) ^ bgx;
				bits = *cdat++;
				((u32 *) dest)[4] =
					(video_font_draw_table[bits >> 6] & eorx) ^ bgx;
				((u32 *) dest)[5] =
					(video_font_draw_table[bits >> 4 & 3] & eorx) ^ bgx;
				if (VIDEO_FONT_WIDTH == 16) {
					((u32 *) dest)[6] =
						(video_font_draw_table[bits >> 2 & 3] & eorx) ^ bgx;
					((u32 *) dest)[7] =
						(video_font_draw_table[bits & 3] & eorx) ^ bgx;
				}
			}
			s++;
			dest0 += VIDEO_FONT_WIDTH * 2;
		}
		break;
	}
}

static inline void video_drawstring (int xx, int yy, char *s)
{
	video_drawchars (xx, yy, (unsigned char *)s, strlen (s));
}

/* Relative to console plotting functions */

static void video_putchars (int xx, int yy, unsigned char *s, int count)
{
#ifdef CONFIG_VIDEO_LOGO
	video_drawchars (xx, yy + VIDEO_LOGO_HEIGHT, s, count);
#else
	video_drawchars (xx, yy, s, count);
#endif
}

static void video_putchar (int xx, int yy, unsigned char c)
{
#ifdef CONFIG_VIDEO_LOGO
	video_drawchars (xx, yy + VIDEO_LOGO_HEIGHT, &c, 1);
#else
	video_drawchars (xx, yy, &c, 1);
#endif
}

static inline void video_putstring (int xx, int yy, unsigned char *s)
{
	video_putchars (xx, yy, (unsigned char *)s, strlen ((char *)s));
}

/************************************************************************/
/* ** VIDEO CONTROLLER LOW-LEVEL FUNCTIONS				*/
/************************************************************************/

#if !defined(CONFIG_RRVISION)
static void video_mode_dupefield (VRAM * source, VRAM * dest, int entries)
{
	int i;

	for (i = 0; i < entries; i++) {
		dest[i] = source[i];	/* Copy the entire record */
		dest[i].fx = (!dest[i].fx) * 3;	/* Negate field bit */
	}

	dest[0].lcyc++;			/* Add a cycle to the first entry */
	dest[entries - 1].lst = 1;	/* Set end of ram entries */
}
#endif

static void inline video_mode_addentry (VRAM * vr,
	int Hx, int Vx, int Fx, int Bx,
	int VDS, int INT, int LCYC, int LP, int LST)
{
	vr->hx = Hx;
	vr->vx = Vx;
	vr->fx = Fx;
	vr->bx = Bx;
	vr->vds = VDS;
	vr->inter = INT;
	vr->lcyc = LCYC;
	vr->lp = LP;
	vr->lst = LST;
}

#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)

static int video_mode_generate (void)
{
	immap_t *immap = (immap_t *) CFG_IMMR;
	VRAM *vr = (VRAM *) (((void *) immap) + 0xb00);	/* Pointer to the VRAM table */
	int DX, X1, X2, DY, Y1, Y2, entry = 0, fifo;

	/* CHECKING PARAMETERS */

	if (video_panning_factor_y < -128)
		video_panning_factor_y = -128;

	if (video_panning_factor_y > 128)
		video_panning_factor_y = 128;

	if (video_panning_factor_x < -128)
		video_panning_factor_x = -128;

	if (video_panning_factor_x > 128)
		video_panning_factor_x = 128;

	/* Setting panning */

	DX = video_panning_range_x = (VIDEO_ACTIVE_COLS - VIDEO_COLS) * 2;
	DY = video_panning_range_y = (VIDEO_ACTIVE_ROWS - VIDEO_ROWS) / 2;

	video_panning_value_x = (video_panning_factor_x + 128) * DX / 256;
	video_panning_value_y = (video_panning_factor_y + 128) * DY / 256;

	/* We assume these are burst units (multiplied by 2, we need it pari) */
	X1 = video_panning_value_x & 0xfffe;
	X2 = DX - X1;

	/* We assume these are field line units (divided by 2, we need it pari) */
	Y1 = video_panning_value_y & 0xfffe;
	Y2 = DY - Y1;

	debug("X1=%d, X2=%d, Y1=%d, Y2=%d, DX=%d, DY=%d VIDEO_COLS=%d \n",
	      X1, X2, Y1, Y2, DX, DY, VIDEO_COLS);

#ifdef VIDEO_MODE_NTSC
/*
 *	     Hx Vx Fx Bx VDS INT LCYC LP LST
 *
 * Retrace blanking
 */
	ADDENTRY (0, 0, 3, 0, 1, 0, 3, 1, 0);
	ADDENTRY (3, 0, 3, 0, 1, 0, 243, 0, 0);
	ADDENTRY (3, 0, 3, 0, 1, 0, 1440, 0, 0);
	ADDENTRY (3, 0, 3, 0, 1, 0, 32, 1, 0);
/*
 * Vertical blanking
 */
	ADDENTRY (0, 0, 0, 0, 1, 0, 18, 1, 0);
	ADDENTRY (3, 0, 0, 0, 1, 0, 243, 0, 0);
	ADDENTRY (3, 0, 0, 0, 1, 0, 1440, 0, 0);
	ADDENTRY (3, 0, 0, 0, 1, 0, 32, 1, 0);
/*
 * Odd field active area (TOP)
 */
	if (Y1 > 0) {
		ADDENTRY (0, 0, 0, 0, 1, 0, Y1, 1, 0);
		ADDENTRY (3, 0, 0, 0, 1, 0, 235, 0, 0);
		ADDENTRY (3, 0, 0, 3, 1, 0, 1448, 0, 0);
		ADDENTRY (3, 0, 0, 0, 1, 0, 32, 1, 0);
	}
/*
 * Odd field active area
 */
	ADDENTRY (0, 0, 0, 0, 1, 0, 240 - DY, 1, 0);
	ADDENTRY (3, 0, 0, 0, 1, 0, 235, 0, 0);
	ADDENTRY (3, 0, 0, 3, 1, 0, 8 + X1, 0, 0);
	ADDENTRY (3, 0, 0, 3, 0, 0, VIDEO_COLS * 2, 0, 0);

	if (X2 > 0)
		ADDENTRY (3, 0, 0, 3, 1, 0, X2, 0, 0);

	ADDENTRY (3, 0, 0, 0, 1, 0, 32, 1, 0);

/*
 * Odd field active area (BOTTOM)
 */
	if (Y1 > 0) {
		ADDENTRY (0, 0, 0, 0, 1, 0, Y2, 1, 0);
		ADDENTRY (3, 0, 0, 0, 1, 0, 235, 0, 0);
		ADDENTRY (3, 0, 0, 3, 1, 0, 1448, 0, 0);
		ADDENTRY (3, 0, 0, 0, 1, 0, 32, 1, 0);
	}
/*
 * Vertical blanking
 */
	ADDENTRY (0, 0, 0, 0, 1, 0, 4, 1, 0);
	ADDENTRY (3, 0, 0, 0, 1, 0, 243, 0, 0);
	ADDENTRY (3, 0, 0, 0, 1, 0, 1440, 0, 0);
	ADDENTRY (3, 0, 0, 0, 1, 0, 32, 1, 0);
/*
 * Vertical blanking
 */
	ADDENTRY (0, 0, 3, 0, 1, 0, 19, 1, 0);
	ADDENTRY (3, 0, 3, 0, 1, 0, 243, 0, 0);
	ADDENTRY (3, 0, 3, 0, 1, 0, 1440, 0, 0);
	ADDENTRY (3, 0, 3, 0, 1, 0, 32, 1, 0);
/*
 * Even field active area (TOP)
 */
	if (Y1 > 0) {
		ADDENTRY (0, 0, 3, 0, 1, 0, Y1, 1, 0);
		ADDENTRY (3, 0, 3, 0, 1, 0, 235, 0, 0);
		ADDENTRY (3, 0, 3, 3, 1, 0, 1448, 0, 0);
		ADDENTRY (3, 0, 3, 0, 1, 0, 32, 1, 0);
	}
/*
 * Even field active area (CENTER)
 */
	ADDENTRY (0, 0, 3, 0, 1, 0, 240 - DY, 1, 0);
	ADDENTRY (3, 0, 3, 0, 1, 0, 235, 0, 0);
	ADDENTRY (3, 0, 3, 3, 1, 0, 8 + X1, 0, 0);
	ADDENTRY (3, 0, 3, 3, 0, 0, VIDEO_COLS * 2, 0, 0);

	if (X2 > 0)
		ADDENTRY (3, 0, 3, 3, 1, 0, X2, 0, 0);

	ADDENTRY (3, 0, 3, 0, 1, 0, 32, 1, 0);
/*
 * Even field active area (BOTTOM)
 */
	if (Y1 > 0) {
		ADDENTRY (0, 0, 3, 0, 1, 0, Y2, 1, 0);
		ADDENTRY (3, 0, 3, 0, 1, 0, 235, 0, 0);
		ADDENTRY (3, 0, 3, 3, 1, 0, 1448, 0, 0);
		ADDENTRY (3, 0, 3, 0, 1, 0, 32, 1, 0);
	}
/*
 * Vertical blanking
 */
	ADDENTRY (0, 0, 3, 0, 1, 0, 1, 1, 0);
	ADDENTRY (3, 0, 3, 0, 1, 0, 243, 0, 0);
	ADDENTRY (3, 0, 3, 0, 1, 0, 1440, 0, 0);
	ADDENTRY (3, 0, 3, 0, 1, 1, 32, 1, 1);
#endif

#ifdef VIDEO_MODE_PAL

#if defined(CONFIG_RRVISION)

#define HPW   160  /* horizontal pulse width (was 139)	*/
#define VPW	2  /* vertical pulse width		*/
#define HBP   104  /* horizontal back porch (was 112)	*/
#define VBP    19  /* vertical back porch (was 19)	*/
#define VID_R 240  /* number of rows			*/

	debug ("[VIDEO CTRL] Starting to add controller entries...");
/*
 * Even field
 */
	ADDENTRY (0, 3, 0, 3, 1, 0, 2, 0, 0);
	ADDENTRY (0, 0, 0, 3, 1, 0, HPW, 0, 0);
	ADDENTRY (3, 0, 0, 3, 1, 0, HBP + (VIDEO_COLS * 2) + 72, 0, 0);

	ADDENTRY (0, 0, 0, 3, 1, 0, VPW, 1, 0);
	ADDENTRY (0, 0, 0, 3, 1, 0, HPW-1, 0, 0);
	ADDENTRY (3, 0, 0, 3, 1, 0, HBP + (VIDEO_COLS * 2) + 72, 1, 0);

	ADDENTRY (0, 3, 0, 3, 1, 0, VBP, 1, 0);
	ADDENTRY (0, 3, 0, 3, 1, 0, HPW-1, 0, 0);
	ADDENTRY (3, 3, 0, 3, 1, 0, HBP + (VIDEO_COLS * 2) + 72, 1, 0);
/*
 * Active area
 */
	ADDENTRY (0, 3, 0, 3, 1, 0, VID_R , 1, 0);
	ADDENTRY (0, 3, 0, 3, 1, 0, HPW-1, 0, 0);
	ADDENTRY (3, 3, 0, 3, 1, 0, HBP, 0, 0);
	ADDENTRY (3, 3, 0, 3, 0, 0, VIDEO_COLS*2, 0, 0);
	ADDENTRY (3, 3, 0, 3, 1, 0, 72, 1, 1);

	ADDENTRY (0, 3, 0, 3, 1, 0, 51, 1, 0);
	ADDENTRY (0, 3, 0, 3, 1, 0, HPW-1, 0, 0);
	ADDENTRY (3, 3, 0, 3, 1, 0, HBP +(VIDEO_COLS * 2) + 72 , 1, 0);
/*
 * Odd field
 */
	ADDENTRY (0, 3, 0, 3, 1, 0, 2, 0, 0);
	ADDENTRY (0, 0, 0, 3, 1, 0, HPW, 0, 0);
	ADDENTRY (3, 0, 0, 3, 1, 0, HBP + (VIDEO_COLS * 2) + 72, 0, 0);

	ADDENTRY (0, 0, 0, 3, 1, 0, VPW+1, 1, 0);
	ADDENTRY (0, 0, 0, 3, 1, 0, HPW-1, 0, 0);
	ADDENTRY (3, 0, 0, 3, 1, 0, HBP + (VIDEO_COLS * 2) + 72, 1, 0);

	ADDENTRY (0, 3, 0, 3, 1, 0, VBP, 1, 0);
	ADDENTRY (0, 3, 0, 3, 1, 0, HPW-1, 0, 0);
	ADDENTRY (3, 3, 0, 3, 1, 0, HBP + (VIDEO_COLS * 2) + 72, 1, 0);
/*
 * Active area
 */
	ADDENTRY (0, 3, 0, 3, 1, 0, VID_R , 1, 0);
	ADDENTRY (0, 3, 0, 3, 1, 0, HPW-1, 0, 0);
	ADDENTRY (3, 3, 0, 3, 1, 0, HBP, 0, 0);
	ADDENTRY (3, 3, 0, 3, 0, 0, VIDEO_COLS*2, 0, 0);
	ADDENTRY (3, 3, 0, 3, 1, 0, 72, 1, 1);

	ADDENTRY (0, 3, 0, 3, 1, 0, 51, 1, 0);
	ADDENTRY (0, 3, 0, 3, 1, 0, HPW-1, 0, 0);
	ADDENTRY (3, 3, 0, 3, 1, 0, HBP +(VIDEO_COLS * 2) + 72 , 1, 0);

	debug ("done\n");

#else  /* !CONFIG_RRVISION */

/*
 *	Hx Vx Fx Bx VDS INT LCYC LP LST
 *
 * vertical; blanking
 */
	ADDENTRY (0, 0, 0, 0, 1, 0, 22, 1, 0);
	ADDENTRY (3, 0, 0, 0, 1, 0, 263, 0, 0);
	ADDENTRY (3, 0, 0, 0, 1, 0, 1440, 0, 0);
	ADDENTRY (3, 0, 0, 0, 1, 0, 24, 1, 0);
/*
 * active area (TOP)
 */
	if (Y1 > 0) {
		ADDENTRY (0, 0, 0, 0, 1, 0, Y1, 1, 0);	/* 11? */
		ADDENTRY (3, 0, 0, 0, 1, 0, 255, 0, 0);
		ADDENTRY (3, 0, 0, 3, 1, 0, 1448, 0, 0);
		ADDENTRY (3, 0, 0, 0, 1, 0, 24, 1, 0);
	}
/*
 * field active area (CENTER)
 */
	ADDENTRY (0, 0, 0, 0, 1, 0, 288 - DY, 1, 0);	/* 265? */
	ADDENTRY (3, 0, 0, 0, 1, 0, 255, 0, 0);
	ADDENTRY (3, 0, 0, 3, 1, 0, 8 + X1, 0, 0);
	ADDENTRY (3, 0, 0, 3, 0, 0, VIDEO_COLS * 2, 0, 0);

	if (X2 > 0)
		ADDENTRY (3, 0, 0, 1, 1, 0, X2, 0, 0);

	ADDENTRY (3, 0, 0, 0, 1, 0, 24, 1, 0);
/*
 * field active area (BOTTOM)
 */
	if (Y2 > 0) {
		ADDENTRY (0, 0, 0, 0, 1, 0, Y2, 1, 0);	/* 12? */
		ADDENTRY (3, 0, 0, 0, 1, 0, 255, 0, 0);
		ADDENTRY (3, 0, 0, 3, 1, 0, 1448, 0, 0);
		ADDENTRY (3, 0, 0, 0, 1, 0, 24, 1, 0);
	}
/*
 * field vertical; blanking
 */
	ADDENTRY (0, 0, 0, 0, 1, 0, 2, 1, 0);
	ADDENTRY (3, 0, 0, 0, 1, 0, 263, 0, 0);
	ADDENTRY (3, 0, 0, 0, 1, 0, 1440, 0, 0);
	ADDENTRY (3, 0, 0, 0, 1, 0, 24, 1, 0);
/*
 * Create the other field (like this, but whit other field selected,
 * one more cycle loop and a last identifier)
 */
	video_mode_dupefield (vr, &vr[entry], entry);
#endif /* CONFIG_RRVISION */

#endif /* VIDEO_MODE_PAL */

	/* See what FIFO are we using */
	fifo = GETBIT (immap->im_vid.vid_vsr, VIDEO_VSR_CAS);

	/* Set number of lines and burst (only one frame for now) */
	if (fifo) {
		immap->im_vid.vid_vfcr0 = VIDEO_BURST_LEN |
			(VIDEO_BURST_LEN << 8) | ((VIDEO_ROWS / 2) << 19);
	} else {
		immap->im_vid.vid_vfcr1 = VIDEO_BURST_LEN |
			(VIDEO_BURST_LEN << 8) | ((VIDEO_ROWS / 2) << 19);
	}

	SETBIT (immap->im_vid.vid_vcmr, VIDEO_VCMR_ASEL, !fifo);

/*
 * Wait until changes are applied (not done)
 * while (GETBIT(immap->im_vid.vid_vsr, VIDEO_VSR_CAS) == fifo) ;
 */

	/* Return number of VRAM entries */
	return entry * 2;
}

static void video_encoder_init (void)
{
#ifdef VIDEO_I2C
	int rc;

	/* Initialize the I2C */
	debug ("[VIDEO ENCODER] Initializing I2C bus...\n");
	i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE);

#ifdef CONFIG_FADS
	/* Reset ADV7176 chip */
	debug ("[VIDEO ENCODER] Resetting encoder...\n");
	(*(int *) BCSR4) &= ~(1 << 21);

	/* Wait for 5 ms inside the reset */
	debug ("[VIDEO ENCODER] Waiting for encoder reset...\n");
	udelay (5000);

	/* Take ADV7176 out of reset */
	(*(int *) BCSR4) |= 1 << 21;

	/* Wait for 5 ms after the reset */
	udelay (5000);
#endif	/* CONFIG_FADS */

	/* Send configuration */
#ifdef DEBUG
	{
		int i;

		puts ("[VIDEO ENCODER] Configuring the encoder...\n");

		printf ("Sending %zu bytes (@ %08lX) to I2C 0x%lX:\n   ",
			sizeof(video_encoder_data),
			(ulong)video_encoder_data,
			(ulong)VIDEO_I2C_ADDR);
		for (i=0; i<sizeof(video_encoder_data); ++i) {
			printf(" %02X", video_encoder_data[i]);
		}
		putc ('\n');
	}
#endif	/* DEBUG */

	if ((rc = i2c_write (VIDEO_I2C_ADDR, 0, 1,
			 video_encoder_data,
			 sizeof(video_encoder_data))) != 0) {
		printf ("i2c_send error: rc=%d\n", rc);
		return;
	}
#endif	/* VIDEO_I2C */
	return;
}

static void video_ctrl_init (void *memptr)
{
	immap_t *immap = (immap_t *) CFG_IMMR;

	video_fb_address = memptr;

	/* Set background */
	debug ("[VIDEO CTRL] Setting background color...\n");
	immap->im_vid.vid_vbcb = VIDEO_BG_COL;

	/* Show the background */
	debug ("[VIDEO CTRL] Forcing background...\n");
	SETBIT (immap->im_vid.vid_vcmr, VIDEO_VCMR_BD, 1);

	/* Turn off video controller */
	debug ("[VIDEO CTRL] Turning off video controller...\n");
	SETBIT (immap->im_vid.vid_vccr, VIDEO_VCCR_VON, 0);

#ifdef CONFIG_FADS
	/* Turn on Video Port LED */
	debug ("[VIDEO CTRL] Turning off video port led...\n");
	SETBIT (*(int *) BCSR4, VIDEO_BCSR4_VIDLED_BIT, 1);

	/* Disable internal clock */
	debug ("[VIDEO CTRL] Disabling internal clock...\n");
	SETBIT (*(int *) BCSR4, VIDEO_BCSR4_EXTCLK_BIT, 0);
#endif

	/* Generate and make active a new video mode */
	debug ("[VIDEO CTRL] Generating video mode...\n");
	video_mode_generate ();

	/* Start of frame buffer (even and odd frame, to make it working with */
	/* any selected active set) */
	debug ("[VIDEO CTRL] Setting frame buffer address...\n");
	immap->im_vid.vid_vfaa1 =
		immap->im_vid.vid_vfaa0 = (u32) video_fb_address;
	immap->im_vid.vid_vfba1 =
	immap->im_vid.vid_vfba0 =
		(u32) video_fb_address + VIDEO_LINE_LEN;

	/* YUV, Big endian, SHIFT/CLK/CLK input (BEFORE ENABLING 27MHZ EXT CLOCK) */
	debug ("[VIDEO CTRL] Setting pixel mode and clocks...\n");
	immap->im_vid.vid_vccr = 0x2042;

	/* Configure port pins */
	debug ("[VIDEO CTRL] Configuring input/output pins...\n");
	immap->im_ioport.iop_pdpar = 0x1fff;
	immap->im_ioport.iop_pddir = 0x0000;

#ifdef CONFIG_FADS
	/* Turn on Video Port Clock - ONLY AFTER SET VCCR TO ENABLE EXTERNAL CLOCK */
	debug ("[VIDEO CTRL] Turning on video clock...\n");
	SETBIT (*(int *) BCSR4, VIDEO_BCSR4_EXTCLK_BIT, 1);

	/* Turn on Video Port LED */
	debug ("[VIDEO CTRL] Turning on video port led...\n");
	SETBIT (*(int *) BCSR4, VIDEO_BCSR4_VIDLED_BIT, 0);
#endif
#ifdef CONFIG_RRVISION
	debug ("PC5->Output(1): enable PAL clock");
	immap->im_ioport.iop_pcpar &= ~(0x0400);
	immap->im_ioport.iop_pcdir |=   0x0400 ;
	immap->im_ioport.iop_pcdat |=   0x0400 ;
	debug ("PDPAR=0x%04X PDDIR=0x%04X PDDAT=0x%04X\n",
	       immap->im_ioport.iop_pdpar,
	       immap->im_ioport.iop_pddir,
	       immap->im_ioport.iop_pddat);
	debug ("PCPAR=0x%04X PCDIR=0x%04X PCDAT=0x%04X\n",
	       immap->im_ioport.iop_pcpar,
	       immap->im_ioport.iop_pcdir,
	       immap->im_ioport.iop_pcdat);
#endif	/* CONFIG_RRVISION */

	/* Blanking the screen. */
	debug ("[VIDEO CTRL] Blanking the screen...\n");
	video_fill (VIDEO_BG_COL);

	/*
	 * Turns on Aggressive Mode. Normally, turning on the caches
	 * will cause the screen to flicker when the caches try to
	 * fill. This gives the FIFO's for the Video Controller
	 * higher priority and prevents flickering because of
	 * underrun. This may still be an issue when using FLASH,
	 * since accessing data from Flash is so slow.
	 */
	debug ("[VIDEO CTRL] Turning on aggressive mode...\n");
	immap->im_siu_conf.sc_sdcr = 0x40;

	/* Turn on video controller */
	debug ("[VIDEO CTRL] Turning on video controller...\n");
	SETBIT (immap->im_vid.vid_vccr, VIDEO_VCCR_VON, 1);

	/* Show the display */
	debug ("[VIDEO CTRL] Enabling the video...\n");
	SETBIT (immap->im_vid.vid_vcmr, VIDEO_VCMR_BD, 0);
}

/************************************************************************/
/* ** CONSOLE FUNCTIONS							*/
/************************************************************************/

static void console_scrollup (void)
{
	/* Copy up rows ignoring the first one */
	memcpyl (CONSOLE_ROW_FIRST, CONSOLE_ROW_SECOND, CONSOLE_SCROLL_SIZE >> 2);

	/* Clear the last one */
	memsetl (CONSOLE_ROW_LAST, CONSOLE_ROW_SIZE >> 2, VIDEO_BG_COL);
}

static inline void console_back (void)
{
	console_col--;

	if (console_col < 0) {
		console_col = CONSOLE_COLS - 1;
		console_row--;
		if (console_row < 0)
			console_row = 0;
	}

	video_putchar ( console_col * VIDEO_FONT_WIDTH,
			console_row * VIDEO_FONT_HEIGHT, ' ');
}

static inline void console_newline (void)
{
	console_row++;
	console_col = 0;

	/* Check if we need to scroll the terminal */
	if (console_row >= CONSOLE_ROWS) {
		/* Scroll everything up */
		console_scrollup ();

		/* Decrement row number */
		console_row--;
	}
}

void video_putc (const char c)
{
	if (!video_enable) {
		serial_putc (c);
		return;
	}

	switch (c) {
	case 13:			/* Simply ignore this */
		break;

	case '\n':			/* Next line, please */
		console_newline ();
		break;

	case 9:				/* Tab (8 chars alignment) */
		console_col |= 0x0008;	/* Next 8 chars boundary */
		console_col &= ~0x0007;	/* Set this bit to zero */

		if (console_col >= CONSOLE_COLS)
			console_newline ();
		break;

	case 8:				/* Eat last character */
		console_back ();
		break;

	default:			/* Add to the console */
		video_putchar ( console_col * VIDEO_FONT_WIDTH,
				console_row * VIDEO_FONT_HEIGHT, c);
		console_col++;
		/* Check if we need to go to next row */
		if (console_col >= CONSOLE_COLS)
			console_newline ();
	}
}

void video_puts (const char *s)
{
	int count = strlen (s);

	if (!video_enable)
		while (count--)
			serial_putc (*s++);
	else
		while (count--)
			video_putc (*s++);
}

/************************************************************************/
/* ** CURSOR BLINKING FUNCTIONS						*/
/************************************************************************/

#ifdef VIDEO_BLINK

#define BLINK_TIMER_ID		0
#define BLINK_TIMER_HZ		2

static unsigned char blink_enabled = 0;
static timer_t blink_timer;

static void blink_update (void)
{
	static int blink_row = -1, blink_col = -1, blink_old = 0;

	/* Check if we have a new position to invert */
	if ((console_row != blink_row) || (console_col != blink_col)) {
		/* Check if we need to reverse last character */
		if (blink_old)
			video_revchar ( blink_col * VIDEO_FONT_WIDTH,
					(blink_row
#ifdef CONFIG_VIDEO_LOGO
					 + VIDEO_LOGO_HEIGHT
#endif
					) * VIDEO_FONT_HEIGHT);

		/* Update values */
		blink_row = console_row;
		blink_col = console_col;
		blink_old = 0;
	}

/* Reverse this character */
	blink_old = !blink_old;
	video_revchar ( console_col * VIDEO_FONT_WIDTH,
			(console_row
#ifdef CONFIG_VIDEO_LOGO
			+ VIDEO_LOGO_HEIGHT
#endif
			) * VIDEO_FONT_HEIGHT);

}

/*
 * Handler for blinking cursor
 */
static void blink_handler (void *arg)
{
/* Blink */
	blink_update ();
/* Ack the timer */
	timer_ack (&blink_timer);
}

int blink_set (int blink)
{
	int ret = blink_enabled;

	if (blink)
		timer_enable (&blink_timer);
	else
		timer_disable (&blink_timer);

	blink_enabled = blink;

	return ret;
}

static inline void blink_close (void)
{
	timer_close (&blink_timer);
}

static inline void blink_init (void)
{
	timer_init (&blink_timer,
			BLINK_TIMER_ID, BLINK_TIMER_HZ,
			blink_handler);
}
#endif

/************************************************************************/
/* ** LOGO PLOTTING FUNCTIONS						*/
/************************************************************************/

#ifdef CONFIG_VIDEO_LOGO
void easylogo_plot (fastimage_t * image, void *screen, int width, int x,
					int y)
{
	int skip = width - image->width, xcount, ycount = image->height;

#ifdef VIDEO_MODE_YUYV
	ushort *source = (ushort *) image->data;
	ushort *dest   = (ushort *) screen + y * width + x;

	while (ycount--) {
		xcount = image->width;
		while (xcount--)
			*dest++ = *source++;
		dest += skip;
	}
#endif
#ifdef VIDEO_MODE_RGB
	unsigned char
	*source = (unsigned short *) image->data,
			*dest = (unsigned short *) screen + ((y * width) + x) * 3;

	while (ycount--) {
		xcount = image->width * 3;
		memcpy (dest, source, xcount);
		source += xcount;
		dest += ycount;
	}
#endif
}

static void *video_logo (void)
{
	u16 *screen = video_fb_address, width = VIDEO_COLS;
#ifdef VIDEO_INFO
# ifndef CONFIG_FADS
	char temp[32];
# endif
	char info[80];
#endif /* VIDEO_INFO */

	easylogo_plot (VIDEO_LOGO_ADDR, screen, width, 0, 0);

#ifdef VIDEO_INFO
	sprintf (info, "%s (%s - %s) ", U_BOOT_VERSION, __DATE__, __TIME__);
	video_drawstring (VIDEO_INFO_X, VIDEO_INFO_Y, info);

	sprintf (info, "(C) 2002 DENX Software Engineering");
	video_drawstring (VIDEO_INFO_X, VIDEO_INFO_Y + VIDEO_FONT_HEIGHT,
					info);

	sprintf (info, "    Wolfgang DENK, wd@denx.de");
	video_drawstring (VIDEO_INFO_X, VIDEO_INFO_Y + VIDEO_FONT_HEIGHT * 2,
					info);
#ifndef CONFIG_FADS		/* all normal boards */
	/* leave one blank line */

	sprintf (info, "MPC823 CPU at %s MHz, %ld MB RAM, %ld MB Flash",
		strmhz(temp, gd->cpu_clk),
		gd->ram_size >> 20,
		gd->bd->bi_flashsize >> 20 );
	video_drawstring (VIDEO_INFO_X, VIDEO_INFO_Y + VIDEO_FONT_HEIGHT * 4,
					info);
#else				/* FADS :-( */
	sprintf (info, "MPC823 CPU at 50 MHz on FADS823 board");
	video_drawstring (VIDEO_INFO_X, VIDEO_INFO_Y + VIDEO_FONT_HEIGHT,
					  info);

	sprintf (info, "2MB FLASH - 8MB DRAM - 4MB SRAM");
	video_drawstring (VIDEO_INFO_X, VIDEO_INFO_Y + VIDEO_FONT_HEIGHT * 2,
					  info);
#endif
#endif

	return video_fb_address + VIDEO_LOGO_HEIGHT * VIDEO_LINE_LEN;
}
#endif

/************************************************************************/
/* ** VIDEO HIGH-LEVEL FUNCTIONS					*/
/************************************************************************/

static int video_init (void *videobase)
{
	/* Initialize the encoder */
	debug ("[VIDEO] Initializing video encoder...\n");
	video_encoder_init ();

	/* Initialize the video controller */
	debug ("[VIDEO] Initializing video controller at %08x...\n",
		   (int) videobase);
	video_ctrl_init (videobase);

	/* Setting the palette */
	video_setpalette  (CONSOLE_COLOR_BLACK,	     0,	   0,	 0);
	video_setpalette  (CONSOLE_COLOR_RED,	  0xFF,	   0,	 0);
	video_setpalette  (CONSOLE_COLOR_GREEN,	     0, 0xFF,	 0);
	video_setpalette  (CONSOLE_COLOR_YELLOW,  0xFF, 0xFF,	 0);
	video_setpalette  (CONSOLE_COLOR_BLUE,	     0,	   0, 0xFF);
	video_setpalette  (CONSOLE_COLOR_MAGENTA, 0xFF,	   0, 0xFF);
	video_setpalette  (CONSOLE_COLOR_CYAN,	     0, 0xFF, 0xFF);
	video_setpalette  (CONSOLE_COLOR_GREY,	  0xAA, 0xAA, 0xAA);
	video_setpalette  (CONSOLE_COLOR_GREY2,	  0xF8, 0xF8, 0xF8);
	video_setpalette  (CONSOLE_COLOR_WHITE,	  0xFF, 0xFF, 0xFF);

#ifndef CFG_WHITE_ON_BLACK
	video_setfgcolor (CONSOLE_COLOR_BLACK);
	video_setbgcolor (CONSOLE_COLOR_GREY2);
#else
	video_setfgcolor (CONSOLE_COLOR_GREY2);
	video_setbgcolor (CONSOLE_COLOR_BLACK);
#endif	/* CFG_WHITE_ON_BLACK */

#ifdef CONFIG_VIDEO_LOGO
	/* Paint the logo and retrieve tv base address */
	debug ("[VIDEO] Drawing the logo...\n");
	video_console_address = video_logo ();
#else
	video_console_address = video_fb_address;
#endif

#ifdef VIDEO_BLINK
	/* Enable the blinking (under construction) */
	blink_init ();
	blink_set (0);				/* To Fix! */
#endif

	/* Initialize the console */
	console_col = 0;
	console_row = 0;
	video_enable = 1;

#ifdef VIDEO_MODE_PAL
# define VIDEO_MODE_TMP1	"PAL"
#endif
#ifdef VIDEO_MODE_NTSC
# define VIDEO_MODE_TMP1	"NTSC"
#endif
#ifdef VIDEO_MODE_YUYV
# define VIDEO_MODE_TMP2	"YCbYCr"
#endif
#ifdef VIDEO_MODE_RGB
# define VIDEO_MODE_TMP2	"RGB"
#endif
	debug ( VIDEO_MODE_TMP1
		" %dx%dx%d (" VIDEO_MODE_TMP2 ") on %s - console %dx%d\n",
			VIDEO_COLS, VIDEO_ROWS, VIDEO_MODE_BPP,
			VIDEO_ENCODER_NAME, CONSOLE_COLS, CONSOLE_ROWS);
	return 0;
}

int drv_video_init (void)
{
	int error, devices = 1;

	device_t videodev;

	video_init ((void *)(gd->fb_base));	/* Video initialization */

/* Device initialization */

	memset (&videodev, 0, sizeof (videodev));

	strcpy (videodev.name, "video");
	videodev.ext = DEV_EXT_VIDEO;	/* Video extensions */
	videodev.flags = DEV_FLAGS_OUTPUT;	/* Output only */
	videodev.putc = video_putc;	/* 'putc' function */
	videodev.puts = video_puts;	/* 'puts' function */

	error = device_register (&videodev);

	return (error == 0) ? devices : error;
}

/************************************************************************/
/* ** ROM capable initialization part - needed to reserve FB memory	*/
/************************************************************************/

/*
 * This is called early in the system initialization to grab memory
 * for the video controller.
 * Returns new address for monitor, after reserving video buffer memory
 *
 * Note that this is running from ROM, so no write access to global data.
 */
ulong video_setmem (ulong addr)
{
	/* Allocate pages for the frame buffer. */
	addr -= VIDEO_SIZE;

	debug ("Reserving %dk for Video Framebuffer at: %08lx\n",
		VIDEO_SIZE>>10, addr);

	return (addr);
}

#endif