]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - cpu/mpc8xx/lcd.c
f6dda607ea5eec71becdb49ee9507320edd034be
[karo-tx-uboot.git] / cpu / mpc8xx / lcd.c
1 /*
2  * (C) Copyright 2001-2002
3  * Wolfgang Denk, DENX Software Engineering -- wd@denx.de
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 /* ** HEADER FILES                                                      */
26 /************************************************************************/
27
28 #include <config.h>
29 #include <common.h>
30 #include <version.h>
31 #include <stdarg.h>
32 #include <lcdvideo.h>
33 #include <linux/types.h>
34 #include <devices.h>
35
36
37 #ifdef CONFIG_LCD
38
39 /************************************************************************/
40 /* ** CONFIG STUFF -- should be moved to board config file              */
41 /************************************************************************/
42 #ifndef CONFIG_EDT32F10
43 #define CONFIG_LCD_LOGO
44 #define LCD_INFO                /* Display Logo, (C) and system info    */
45 #endif
46
47 #ifdef CONFIG_V37
48 #undef CONFIG_LCD_LOGO
49 #undef LCD_INFO
50 #endif
51
52 /* #define LCD_TEST_PATTERN */  /* color backgnd for frame/color adjust */
53 /* #define CFG_INVERT_COLORS */ /* Not needed - adjust vl_dp instead    */
54 /************************************************************************/
55
56 /************************************************************************/
57 /* ** FONT AND LOGO DATA                                                */
58 /************************************************************************/
59
60 #include <video_font.h>         /* Get font data, width and height      */
61
62 #ifdef CONFIG_LCD_LOGO
63 # include <bmp_logo.h>          /* Get logo data, width and height      */
64 #endif
65
66 /************************************************************************/
67 /************************************************************************/
68
69 /*
70  *  Information about displays we are using.  This is for configuring
71  *  the LCD controller and memory allocation.  Someone has to know what
72  *  is connected, as we can't autodetect anything.
73  */
74 #define CFG_HIGH        0       /* Pins are active high */
75 #define CFG_LOW         1       /* Pins are active low */
76
77 typedef struct vidinfo {
78     ushort      vl_col;         /* Number of columns (i.e. 640) */
79     ushort      vl_row;         /* Number of rows (i.e. 480) */
80     ushort      vl_width;       /* Width of display area in millimeters */
81     ushort      vl_height;      /* Height of display area in millimeters */
82
83     /* LCD configuration register.
84     */
85     u_char      vl_clkp;        /* Clock polarity */
86     u_char      vl_oep;         /* Output Enable polarity */
87     u_char      vl_hsp;         /* Horizontal Sync polarity */
88     u_char      vl_vsp;         /* Vertical Sync polarity */
89     u_char      vl_dp;          /* Data polarity */
90     u_char      vl_bpix;        /* Bits per pixel, 0 = 1, 1 = 2, 2 = 4, 3 = 8 */
91     u_char      vl_lbw;         /* LCD Bus width, 0 = 4, 1 = 8 */
92     u_char      vl_splt;        /* Split display, 0 = single-scan, 1 = dual-scan */
93     u_char      vl_clor;        /* Color, 0 = mono, 1 = color */
94     u_char      vl_tft;         /* 0 = passive, 1 = TFT */
95
96     /* Horizontal control register.  Timing from data sheet.
97     */
98     ushort      vl_wbl;         /* Wait between lines */
99
100     /* Vertical control register.
101     */
102     u_char      vl_vpw;         /* Vertical sync pulse width */
103     u_char      vl_lcdac;       /* LCD AC timing */
104     u_char      vl_wbf;         /* Wait between frames */
105 } vidinfo_t;
106
107 #define LCD_MONOCHROME  0
108 #define LCD_COLOR2      1
109 #define LCD_COLOR4      2
110 #define LCD_COLOR8      3
111
112 /*----------------------------------------------------------------------*/
113 #ifdef CONFIG_KYOCERA_KCS057QV1AJ
114 /*
115  *  Kyocera KCS057QV1AJ-G23. Passive, color, single scan.
116  */
117 #define LCD_BPP LCD_COLOR4
118
119 static vidinfo_t panel_info = {
120     640, 480, 132, 99, CFG_HIGH, CFG_HIGH, CFG_HIGH, CFG_HIGH, CFG_HIGH,
121     LCD_BPP, 1, 0, 1, 0,  5, 0, 0, 0
122                 /* wbl, vpw, lcdac, wbf */
123 };
124 #endif /* CONFIG_KYOCERA_KCS057QV1AJ */
125 /*----------------------------------------------------------------------*/
126
127 /*----------------------------------------------------------------------*/
128 #ifdef CONFIG_NEC_NL6648AC33
129 /*
130  *  NEC NL6648AC33-18. Active, color, single scan.
131  */
132 static vidinfo_t panel_info = {
133     640, 480, 132, 99, CFG_HIGH, CFG_HIGH, CFG_LOW, CFG_LOW, CFG_HIGH,
134     3, 0, 0, 1, 1, 144, 2, 0, 33
135                 /* wbl, vpw, lcdac, wbf */
136 };
137 #endif /* CONFIG_NEC_NL6648AC33 */
138 /*----------------------------------------------------------------------*/
139
140 #ifdef CONFIG_NEC_NL6648BC20
141 /*
142  *  NEC NL6648BC20-08. 6.5", 640x480. Active, color, single scan.
143  */
144 static vidinfo_t panel_info = {
145     640, 480, 132, 99, CFG_HIGH, CFG_HIGH, CFG_LOW, CFG_LOW, CFG_HIGH,
146     3, 0, 0, 1, 1, 144, 2, 0, 33
147                 /* wbl, vpw, lcdac, wbf */
148 };
149 #endif /* CONFIG_NEC_NL6648BC20 */
150 /*----------------------------------------------------------------------*/
151
152 #ifdef CONFIG_SHARP_LQ104V7DS01
153 /*
154  *  SHARP LQ104V7DS01. 6.5", 640x480. Active, color, single scan.
155  */
156 static vidinfo_t panel_info = {
157     640, 480, 132, 99, CFG_HIGH, CFG_HIGH, CFG_LOW, CFG_LOW, CFG_LOW,
158     3, 0, 0, 1, 1, 25, 1, 0, 33
159                 /* wbl, vpw, lcdac, wbf */
160 };
161 #endif /* CONFIG_SHARP_LQ104V7DS01 */
162 /*----------------------------------------------------------------------*/
163
164 #ifdef CONFIG_SHARP_16x9
165 /*
166  * Sharp 320x240. Active, color, single scan.  It isn't 16x9, and I am
167  * not sure what it is.......
168  */
169 static vidinfo_t panel_info = {
170     320, 240, 0, 0, CFG_HIGH, CFG_HIGH, CFG_HIGH, CFG_HIGH, CFG_HIGH,
171     3, 0, 0, 1, 1, 15, 4, 0, 3
172 };
173 #endif /* CONFIG_SHARP_16x9 */
174 /*----------------------------------------------------------------------*/
175
176 #ifdef CONFIG_SHARP_LQ057Q3DC02
177 /*
178  * Sharp LQ057Q3DC02 display. Active, color, single scan.
179  */
180 static vidinfo_t panel_info = {
181     320, 240, 0, 0, CFG_HIGH, CFG_HIGH, CFG_LOW, CFG_LOW, CFG_HIGH,
182     3, 0, 0, 1, 1, 15, 4, 0, 3
183                 /* wbl, vpw, lcdac, wbf */
184 };
185 #define LCD_INFO_BELOW_LOGO
186 #endif /* CONFIG_SHARP_LQ057Q3DC02 */
187 /*----------------------------------------------------------------------*/
188
189 #ifdef CONFIG_SHARP_LQ64D341
190 /*
191  * Sharp LQ64D341 display, 640x480. Active, color, single scan.
192  */
193 static vidinfo_t panel_info = {
194     640, 480, 0, 0, CFG_HIGH, CFG_HIGH, CFG_LOW, CFG_LOW, CFG_HIGH,
195     3, 0, 0, 1, 1, 128, 16, 0, 32
196                 /* wbl, vpw, lcdac, wbf */
197 };
198 #endif /* CONFIG_SHARP_LQ64D341 */
199
200 #ifdef CONFIG_SHARP_LQ084V1DG21
201 /*
202  * Sharp LQ084V1DG21 display, 640x480. Active, color, single scan.
203  */
204 static vidinfo_t panel_info = {
205     640, 480, 171, 129, CFG_HIGH, CFG_HIGH, CFG_LOW, CFG_LOW, CFG_LOW,
206     3, 0, 0, 1, 1, 160, 3, 0, 48
207                 /* wbl, vpw, lcdac, wbf */
208 };
209 #endif /* CONFIG_SHARP_LQ084V1DG21 */
210
211 /*----------------------------------------------------------------------*/
212
213 #ifdef CONFIG_HLD1045
214 /*
215  * HLD1045 display, 640x480. Active, color, single scan.
216  */
217 static vidinfo_t panel_info = {
218     640, 480, 0, 0, CFG_HIGH, CFG_HIGH, CFG_LOW, CFG_LOW, CFG_HIGH,
219     3, 0, 0, 1, 1, 160, 3, 0, 48
220                 /* wbl, vpw, lcdac, wbf */
221 };
222 #endif /* CONFIG_HLD1045 */
223 /*----------------------------------------------------------------------*/
224
225 #ifdef CONFIG_PRIMEVIEW_V16C6448AC
226 /*
227  * Prime View V16C6448AC
228  */
229 static vidinfo_t panel_info = {
230     640, 480, 130, 98, CFG_HIGH, CFG_HIGH, CFG_LOW, CFG_LOW, CFG_HIGH,
231     3, 0, 0, 1, 1, 144, 2, 0, 35
232                 /* wbl, vpw, lcdac, wbf */
233 };
234 #endif /* CONFIG_PRIMEVIEW_V16C6448AC */
235
236 /*----------------------------------------------------------------------*/
237
238 #ifdef CONFIG_OPTREX_BW
239 /*
240  * Optrex   CBL50840-2 NF-FW 99 22 M5
241  * or
242  * Hitachi  LMG6912RPFC-00T
243  * or
244  * Hitachi  SP14Q002
245  *
246  * 320x240. Black & white.
247  */
248 #define OPTREX_BPP      0       /* 0 - monochrome,     1 bpp */
249                                 /* 1 -  4 grey levels, 2 bpp */
250                                 /* 2 - 16 grey levels, 4 bpp */
251 static vidinfo_t panel_info = {
252     320, 240, 0, 0, CFG_HIGH, CFG_HIGH, CFG_HIGH, CFG_HIGH, CFG_LOW,
253     OPTREX_BPP, 0, 0, 0, 0, 0, 0, 0, 0, 4
254 };
255 #endif /* CONFIG_OPTREX_BW */
256
257 /*-----------------------------------------------------------------*/
258 #ifdef CONFIG_EDT32F10
259 /*
260  * Emerging Display Technologies 320x240. Passive, monochrome, single scan.
261  */
262 #define LCD_BPP         LCD_MONOCHROME
263 #define LCD_DF          20
264
265 static vidinfo_t panel_info = {
266     320, 240, 0, 0, CFG_HIGH, CFG_HIGH, CFG_HIGH, CFG_HIGH, CFG_LOW,
267     LCD_BPP,  0, 0, 0, 0, 0, 15, 0, 0
268 };
269 #endif
270 /*----------------------------------------------------------------------*/
271
272 #if defined(LCD_INFO_BELOW_LOGO)
273 # define LCD_INFO_X             0
274 # define LCD_INFO_Y             (BMP_LOGO_HEIGHT + VIDEO_FONT_HEIGHT)
275 #elif defined(CONFIG_LCD_LOGO)
276 # define LCD_INFO_X             (BMP_LOGO_WIDTH + 4 * VIDEO_FONT_WIDTH)
277 # define LCD_INFO_Y             (VIDEO_FONT_HEIGHT)
278 #else
279 # define LCD_INFO_X             (VIDEO_FONT_WIDTH)
280 # define LCD_INFO_Y             (VIDEO_FONT_HEIGHT)
281 #endif
282
283 #ifndef LCD_BPP
284 #define LCD_BPP                 LCD_COLOR8
285 #endif
286 #ifndef LCD_DF
287 #define LCD_DF                  1
288 #endif
289
290 #define NBITS(bit_code)         (1 << (bit_code))
291 #define NCOLORS(bit_code)       (1 << NBITS(bit_code))
292
293 static int lcd_line_length;
294
295 static int lcd_color_fg;
296 static int lcd_color_bg;
297
298 static char lcd_is_enabled = 0;         /* Indicate that LCD is enabled */
299
300 /*
301  * Frame buffer memory information
302  */
303 static void *lcd_base;                  /* Start of framebuffer memory  */
304 static void *lcd_console_address;       /* Start of console buffer      */
305
306
307 /************************************************************************/
308 /* ** CONSOLE CONSTANTS                                                 */
309 /************************************************************************/
310
311 #if LCD_BPP == LCD_MONOCHROME
312
313 /*
314  * Simple color definitions
315  */
316 #define CONSOLE_COLOR_BLACK      0
317 #define CONSOLE_COLOR_WHITE      1      /* Must remain last / highest */
318
319 #else
320
321 /*
322  * Simple color definitions
323  */
324 #define CONSOLE_COLOR_BLACK      0
325 #define CONSOLE_COLOR_RED        1
326 #define CONSOLE_COLOR_GREEN      2
327 #define CONSOLE_COLOR_YELLOW     3
328 #define CONSOLE_COLOR_BLUE       4
329 #define CONSOLE_COLOR_MAGENTA    5
330 #define CONSOLE_COLOR_CYAN       6
331 #define CONSOLE_COLOR_GREY      14
332 #define CONSOLE_COLOR_WHITE     15      /* Must remain last / highest */
333
334 #endif
335
336 #if defined(CONFIG_LCD_LOGO) && (CONSOLE_COLOR_WHITE >= BMP_LOGO_OFFSET)
337 #error Default Color Map overlaps with Logo Color Map
338 #endif
339
340 /************************************************************************/
341
342 #ifndef PAGE_SIZE
343 #define PAGE_SIZE       4096
344 #endif
345
346
347 /************************************************************************/
348 /* ** CONSOLE DEFINITIONS & FUNCTIONS                                   */
349 /************************************************************************/
350
351 #if defined(CONFIG_LCD_LOGO) && !defined(LCD_INFO_BELOW_LOGO)
352 #define CONSOLE_ROWS            ((panel_info.vl_row-BMP_LOGO_HEIGHT) \
353                                         / VIDEO_FONT_HEIGHT)
354 #else
355 #define CONSOLE_ROWS            (panel_info.vl_row / VIDEO_FONT_HEIGHT)
356 #endif
357 #define CONSOLE_COLS            (panel_info.vl_col / VIDEO_FONT_WIDTH)
358 #define CONSOLE_ROW_SIZE        (VIDEO_FONT_HEIGHT * lcd_line_length)
359 #define CONSOLE_ROW_FIRST       (lcd_console_address)
360 #define CONSOLE_ROW_SECOND      (lcd_console_address + CONSOLE_ROW_SIZE)
361 #define CONSOLE_ROW_LAST        (lcd_console_address + CONSOLE_SIZE \
362                                         - CONSOLE_ROW_SIZE)
363 #define CONSOLE_SIZE            (CONSOLE_ROW_SIZE * CONSOLE_ROWS)
364 #define CONSOLE_SCROLL_SIZE     (CONSOLE_SIZE - CONSOLE_ROW_SIZE)
365
366 #if  LCD_BPP == LCD_MONOCHROME
367 #define COLOR_MASK(c)           ((c)      | (c) << 1 | (c) << 2 | (c) << 3 | \
368                                  (c) << 4 | (c) << 5 | (c) << 6 | (c) << 7)
369 #elif LCD_BPP == LCD_COLOR8
370 #define COLOR_MASK(c)           (c)
371 #else
372 #error Unsupported LCD BPP.
373 #endif
374
375 static short console_col;
376 static short console_row;
377
378 /************************************************************************/
379
380 ulong   lcd_setmem (ulong addr);
381
382 static void     lcd_drawchars  (ushort x, ushort y, uchar *str, int count);
383 static inline void lcd_puts_xy (ushort x, ushort y, uchar *s);
384 static inline void lcd_putc_xy (ushort x, ushort y, uchar  c);
385
386 static int      lcd_init (void *lcdbase);
387 static void     lcd_ctrl_init (void *lcdbase);
388 static void     lcd_enable (void);
389 static void    *lcd_logo (void);
390 #if LCD_BPP == LCD_COLOR8
391 static void     lcd_setcolreg (ushort regno,
392                                 ushort red, ushort green, ushort blue);
393 #endif
394 #if LCD_BPP == LCD_MONOCHROME
395 static void     lcd_initcolregs (void);
396 #endif
397 static int      lcd_getbgcolor (void);
398 static void     lcd_setfgcolor (int color);
399 static void     lcd_setbgcolor (int color);
400
401 #ifdef  NOT_USED_SO_FAR
402 static void     lcd_disable (void);
403 static void     lcd_getcolreg (ushort regno,
404                                 ushort *red, ushort *green, ushort *blue);
405 static int      lcd_getfgcolor (void);
406 #endif  /* NOT_USED_SO_FAR */
407
408 /************************************************************************/
409
410 /*----------------------------------------------------------------------*/
411
412 static void console_scrollup (void)
413 {
414 #if 1
415         /* Copy up rows ignoring the first one */
416         memcpy (CONSOLE_ROW_FIRST, CONSOLE_ROW_SECOND, CONSOLE_SCROLL_SIZE);
417
418         /* Clear the last one */
419         memset (CONSOLE_ROW_LAST, COLOR_MASK(lcd_color_bg), CONSOLE_ROW_SIZE);
420 #else
421         /*
422          * Poor attempt to optimize speed by moving "long"s.
423          * But the code is ugly, and not a bit faster :-(
424          */
425         ulong *t = (ulong *)CONSOLE_ROW_FIRST;
426         ulong *s = (ulong *)CONSOLE_ROW_SECOND;
427         ulong    l = CONSOLE_SCROLL_SIZE / sizeof(ulong);
428         uchar  c = lcd_color_bg & 0xFF;
429         ulong val= (c<<24) | (c<<16) | (c<<8) | c;
430
431         while (l--)
432                 *t++ = *s++;
433
434         t = (ulong *)CONSOLE_ROW_LAST;
435         l = CONSOLE_ROW_SIZE / sizeof(ulong);
436
437         while (l-- > 0)
438                 *t++ = val;
439 #endif
440 }
441
442 /*----------------------------------------------------------------------*/
443
444 static inline void console_back (void)
445 {
446         if (--console_col < 0) {
447                 console_col = CONSOLE_COLS-1 ;
448                 if (--console_row < 0) {
449                         console_row = 0;
450                 }
451         }
452
453         lcd_putc_xy (console_col * VIDEO_FONT_WIDTH,
454                      console_row * VIDEO_FONT_HEIGHT,
455                      ' ');
456 }
457
458 /*----------------------------------------------------------------------*/
459
460 static inline void console_newline (void)
461 {
462         ++console_row;
463         console_col = 0;
464
465         /* Check if we need to scroll the terminal */
466         if (console_row >= CONSOLE_ROWS) {
467                 /* Scroll everything up */
468                 console_scrollup () ;
469                 --console_row;
470         }
471 }
472
473 /*----------------------------------------------------------------------*/
474
475 void lcd_putc (const char c)
476 {
477         if (!lcd_is_enabled) {
478                 serial_putc(c);
479                 return;
480         }
481
482         switch (c) {
483         case '\r':      console_col = 0;
484                         return;
485
486         case '\n':      console_newline();
487                         return;
488
489         case '\t':      /* Tab (8 chars alignment) */
490                         console_col |=  8;
491                         console_col &= ~7;
492
493                         if (console_col >= CONSOLE_COLS) {
494                                 console_newline();
495                         }
496                         return;
497
498         case '\b':      console_back();
499                         return;
500
501         default:        lcd_putc_xy (console_col * VIDEO_FONT_WIDTH,
502                                      console_row * VIDEO_FONT_HEIGHT,
503                                      c);
504                         if (++console_col >= CONSOLE_COLS) {
505                                 console_newline();
506                         }
507                         return;
508         }
509         /* NOTREACHED */
510 }
511
512 /*----------------------------------------------------------------------*/
513
514 void lcd_puts (const char *s)
515 {
516         if (!lcd_is_enabled) {
517                 serial_puts (s);
518                 return;
519         }
520
521         while (*s) {
522                 lcd_putc (*s++);
523         }
524 }
525
526 /************************************************************************/
527 /* ** Low-Level Graphics Routines                                       */
528 /************************************************************************/
529
530 static void lcd_drawchars (ushort x, ushort y, uchar *str, int count)
531 {
532         uchar *dest;
533         ushort off, row;
534
535         dest = (uchar *)(lcd_base + y * lcd_line_length + x * (1 << LCD_BPP) / 8);
536         off  = x * (1 << LCD_BPP) % 8;
537
538         for (row=0;  row < VIDEO_FONT_HEIGHT;  ++row, dest += lcd_line_length)  {
539                 uchar *s = str;
540                 uchar *d = dest;
541                 int i;
542
543 #if LCD_BPP == LCD_MONOCHROME
544                 uchar rest = *d & -(1 << (8-off));
545                 uchar sym;
546 #endif
547                 for (i=0; i<count; ++i) {
548                         uchar c, bits;
549
550                         c = *s++;
551                         bits = video_fontdata[c * VIDEO_FONT_HEIGHT + row];
552
553 #if LCD_BPP == LCD_MONOCHROME
554                         sym  = (COLOR_MASK(lcd_color_fg) & bits) |
555                                (COLOR_MASK(lcd_color_bg) & ~bits);
556
557                         *d++ = rest | (sym >> off);
558                         rest = sym << (8-off);
559 #elif LCD_BPP == LCD_COLOR8
560                         for (c=0; c<8; ++c) {
561                                 *d++ = (bits & 0x80) ?
562                                                 lcd_color_fg : lcd_color_bg;
563                                 bits <<= 1;
564                         }
565 #endif
566                 }
567
568 #if LCD_BPP == LCD_MONOCHROME
569                 *d  = rest | (*d & ((1 << (8-off)) - 1));
570 #endif
571         }
572 }
573
574 /*----------------------------------------------------------------------*/
575
576 static inline void lcd_puts_xy (ushort x, ushort y, uchar *s)
577 {
578 #if defined(CONFIG_LCD_LOGO) && !defined(LCD_INFO_BELOW_LOGO)
579         lcd_drawchars (x, y+BMP_LOGO_HEIGHT, s, strlen (s));
580 #else
581         lcd_drawchars (x, y, s, strlen (s));
582 #endif
583 }
584
585 /*----------------------------------------------------------------------*/
586
587 static inline void lcd_putc_xy (ushort x, ushort y, uchar c)
588 {
589 #if defined(CONFIG_LCD_LOGO) && !defined(LCD_INFO_BELOW_LOGO)
590         lcd_drawchars (x, y+BMP_LOGO_HEIGHT, &c, 1);
591 #else
592         lcd_drawchars (x, y, &c, 1);
593 #endif
594 }
595
596 /************************************************************************/
597 /**  Small utility to check that you got the colours right              */
598 /************************************************************************/
599 #ifdef LCD_TEST_PATTERN
600
601 #define N_BLK_VERT      2
602 #define N_BLK_HOR       3
603
604 static int test_colors[N_BLK_HOR*N_BLK_VERT] = {
605         CONSOLE_COLOR_RED,      CONSOLE_COLOR_GREEN,    CONSOLE_COLOR_YELLOW,
606         CONSOLE_COLOR_BLUE,     CONSOLE_COLOR_MAGENTA,  CONSOLE_COLOR_CYAN,
607 };
608
609 static void test_pattern (void)
610 {
611         ushort v_max  = panel_info.vl_row;
612         ushort h_max  = panel_info.vl_col;
613         ushort v_step = (v_max + N_BLK_VERT - 1) / N_BLK_VERT;
614         ushort h_step = (h_max + N_BLK_HOR  - 1) / N_BLK_HOR;
615         ushort v, h;
616         uchar *pix = (uchar *)lcd_base;
617
618         printf ("[LCD] Test Pattern: %d x %d [%d x %d]\n",
619                 h_max, v_max, h_step, v_step);
620
621         /* WARNING: Code silently assumes 8bit/pixel */
622         for (v=0; v<v_max; ++v) {
623                 uchar iy = v / v_step;
624                 for (h=0; h<h_max; ++h) {
625                         uchar ix = N_BLK_HOR * iy + (h/h_step);
626                         *pix++ = test_colors[ix];
627                 }
628         }
629 }
630 #endif /* LCD_TEST_PATTERN */
631
632
633 /************************************************************************/
634 /* ** GENERIC Initialization Routines                                   */
635 /************************************************************************/
636
637 int drv_lcd_init (void)
638 {
639         DECLARE_GLOBAL_DATA_PTR;
640
641         device_t lcddev;
642         int rc;
643
644         lcd_base = (void *)(gd->fb_base);
645
646         lcd_line_length = (panel_info.vl_col * NBITS (panel_info.vl_bpix)) / 8;
647
648         lcd_init (lcd_base);            /* LCD initialization */
649
650         /* Device initialization */
651         memset (&lcddev, 0, sizeof (lcddev));
652
653         strcpy (lcddev.name, "lcd");
654         lcddev.ext   = 0;                       /* No extensions */
655         lcddev.flags = DEV_FLAGS_OUTPUT;        /* Output only */
656         lcddev.putc  = lcd_putc;                /* 'putc' function */
657         lcddev.puts  = lcd_puts;                /* 'puts' function */
658
659         rc = device_register (&lcddev);
660
661         return (rc == 0) ? 1 : rc;
662 }
663
664 /*----------------------------------------------------------------------*/
665
666 static int lcd_init (void *lcdbase)
667 {
668         /* Initialize the lcd controller */
669         debug ("[LCD] Initializing LCD frambuffer at %p\n", lcdbase);
670
671         lcd_ctrl_init (lcdbase);
672
673 #if LCD_BPP == LCD_MONOCHROME
674         /* Setting the palette */
675         lcd_initcolregs();
676
677 #elif LCD_BPP == LCD_COLOR8
678         /* Setting the palette */
679         lcd_setcolreg  (CONSOLE_COLOR_BLACK,       0,    0,    0);
680         lcd_setcolreg  (CONSOLE_COLOR_RED,      0xFF,    0,    0);
681         lcd_setcolreg  (CONSOLE_COLOR_GREEN,       0, 0xFF,    0);
682         lcd_setcolreg  (CONSOLE_COLOR_YELLOW,   0xFF, 0xFF,    0);
683         lcd_setcolreg  (CONSOLE_COLOR_BLUE,        0,    0, 0xFF);
684         lcd_setcolreg  (CONSOLE_COLOR_MAGENTA,  0xFF,    0, 0xFF);
685         lcd_setcolreg  (CONSOLE_COLOR_CYAN,        0, 0xFF, 0xFF);
686         lcd_setcolreg  (CONSOLE_COLOR_GREY,     0xAA, 0xAA, 0xAA);
687         lcd_setcolreg  (CONSOLE_COLOR_WHITE,    0xFF, 0xFF, 0xFF);
688 #endif
689
690 #ifndef CFG_WHITE_ON_BLACK
691         lcd_setfgcolor (CONSOLE_COLOR_BLACK);
692         lcd_setbgcolor (CONSOLE_COLOR_WHITE);
693 #else
694         lcd_setfgcolor (CONSOLE_COLOR_WHITE);
695         lcd_setbgcolor (CONSOLE_COLOR_BLACK);
696 #endif  /* CFG_WHITE_ON_BLACK */
697
698 #ifdef  LCD_TEST_PATTERN
699         test_pattern();
700 #else
701         /* set framebuffer to background color */
702         memset ((char *)lcd_base,
703                 COLOR_MASK(lcd_getbgcolor()),
704                 lcd_line_length*panel_info.vl_row);
705 #endif
706
707         lcd_enable ();
708
709         /* Paint the logo and retrieve LCD base address */
710         debug ("[LCD] Drawing the logo...\n");
711         lcd_console_address = lcd_logo ();
712
713         /* Initialize the console */
714         console_col = 0;
715 #ifdef LCD_INFO_BELOW_LOGO
716         console_row = 7 + BMP_LOGO_HEIGHT / VIDEO_FONT_HEIGHT;
717 #else
718         console_row = 1;        /* leave 1 blank line below logo */
719 #endif
720         lcd_is_enabled = 1;
721
722         return 0;
723 }
724
725
726 /************************************************************************/
727 /* ** ROM capable initialization part - needed to reserve FB memory     */
728 /************************************************************************/
729
730 /*
731  * This is called early in the system initialization to grab memory
732  * for the LCD controller.
733  * Returns new address for monitor, after reserving LCD buffer memory
734  *
735  * Note that this is running from ROM, so no write access to global data.
736  */
737 ulong lcd_setmem (ulong addr)
738 {
739         ulong size;
740         int line_length = (panel_info.vl_col * NBITS (panel_info.vl_bpix)) / 8;
741
742         debug ("LCD panel info: %d x %d, %d bit/pix\n",
743                 panel_info.vl_col, panel_info.vl_row, NBITS (panel_info.vl_bpix) );
744
745         size = line_length * panel_info.vl_row;
746
747         /* Round up to nearest full page */
748         size = (size + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
749
750         /* Allocate pages for the frame buffer. */
751         addr -= size;
752
753         debug ("Reserving %ldk for LCD Framebuffer at: %08lx\n", size>>10, addr);
754
755         return (addr);
756 }
757
758
759 /************************************************************************/
760 /* ----------------- chipset specific functions ----------------------- */
761 /************************************************************************/
762
763 static void lcd_ctrl_init (void *lcdbase)
764 {
765         volatile immap_t *immr = (immap_t *) CFG_IMMR;
766         volatile lcd823_t *lcdp = &immr->im_lcd;
767
768         uint lccrtmp;
769
770         /* Initialize the LCD control register according to the LCD
771          * parameters defined.  We do everything here but enable
772          * the controller.
773          */
774
775         lccrtmp  = LCDBIT (LCCR_BNUM_BIT,
776                    (((panel_info.vl_row * panel_info.vl_col) * (1 << LCD_BPP)) / 128));
777
778         lccrtmp |= LCDBIT (LCCR_CLKP_BIT, panel_info.vl_clkp)   |
779                    LCDBIT (LCCR_OEP_BIT,  panel_info.vl_oep)    |
780                    LCDBIT (LCCR_HSP_BIT,  panel_info.vl_hsp)    |
781                    LCDBIT (LCCR_VSP_BIT,  panel_info.vl_vsp)    |
782                    LCDBIT (LCCR_DP_BIT,   panel_info.vl_dp)     |
783                    LCDBIT (LCCR_BPIX_BIT, panel_info.vl_bpix)   |
784                    LCDBIT (LCCR_LBW_BIT,  panel_info.vl_lbw)    |
785                    LCDBIT (LCCR_SPLT_BIT, panel_info.vl_splt)   |
786                    LCDBIT (LCCR_CLOR_BIT, panel_info.vl_clor)   |
787                    LCDBIT (LCCR_TFT_BIT,  panel_info.vl_tft);
788
789 #if 0
790         lccrtmp |= ((SIU_LEVEL5 / 2) << 12);
791         lccrtmp |= LCCR_EIEN;
792 #endif
793
794         lcdp->lcd_lccr = lccrtmp;
795         lcdp->lcd_lcsr = 0xFF;          /* Clear pending interrupts */
796
797         /* Initialize LCD controller bus priorities.
798          */
799         immr->im_siu_conf.sc_sdcr &= ~0x0f;     /* RAID = LAID = 0 */
800
801         /* set SHFT/CLOCK division factor 4
802          * This needs to be set based upon display type and processor
803          * speed.  The TFT displays run about 20 to 30 MHz.
804          * I was running 64 MHz processor speed.
805          * The value for this divider must be chosen so the result is
806          * an integer of the processor speed (i.e., divide by 3 with
807          * 64 MHz would be bad).
808          */
809         immr->im_clkrst.car_sccr &= ~0x1F;
810         immr->im_clkrst.car_sccr |= LCD_DF;     /* was 8 */
811
812 #ifndef CONFIG_EDT32F10
813         /* Enable LCD on port D.
814          */
815         immr->im_ioport.iop_pdpar |= 0x1FFF;
816         immr->im_ioport.iop_pddir |= 0x1FFF;
817
818         /* Enable LCD_A/B/C on port B.
819          */
820         immr->im_cpm.cp_pbpar |= 0x00005001;
821         immr->im_cpm.cp_pbdir |= 0x00005001;
822 #else
823         /* Enable LCD on port D.
824          */
825         immr->im_ioport.iop_pdpar |= 0x1DFF;
826         immr->im_ioport.iop_pdpar &= ~0x0200;
827         immr->im_ioport.iop_pddir |= 0x1FFF;
828         immr->im_ioport.iop_pddat |= 0x0200;
829 #endif
830
831         /* Load the physical address of the linear frame buffer
832          * into the LCD controller.
833          * BIG NOTE:  This has to be modified to load A and B depending
834          * upon the split mode of the LCD.
835          */
836         lcdp->lcd_lcfaa = (ulong)lcd_base;
837         lcdp->lcd_lcfba = (ulong)lcd_base;
838
839         /* MORE HACKS...This must be updated according to 823 manual
840          * for different panels.
841          */
842 #ifndef CONFIG_EDT32F10
843         lcdp->lcd_lchcr = LCHCR_BO |
844                           LCDBIT (LCHCR_AT_BIT, 4) |
845                           LCDBIT (LCHCR_HPC_BIT, panel_info.vl_col) |
846                           panel_info.vl_wbl;
847 #else
848         lcdp->lcd_lchcr = LCHCR_BO |
849                           LCDBIT (LCHCR_AT_BIT, 4) |
850                           LCDBIT (LCHCR_HPC_BIT, panel_info.vl_col/4) |
851                           panel_info.vl_wbl;
852 #endif
853
854         lcdp->lcd_lcvcr = LCDBIT (LCVCR_VPW_BIT, panel_info.vl_vpw) |
855                           LCDBIT (LCVCR_LCD_AC_BIT, panel_info.vl_lcdac) |
856                           LCDBIT (LCVCR_VPC_BIT, panel_info.vl_row) |
857                           panel_info.vl_wbf;
858
859 }
860
861 /*----------------------------------------------------------------------*/
862
863 #ifdef  NOT_USED_SO_FAR
864 static void
865 lcd_getcolreg (ushort regno, ushort *red, ushort *green, ushort *blue)
866 {
867         volatile immap_t *immr = (immap_t *) CFG_IMMR;
868         volatile cpm8xx_t *cp = &(immr->im_cpm);
869         unsigned short colreg, *cmap_ptr;
870
871         cmap_ptr = (unsigned short *)&cp->lcd_cmap[regno * 2];
872
873         colreg = *cmap_ptr;
874 #ifdef  CFG_INVERT_COLORS
875         colreg ^= 0x0FFF;
876 #endif
877
878         *red   = (colreg >> 8) & 0x0F;
879         *green = (colreg >> 4) & 0x0F;
880         *blue  =  colreg       & 0x0F;
881 }
882 #endif  /* NOT_USED_SO_FAR */
883
884 /*----------------------------------------------------------------------*/
885
886 #if LCD_BPP == LCD_COLOR8
887 static void
888 lcd_setcolreg (ushort regno, ushort red, ushort green, ushort blue)
889 {
890         volatile immap_t *immr = (immap_t *) CFG_IMMR;
891         volatile cpm8xx_t *cp = &(immr->im_cpm);
892         unsigned short colreg, *cmap_ptr;
893
894         cmap_ptr = (unsigned short *)&cp->lcd_cmap[regno * 2];
895
896         colreg = ((red   & 0x0F) << 8) |
897                  ((green & 0x0F) << 4) |
898                   (blue  & 0x0F) ;
899 #ifdef  CFG_INVERT_COLORS
900         colreg ^= 0x0FFF;
901 #endif
902         *cmap_ptr = colreg;
903
904         debug ("setcolreg: reg %2d @ %p: R=%02X G=%02X B=%02X => %02X%02X\n",
905                 regno, &(cp->lcd_cmap[regno * 2]),
906                 red, green, blue,
907                 cp->lcd_cmap[ regno * 2 ], cp->lcd_cmap[(regno * 2) + 1]);
908 }
909 #endif  /* LCD_COLOR8 */
910
911 /*----------------------------------------------------------------------*/
912
913 #if LCD_BPP == LCD_MONOCHROME
914 static
915 void lcd_initcolregs (void)
916 {
917         volatile immap_t *immr = (immap_t *) CFG_IMMR;
918         volatile cpm8xx_t *cp = &(immr->im_cpm);
919         ushort regno;
920
921         for (regno = 0; regno < 16; regno++) {
922                 cp->lcd_cmap[regno * 2] = 0;
923                 cp->lcd_cmap[(regno * 2) + 1] = regno & 0x0f;
924         }
925 }
926 #endif
927
928 /*----------------------------------------------------------------------*/
929
930 static void lcd_setfgcolor (int color)
931 {
932         lcd_color_fg = color & 0x0F;
933 }
934
935 /*----------------------------------------------------------------------*/
936
937 static void lcd_setbgcolor (int color)
938 {
939         lcd_color_bg = color & 0x0F;
940 }
941
942 /*----------------------------------------------------------------------*/
943
944 #ifdef  NOT_USED_SO_FAR
945 static int lcd_getfgcolor (void)
946 {
947         return lcd_color_fg;
948 }
949 #endif  /* NOT_USED_SO_FAR */
950
951 /*----------------------------------------------------------------------*/
952
953 static int lcd_getbgcolor (void)
954 {
955         return lcd_color_bg;
956 }
957
958 /*----------------------------------------------------------------------*/
959
960 static void lcd_enable (void)
961 {
962         volatile immap_t *immr = (immap_t *) CFG_IMMR;
963         volatile lcd823_t *lcdp = &immr->im_lcd;
964
965         /* Enable the LCD panel */
966         immr->im_siu_conf.sc_sdcr |= (1 << (31 - 25));          /* LAM = 1 */
967         lcdp->lcd_lccr |= LCCR_PON;
968
969 #ifdef CONFIG_V37
970         /* Turn on display backlight */
971         immr->im_cpm.cp_pbpar |= 0x00008000;
972         immr->im_cpm.cp_pbdir |= 0x00008000;
973 #endif
974
975 #if defined(CONFIG_LWMON)
976     {   uchar c = pic_read (0x60);
977         c |= 0x07;      /* Power on CCFL, Enable CCFL, Chip Enable LCD */
978         pic_write (0x60, c);
979     }
980 #elif defined(CONFIG_R360MPI)
981     {
982         extern void r360_pwm_write (uchar reg, uchar val);
983
984         r360_pwm_write(8, 1);
985         r360_pwm_write(0, 4);
986         r360_pwm_write(1, 6);
987     }
988 #endif /* CONFIG_LWMON */
989 }
990
991 /*----------------------------------------------------------------------*/
992
993 #ifdef  NOT_USED_SO_FAR
994 static void lcd_disable (void)
995 {
996         volatile immap_t *immr = (immap_t *) CFG_IMMR;
997         volatile lcd823_t *lcdp = &immr->im_lcd;
998
999 #if defined(CONFIG_LWMON)
1000     {   uchar c = pic_read (0x60);
1001         c &= ~0x07;     /* Power off CCFL, Disable CCFL, Chip Disable LCD */
1002         pic_write (0x60, c);
1003     }
1004 #elif defined(CONFIG_R360MPI)
1005     {
1006         extern void r360_pwm_write (uchar reg, uchar val);
1007
1008         r360_pwm_write(0, 0);
1009         r360_pwm_write(1, 0);
1010     }
1011 #endif /* CONFIG_LWMON */
1012         /* Disable the LCD panel */
1013         lcdp->lcd_lccr &= ~LCCR_PON;
1014         immr->im_siu_conf.sc_sdcr &= ~(1 << (31 - 25)); /* LAM = 0 */
1015 }
1016 #endif  /* NOT_USED_SO_FAR */
1017
1018
1019 /************************************************************************/
1020 /* ** Chipset depending Bitmap / Logo stuff...                          */
1021 /************************************************************************/
1022
1023
1024 #ifdef CONFIG_LCD_LOGO
1025 static void bitmap_plot (int x, int y)
1026 {
1027         volatile immap_t *immr = (immap_t *) CFG_IMMR;
1028         volatile cpm8xx_t *cp = &(immr->im_cpm);
1029         ushort *cmap;
1030         ushort i;
1031         uchar *bmap;
1032         uchar *fb;
1033
1034         debug ("Logo: width %d  height %d  colors %d  cmap %d\n",
1035                 BMP_LOGO_WIDTH, BMP_LOGO_HEIGHT, BMP_LOGO_COLORS,
1036                 sizeof(bmp_logo_palette)/(sizeof(ushort))
1037         );
1038
1039         /* Leave room for default color map */
1040         cmap = (ushort *)&(cp->lcd_cmap[BMP_LOGO_OFFSET*sizeof(ushort)]);
1041
1042         /* Set color map */
1043         for (i=0; i<(sizeof(bmp_logo_palette)/(sizeof(ushort))); ++i) {
1044                 ushort colreg = bmp_logo_palette[i];
1045 #ifdef  CFG_INVERT_COLORS
1046                 colreg ^= 0xFFF;
1047 #endif
1048                 *cmap++ = colreg;
1049         }
1050
1051         bmap = &bmp_logo_bitmap[0];
1052         fb   = (char *)(lcd_base + y * lcd_line_length + x);
1053
1054         for (i=0; i<BMP_LOGO_HEIGHT; ++i) {
1055                 memcpy (fb, bmap, BMP_LOGO_WIDTH);
1056                 bmap += BMP_LOGO_WIDTH;
1057                 fb   += panel_info.vl_col;
1058         }
1059 }
1060 #endif /* CONFIG_LCD_LOGO */
1061
1062 /*----------------------------------------------------------------------*/
1063
1064 static void *lcd_logo (void)
1065 {
1066 #ifdef LCD_INFO
1067         DECLARE_GLOBAL_DATA_PTR;
1068
1069         char info[80];
1070         char temp[32];
1071 #endif /* LCD_INFO */
1072
1073 #ifdef CONFIG_LCD_LOGO
1074         bitmap_plot (0, 0);
1075 #endif /* CONFIG_LCD_LOGO */
1076
1077
1078 #ifdef LCD_INFO
1079         sprintf (info, "%s (%s - %s) ", U_BOOT_VERSION, __DATE__, __TIME__);
1080         lcd_drawchars (LCD_INFO_X, LCD_INFO_Y, info, strlen(info));
1081
1082         sprintf (info, "(C) 2002 DENX Software Engineering");
1083         lcd_drawchars (LCD_INFO_X, LCD_INFO_Y + VIDEO_FONT_HEIGHT,
1084                                         info, strlen(info));
1085
1086         sprintf (info, "    Wolfgang DENK, wd@denx.de");
1087         lcd_drawchars (LCD_INFO_X, LCD_INFO_Y + VIDEO_FONT_HEIGHT * 2,
1088                                         info, strlen(info));
1089 #ifdef LCD_INFO_BELOW_LOGO
1090         sprintf (info, "MPC823 CPU at %s MHz",
1091                 strmhz(temp, gd->cpu_clk));
1092         lcd_drawchars (LCD_INFO_X, LCD_INFO_Y + VIDEO_FONT_HEIGHT * 3,
1093                                         info, strlen(info));
1094         sprintf (info, "  %ld MB RAM, %ld MB Flash",
1095                 gd->ram_size >> 20,
1096                 gd->bd->bi_flashsize >> 20 );
1097         lcd_drawchars (LCD_INFO_X, LCD_INFO_Y + VIDEO_FONT_HEIGHT * 4,
1098                                         info, strlen(info));
1099 #else
1100         /* leave one blank line */
1101
1102         sprintf (info, "MPC823 CPU at %s MHz, %ld MB RAM, %ld MB Flash",
1103                 strmhz(temp, gd->cpu_clk),
1104                 gd->ram_size >> 20,
1105                 gd->bd->bi_flashsize >> 20 );
1106         lcd_drawchars (LCD_INFO_X, LCD_INFO_Y + VIDEO_FONT_HEIGHT * 4,
1107                                         info, strlen(info));
1108 #endif /* LCD_INFO_BELOW_LOGO */
1109 #endif /* LCD_INFO */
1110
1111 #if defined(CONFIG_LCD_LOGO) && !defined(LCD_INFO_BELOW_LOGO)
1112         return ((void *)((ulong)lcd_base + BMP_LOGO_HEIGHT * lcd_line_length));
1113 #else
1114         return ((void *)lcd_base);
1115 #endif  /* CONFIG_LCD_LOGO */
1116 }
1117
1118 /************************************************************************/
1119 /************************************************************************/
1120
1121 #endif /* CONFIG_LCD */