Import upstream u-boot 1.1.4
[u-boot.git] / board / esd / common / lcd.c
1 /*
2  * (C) Copyright 2003-2004
3  * Stefan Roese, esd gmbh germany, stefan.roese@esd-electronics.com
4  *
5  * (C) Copyright 2005
6  * Stefan Roese, DENX Software Engineering, sr@denx.de.
7  *
8  * See file CREDITS for list of people who contributed to this
9  * project.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License as
13  * published by the Free Software Foundation; either version 2 of
14  * the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24  * MA 02111-1307 USA
25  */
26
27 #include "lcd.h"
28
29
30 extern int video_display_bitmap (ulong, int, int);
31
32
33 int palette_index;
34 int palette_value;
35 int lcd_depth;
36 unsigned char *glob_lcd_reg;
37 unsigned char *glob_lcd_mem;
38
39 #ifdef CFG_LCD_ENDIAN
40 void lcd_setup(int lcd, int config)
41 {
42         if (lcd == 0) {
43                 /*
44                  * Set endianess and reset lcd controller 0 (small)
45                  */
46                 out32(GPIO0_OR, in32(GPIO0_OR) & ~CFG_LCD0_RST); /* set reset to low */
47                 udelay(10); /* wait 10us */
48                 if (config == 1) {
49                         out32(GPIO0_OR, in32(GPIO0_OR) | CFG_LCD_ENDIAN); /* big-endian */
50                 } else {
51                         out32(GPIO0_OR, in32(GPIO0_OR) & ~CFG_LCD_ENDIAN); /* little-endian */
52                 }
53                 udelay(10); /* wait 10us */
54                 out32(GPIO0_OR, in32(GPIO0_OR) | CFG_LCD0_RST); /* set reset to high */
55         } else {
56                 /*
57                  * Set endianess and reset lcd controller 1 (big)
58                  */
59                 out32(GPIO0_OR, in32(GPIO0_OR) & ~CFG_LCD1_RST); /* set reset to low */
60                 udelay(10); /* wait 10us */
61                 if (config == 1) {
62                         out32(GPIO0_OR, in32(GPIO0_OR) | CFG_LCD_ENDIAN); /* big-endian */
63                 } else {
64                         out32(GPIO0_OR, in32(GPIO0_OR) & ~CFG_LCD_ENDIAN); /* little-endian */
65                 }
66                 udelay(10); /* wait 10us */
67                 out32(GPIO0_OR, in32(GPIO0_OR) | CFG_LCD1_RST); /* set reset to high */
68         }
69
70         /*
71          * CFG_LCD_ENDIAN may also be FPGA_RESET, so set inactive
72          */
73         out32(GPIO0_OR, in32(GPIO0_OR) | CFG_LCD_ENDIAN); /* set reset high again */
74 }
75 #endif /* #ifdef CFG_LCD_ENDIAN */
76
77
78 void lcd_bmp(uchar *logo_bmp)
79 {
80         int i;
81         uchar *ptr;
82         ushort *ptr2;
83         ushort val;
84         unsigned char *dst = NULL;
85         int x, y;
86         int width, height, bpp, colors, line_size;
87         int header_size;
88         unsigned char *bmp;
89         unsigned char r, g, b;
90         BITMAPINFOHEADER *bm_info;
91         ulong len;
92
93         /*
94          * Check for bmp mark 'BM'
95          */
96         if (*(ushort *)logo_bmp != 0x424d) {
97
98                 /*
99                  * Decompress bmp image
100                  */
101                 len = CFG_VIDEO_LOGO_MAX_SIZE;
102                 dst = malloc(CFG_VIDEO_LOGO_MAX_SIZE);
103                 if (dst == NULL) {
104                         printf("Error: malloc in gunzip failed!\n");
105                         return;
106                 }
107                 if (gunzip(dst, CFG_VIDEO_LOGO_MAX_SIZE, (uchar *)logo_bmp, &len) != 0) {
108                         return;
109                 }
110                 if (len == CFG_VIDEO_LOGO_MAX_SIZE) {
111                         printf("Image could be truncated (increase CFG_VIDEO_LOGO_MAX_SIZE)!\n");
112                 }
113
114                 /*
115                  * Check for bmp mark 'BM'
116                  */
117                 if (*(ushort *)dst != 0x424d) {
118                         printf("LCD: Unknown image format!\n");
119                         free(dst);
120                         return;
121                 }
122         } else {
123                 /*
124                  * Uncompressed BMP image, just use this pointer
125                  */
126                 dst = (uchar *)logo_bmp;
127         }
128
129         /*
130          * Get image info from bmp-header
131          */
132         bm_info = (BITMAPINFOHEADER *)(dst + 14);
133         bpp = LOAD_SHORT(bm_info->biBitCount);
134         width = LOAD_LONG(bm_info->biWidth);
135         height = LOAD_LONG(bm_info->biHeight);
136         switch (bpp) {
137         case 1:
138                 colors = 1;
139                 line_size = width >> 3;
140                 break;
141         case 4:
142                 colors = 16;
143                 line_size = width >> 1;
144                 break;
145         case 8:
146                 colors = 256;
147                 line_size = width;
148                 break;
149         case 24:
150                 colors = 0;
151                 line_size = width * 3;
152                 break;
153         default:
154                 printf("LCD: Unknown bpp (%d) im image!\n", bpp);
155                 if ((dst != NULL) && (dst != (uchar *)logo_bmp)) {
156                         free(dst);
157                 }
158                 return;
159         }
160         printf(" (%d*%d, %dbpp)\n", width, height, bpp);
161
162         /*
163          * Write color palette
164          */
165         if ((colors <= 256) && (lcd_depth <= 8)) {
166                 ptr = (unsigned char *)(dst + 14 + 40);
167                 for (i=0; i<colors; i++) {
168                         b = *ptr++;
169                         g = *ptr++;
170                         r = *ptr++;
171                         ptr++;
172                         S1D_WRITE_PALETTE(glob_lcd_reg, i, r, g, b);
173                 }
174         }
175
176         /*
177          * Write bitmap data into framebuffer
178          */
179         ptr = glob_lcd_mem;
180         ptr2 = (ushort *)glob_lcd_mem;
181         header_size = 14 + 40 + 4*colors;          /* skip bmp header */
182         for (y=0; y<height; y++) {
183                 bmp = &dst[(height-1-y)*line_size + header_size];
184                 if (lcd_depth == 16) {
185                         if (bpp == 24) {
186                                 for (x=0; x<width; x++) {
187                                         /*
188                                          * Generate epson 16bpp fb-format from 24bpp image
189                                          */
190                                         b = *bmp++ >> 3;
191                                         g = *bmp++ >> 2;
192                                         r = *bmp++ >> 3;
193                                         val = ((r & 0x1f) << 11) | ((g & 0x3f) << 5) | (b & 0x1f);
194                                         *ptr2++ = val;
195                                 }
196                         } else if (bpp == 8) {
197                                 for (x=0; x<line_size; x++) {
198                                         /* query rgb value from palette */
199                                         ptr = (unsigned char *)(dst + 14 + 40) ;
200                                         ptr += (*bmp++) << 2;
201                                         b = *ptr++ >> 3;
202                                         g = *ptr++ >> 2;
203                                         r = *ptr++ >> 3;
204                                         val = ((r & 0x1f) << 11) | ((g & 0x3f) << 5) | (b & 0x1f);
205                                         *ptr2++ = val;
206                                 }
207                         }
208                 } else {
209                         for (x=0; x<line_size; x++) {
210                                 *ptr++ = *bmp++;
211                         }
212                 }
213         }
214
215         if ((dst != NULL) && (dst != (uchar *)logo_bmp)) {
216                 free(dst);
217         }
218 }
219
220
221 void lcd_init(uchar *lcd_reg, uchar *lcd_mem, S1D_REGS *regs, int reg_count,
222               uchar *logo_bmp, ulong len)
223 {
224         int i;
225         ushort s1dReg;
226         uchar s1dValue;
227         int reg_byte_swap;
228
229         /*
230          * Detect epson
231          */
232         if (lcd_reg[0] == 0x1c) {
233                 /*
234                  * Big epson detected
235                  */
236                 reg_byte_swap = FALSE;
237                 palette_index = 0x1e2;
238                 palette_value = 0x1e4;
239                 lcd_depth = 16;
240                 puts("LCD:   S1D13806");
241         } else if (lcd_reg[1] == 0x1c) {
242                 /*
243                  * Big epson detected (with register swap bug)
244                  */
245                 reg_byte_swap = TRUE;
246                 palette_index = 0x1e3;
247                 palette_value = 0x1e5;
248                 lcd_depth = 16;
249                 puts("LCD:   S1D13806S");
250         } else if (lcd_reg[0] == 0x18) {
251                 /*
252                  * Small epson detected (704)
253                  */
254                 reg_byte_swap = FALSE;
255                 palette_index = 0x15;
256                 palette_value = 0x17;
257                 lcd_depth = 8;
258                 puts("LCD:   S1D13704");
259         } else if (lcd_reg[0x10000] == 0x24) {
260                 /*
261                  * Small epson detected (705)
262                  */
263                 reg_byte_swap = FALSE;
264                 palette_index = 0x15;
265                 palette_value = 0x17;
266                 lcd_depth = 8;
267                 lcd_reg += 0x10000; /* add offset for 705 regs */
268                 puts("LCD:   S1D13705");
269         } else {
270                 puts("LCD:   No controller detected!\n");
271                 return;
272         }
273
274         /*
275          * Setup lcd controller regs
276          */
277         for (i = 0; i<reg_count; i++) {
278                 s1dReg = regs[i].Index;
279                 if (reg_byte_swap) {
280                         if ((s1dReg & 0x0001) == 0)
281                                 s1dReg |= 0x0001;
282                         else
283                                 s1dReg &= ~0x0001;
284                 }
285                 s1dValue = regs[i].Value;
286                 lcd_reg[s1dReg] = s1dValue;
287         }
288
289         /*
290          * Save reg & mem pointer for later usage (e.g. bmp command)
291          */
292         glob_lcd_reg = lcd_reg;
293         glob_lcd_mem = lcd_mem;
294
295         /*
296          * Display bmp image
297          */
298         lcd_bmp(logo_bmp);
299 }
300
301 #ifdef CONFIG_VIDEO_SM501
302 int do_esdbmp(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
303 {
304         ulong addr;
305         char *str;
306
307         if (argc != 2) {
308                 printf ("Usage:\n%s\n", cmdtp->usage);
309                 return 1;
310         }
311
312         addr = simple_strtoul(argv[1], NULL, 16);
313
314         str = getenv("bd_type");
315         if ((strcmp(str, "ppc221") == 0) || (strcmp(str, "ppc231") == 0)) {
316                 /*
317                  * SM501 available, use standard bmp command
318                  */
319                 return (video_display_bitmap(addr, 0, 0));
320         } else {
321                 /*
322                  * No SM501 available, use esd epson bmp command
323                  */
324                 lcd_bmp((uchar *)addr);
325                 return 0;
326         }
327 }
328
329 U_BOOT_CMD(
330         esdbmp, 2,      1,      do_esdbmp,
331         "esdbmp   - display BMP image\n",
332         "<imageAddr> - display image\n"
333 );
334 #endif