2 * Copyright 2007 Freescale Semiconductor, Inc.
3 * York Sun <yorksun@freescale.com>
5 * FSL DIU Framebuffer driver
7 * See file CREDITS for list of people who contributed to this
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 #include "fsl_diu_fb.h"
33 const char *name
; /* optional */
34 unsigned int refresh
; /* optional */
37 unsigned int pixclock
;
38 unsigned int left_margin
;
39 unsigned int right_margin
;
40 unsigned int upper_margin
;
41 unsigned int lower_margin
;
42 unsigned int hsync_len
;
43 unsigned int vsync_len
;
49 #define FB_SYNC_VERT_HIGH_ACT 2 /* vertical sync high active */
50 #define FB_SYNC_COMP_HIGH_ACT 8 /* composite sync high active */
51 #define FB_VMODE_NONINTERLACED 0 /* non interlaced */
54 * These parameters give default parameters
55 * for video output 1024x768,
56 * FIXME - change timing to proper amounts
57 * hsync 31.5kHz, vsync 60Hz
59 static struct fb_videomode fsl_diu_mode_1024
= {
70 .sync
= FB_SYNC_COMP_HIGH_ACT
| FB_SYNC_VERT_HIGH_ACT
,
71 .vmode
= FB_VMODE_NONINTERLACED
74 static struct fb_videomode fsl_diu_mode_1280
= {
75 .name
= "1280x1024-60",
86 .sync
= FB_SYNC_COMP_HIGH_ACT
| FB_SYNC_VERT_HIGH_ACT
,
87 .vmode
= FB_VMODE_NONINTERLACED
91 * These are the fields of area descriptor(in DDR memory) for every plane
94 /* Word 0(32-bit) in DDR memory */
95 unsigned int pix_fmt
; /* hard coding pixel format */
96 /* Word 1(32-bit) in DDR memory */
98 /* Word 2(32-bit) in DDR memory */
99 unsigned int src_size_g_alpha
;
100 /* Word 3(32-bit) in DDR memory */
101 unsigned int aoi_size
;
102 /* Word 4(32-bit) in DDR memory */
103 unsigned int offset_xyi
;
104 /* Word 5(32-bit) in DDR memory */
105 unsigned int offset_xyd
;
106 /* Word 6(32-bit) in DDR memory */
107 unsigned int ckmax_r
:8;
108 unsigned int ckmax_g
:8;
109 unsigned int ckmax_b
:8;
111 /* Word 7(32-bit) in DDR memory */
112 unsigned int ckmin_r
:8;
113 unsigned int ckmin_g
:8;
114 unsigned int ckmin_b
:8;
115 unsigned int res10
:8;
116 /* Word 8(32-bit) in DDR memory */
117 unsigned int next_ad
;
118 /* Word 9(32-bit) in DDR memory, just for 64-bit aligned */
122 }__attribute__ ((packed
));
128 unsigned int desc
[3];
130 unsigned int pallete
;
132 unsigned int curs_pos
;
133 unsigned int diu_mode
;
135 unsigned int bgnd_wb
;
136 unsigned int disp_size
;
137 unsigned int wb_size
;
138 unsigned int wb_mem_addr
;
139 unsigned int hsyn_para
;
140 unsigned int vsyn_para
;
141 unsigned int syn_pol
;
142 unsigned int thresholds
;
143 unsigned int int_status
;
144 unsigned int int_mask
;
145 unsigned int colorbar
[8];
146 unsigned int filling
;
148 } __attribute__ ((packed
));
152 volatile unsigned int mode
; /* DIU operation mode */
156 unsigned char * paddr
; /* Virtual address */
161 * Modes of operation of DIU
163 #define MFB_MODE0 0 /* DIU off */
164 #define MFB_MODE1 1 /* All three planes output to display */
165 #define MFB_MODE2 2 /* Plane 1 to display,
166 * planes 2+3 written back to memory */
167 #define MFB_MODE3 3 /* All three planes written back to memory */
168 #define MFB_MODE4 4 /* Color bar generation */
172 static struct fb_info fsl_fb_info
;
173 static struct diu_addr gamma
, cursor
;
174 static struct diu_ad fsl_diu_fb_ad
__attribute__ ((aligned(32)));
175 static struct diu_ad dummy_ad
__attribute__ ((aligned(32)));
176 static unsigned char *dummy_fb
;
177 static struct diu_hw dr
= {
182 int fb_initialized
= 0;
183 const int default_xres
= 1280;
184 const int default_pixel_format
= 0x88882317;
186 static int map_video_memory(struct fb_info
*info
, unsigned long bytes_align
);
187 static void enable_lcdc(void);
188 static void disable_lcdc(void);
189 static int fsl_diu_enable_panel(struct fb_info
*info
);
190 static int fsl_diu_disable_panel(struct fb_info
*info
);
191 static int allocate_buf(struct diu_addr
*buf
, u32 size
, u32 bytes_align
);
192 void diu_set_pixel_clock(unsigned int pixclock
);
194 int fsl_diu_init(int xres
,
195 unsigned int pixel_format
,
197 unsigned char *splash_bmp
)
199 struct fb_videomode
*fsl_diu_mode_db
;
200 struct diu_ad
*ad
= &fsl_diu_fb_ad
;
202 struct fb_info
*info
= &fsl_fb_info
;
203 struct fb_var_screeninfo
*var
= &info
->var
;
204 unsigned char *gamma_table_base
;
207 debug("Enter fsl_diu_init\n");
208 dr
.diu_reg
= (struct diu
*) (CONFIG_SYS_DIU_ADDR
);
209 hw
= (struct diu
*) dr
.diu_reg
;
214 fsl_diu_mode_db
= &fsl_diu_mode_1280
;
216 fsl_diu_mode_db
= &fsl_diu_mode_1024
;
219 if (0 == fb_initialized
) {
220 allocate_buf(&gamma
, 768, 32);
221 debug("gamma is allocated @ 0x%x\n",
222 (unsigned int)gamma
.paddr
);
223 allocate_buf(&cursor
, MAX_CURS
* MAX_CURS
* 2, 32);
224 debug("curosr is allocated @ 0x%x\n",
225 (unsigned int)cursor
.paddr
);
227 /* create a dummy fb and dummy ad */
228 dummy_fb
= malloc(64);
229 if (NULL
== dummy_fb
) {
230 printf("Cannot allocate dummy fb\n");
233 dummy_ad
.addr
= cpu_to_le32((unsigned int)dummy_fb
);
234 dummy_ad
.pix_fmt
= 0x88882317;
235 dummy_ad
.src_size_g_alpha
= 0x04400000; /* alpha = 0 */
236 dummy_ad
.aoi_size
= 0x02000400;
237 dummy_ad
.offset_xyi
= 0;
238 dummy_ad
.offset_xyd
= 0;
239 dummy_ad
.next_ad
= 0;
240 /* Memory allocation for framebuffer */
241 if (map_video_memory(info
, 32)) {
242 printf("Unable to allocate fb memory 1\n");
247 memset(info
->screen_base
, 0, info
->smem_len
);
249 dr
.diu_reg
->desc
[0] = (unsigned int) &dummy_ad
;
250 dr
.diu_reg
->desc
[1] = (unsigned int) &dummy_ad
;
251 dr
.diu_reg
->desc
[2] = (unsigned int) &dummy_ad
;
252 debug("dummy dr.diu_reg->desc[0] = 0x%x\n", dr
.diu_reg
->desc
[0]);
253 debug("dummy desc[0] = 0x%x\n", hw
->desc
[0]);
256 var
->xres
= fsl_diu_mode_db
->xres
;
257 var
->yres
= fsl_diu_mode_db
->yres
;
258 var
->bits_per_pixel
= 32;
259 var
->pixclock
= fsl_diu_mode_db
->pixclock
;
260 var
->left_margin
= fsl_diu_mode_db
->left_margin
;
261 var
->right_margin
= fsl_diu_mode_db
->right_margin
;
262 var
->upper_margin
= fsl_diu_mode_db
->upper_margin
;
263 var
->lower_margin
= fsl_diu_mode_db
->lower_margin
;
264 var
->hsync_len
= fsl_diu_mode_db
->hsync_len
;
265 var
->vsync_len
= fsl_diu_mode_db
->vsync_len
;
266 var
->sync
= fsl_diu_mode_db
->sync
;
267 var
->vmode
= fsl_diu_mode_db
->vmode
;
268 info
->line_length
= var
->xres
* var
->bits_per_pixel
/ 8;
270 info
->logo_height
= 0;
272 ad
->pix_fmt
= pixel_format
;
273 ad
->addr
= cpu_to_le32((unsigned int)info
->screen_base
);
275 = cpu_to_le32((var
->yres
<< 12) | var
->xres
);
276 /* fix me. AOI should not be greater than display size */
277 ad
->aoi_size
= cpu_to_le32(( var
->yres
<< 16) | var
->xres
);
281 /* Disable chroma keying function */
290 gamma_table_base
= gamma
.paddr
;
291 debug("gamma_table_base is allocated @ 0x%x\n",
292 (unsigned int)gamma_table_base
);
294 /* Prep for DIU init - gamma table */
296 for (i
= 0; i
<= 2; i
++)
297 for (j
= 0; j
<= 255; j
++)
298 *gamma_table_base
++ = j
;
300 if (gamma_fix
== 1) { /* fix the gamma */
301 debug("Fix gamma table\n");
302 gamma_table_base
= gamma
.paddr
;
303 for (i
= 0; i
< 256*3; i
++) {
304 gamma_table_base
[i
] = (gamma_table_base
[i
] << 2)
305 | ((gamma_table_base
[i
] >> 6) & 0x03);
309 debug("update-lcdc: HW - %p\n Disabling DIU\n", hw
);
311 /* Program DIU registers */
313 hw
->gamma
= (unsigned int) gamma
.paddr
;
314 hw
->cursor
= (unsigned int) cursor
.paddr
;
315 hw
->bgnd
= 0x007F7F7F; /* BGND */
316 hw
->bgnd_wb
= 0; /* BGND_WB */
317 hw
->disp_size
= var
->yres
<< 16 | var
->xres
; /* DISP SIZE */
318 hw
->wb_size
= 0; /* WB SIZE */
319 hw
->wb_mem_addr
= 0; /* WB MEM ADDR */
320 hw
->hsyn_para
= var
->left_margin
<< 22 | /* BP_H */
321 var
->hsync_len
<< 11 | /* PW_H */
322 var
->right_margin
; /* FP_H */
323 hw
->vsyn_para
= var
->upper_margin
<< 22 | /* BP_V */
324 var
->vsync_len
<< 11 | /* PW_V */
325 var
->lower_margin
; /* FP_V */
327 hw
->syn_pol
= 0; /* SYNC SIGNALS POLARITY */
328 hw
->thresholds
= 0x00037800; /* The Thresholds */
329 hw
->int_status
= 0; /* INTERRUPT STATUS */
330 hw
->int_mask
= 0; /* INT MASK */
331 hw
->plut
= 0x01F5F666;
333 /* Pixel Clock configuration */
334 debug("DIU pixclock in ps - %d\n", var
->pixclock
);
335 diu_set_pixel_clock(var
->pixclock
);
340 info
->logo_height
= fsl_diu_display_bmp(splash_bmp
, 0, 0, 0);
341 info
->logo_size
= info
->logo_height
* info
->line_length
;
342 debug("logo height %d, logo_size 0x%x\n",
343 info
->logo_height
,info
->logo_size
);
347 fsl_diu_enable_panel(info
);
353 char *fsl_fb_open(struct fb_info
**info
)
355 *info
= &fsl_fb_info
;
356 return (char *) ((unsigned int)(*info
)->screen_base
357 + (*info
)->logo_size
);
360 void fsl_diu_close(void)
362 struct fb_info
*info
= &fsl_fb_info
;
363 fsl_diu_disable_panel(info
);
366 static int fsl_diu_enable_panel(struct fb_info
*info
)
368 struct diu
*hw
= dr
.diu_reg
;
369 struct diu_ad
*ad
= &fsl_diu_fb_ad
;
371 debug("Entered: enable_panel\n");
372 if (hw
->desc
[0] != (unsigned int)ad
)
373 hw
->desc
[0] = (unsigned int)ad
;
374 debug("desc[0] = 0x%x\n", hw
->desc
[0]);
378 static int fsl_diu_disable_panel(struct fb_info
*info
)
380 struct diu
*hw
= dr
.diu_reg
;
382 debug("Entered: disable_panel\n");
383 if (hw
->desc
[0] != (unsigned int)&dummy_ad
)
384 hw
->desc
[0] = (unsigned int)&dummy_ad
;
388 static int map_video_memory(struct fb_info
*info
, unsigned long bytes_align
)
390 unsigned long offset
;
393 debug("Entered: map_video_memory\n");
394 /* allocate maximum 1280*1024 with 32bpp */
395 info
->smem_len
= 1280 * 4 *1024 + bytes_align
;
396 debug("MAP_VIDEO_MEMORY: smem_len = %d\n", info
->smem_len
);
397 info
->screen_base
= malloc(info
->smem_len
);
398 if (info
->screen_base
== NULL
) {
399 printf("Unable to allocate fb memory\n");
402 info
->smem_start
= (unsigned int) info
->screen_base
;
403 mask
= bytes_align
- 1;
404 offset
= (unsigned long)info
->screen_base
& mask
;
406 info
->screen_base
+= (bytes_align
- offset
);
407 info
->smem_len
= info
->smem_len
- (bytes_align
- offset
);
409 info
->smem_len
= info
->smem_len
- bytes_align
;
411 info
->screen_size
= info
->smem_len
;
413 debug("Allocated fb @ 0x%08lx, size=%d.\n",
414 info
->smem_start
, info
->smem_len
);
419 static void enable_lcdc(void)
421 struct diu
*hw
= dr
.diu_reg
;
423 debug("Entered: enable_lcdc, fb_enabled = %d\n", fb_enabled
);
425 hw
->diu_mode
= dr
.mode
;
428 debug("diu_mode = %d\n", hw
->diu_mode
);
431 static void disable_lcdc(void)
433 struct diu
*hw
= dr
.diu_reg
;
435 debug("Entered: disable_lcdc, fb_enabled = %d\n", fb_enabled
);
443 * Align to 64-bit(8-byte), 32-byte, etc.
445 static int allocate_buf(struct diu_addr
*buf
, u32 size
, u32 bytes_align
)
450 debug("Entered: allocate_buf\n");
451 ssize
= size
+ bytes_align
;
452 buf
->paddr
= malloc(ssize
);
456 memset(buf
->paddr
, 0, ssize
);
457 mask
= bytes_align
- 1;
458 offset
= (u32
)buf
->paddr
& mask
;
460 buf
->offset
= bytes_align
- offset
;
461 buf
->paddr
= (unsigned char *) ((u32
)buf
->paddr
+ offset
);
467 int fsl_diu_display_bmp(unsigned char *bmp
,
472 struct fb_info
*info
= &fsl_fb_info
;
473 unsigned char r
, g
, b
;
474 unsigned int *fb_t
, val
;
475 unsigned char *bitmap
;
476 unsigned int palette
[256];
477 int width
, height
, bpp
, ncolors
, raster
, offset
, x
, y
, i
, k
, cpp
;
480 printf("Must supply a bitmap address\n");
484 raster
= bmp
[10] + (bmp
[11] << 8) + (bmp
[12] << 16) + (bmp
[13] << 24);
485 width
= (bmp
[21] << 24) | (bmp
[20] << 16) | (bmp
[19] << 8) | bmp
[18];
486 height
= (bmp
[25] << 24) | (bmp
[24] << 16) | (bmp
[23] << 8) | bmp
[22];
487 bpp
= (bmp
[29] << 8) | (bmp
[28]);
488 ncolors
= bmp
[46] + (bmp
[47] << 8) + (bmp
[48] << 16) + (bmp
[49] << 24);
489 bitmap
= bmp
+ raster
;
490 cpp
= info
->var
.bits_per_pixel
/ 8;
492 debug("bmp = 0x%08x\n", (unsigned int)bmp
);
493 debug("bitmap = 0x%08x\n", (unsigned int)bitmap
);
494 debug("width = %d\n", width
);
495 debug("height = %d\n", height
);
496 debug("bpp = %d\n", bpp
);
497 debug("ncolors = %d\n", ncolors
);
499 debug("xres = %d\n", info
->var
.xres
);
500 debug("yres = %d\n", info
->var
.yres
);
501 debug("Screen_base = 0x%x\n", (unsigned int)info
->screen_base
);
503 if (((width
+xoffset
) > info
->var
.xres
) ||
504 ((height
+yoffset
) > info
->var
.yres
)) {
505 printf("bitmap is out of range, image too large or too much offset\n");
509 for (i
= 0, offset
= 54; i
< ncolors
; i
++, offset
+= 4)
510 palette
[i
] = (bmp
[offset
+2] << 16)
511 + (bmp
[offset
+1] << 8) + bmp
[offset
];
516 for (y
= height
- 1; y
>= 0; y
--) {
517 fb_t
= (unsigned int *) ((unsigned int)info
->screen_base
+ (((y
+yoffset
) * info
->var
.xres
) + xoffset
)*cpp
);
518 for (x
= 0; x
< width
; x
+= 8) {
520 for (k
= 0; k
< 8; k
++) {
528 for (i
= (width
/ 2) % 4; i
> 0; i
--)
533 for (y
= height
- 1; y
>= 0; y
--) {
534 fb_t
= (unsigned int *) ((unsigned int)info
->screen_base
+ (((y
+yoffset
) * info
->var
.xres
) + xoffset
)*cpp
);
535 for (x
= 0; x
< width
; x
+= 2) {
539 *fb_t
++ = palette
[r
];
540 *fb_t
++ = palette
[g
];
542 for (i
= (width
/ 2) % 4; i
> 0; i
--)
547 for (y
= height
- 1; y
>= 0; y
--) {
548 fb_t
= (unsigned int *) ((unsigned int)info
->screen_base
+ (((y
+yoffset
) * info
->var
.xres
) + xoffset
)*cpp
);
549 for (x
= 0; x
< width
; x
++) {
550 *fb_t
++ = palette
[ *bitmap
++ ];
552 for (i
= (width
/ 2) % 4; i
> 0; i
--)
557 for (y
= height
- 1; y
>= 0; y
--) {
558 fb_t
= (unsigned int *) ((unsigned int)info
->screen_base
+ (((y
+yoffset
) * info
->var
.xres
) + xoffset
)*cpp
);
559 for (x
= 0; x
< width
; x
++) {
563 val
= (r
<< 16) + (g
<< 8) + b
;
566 for (; (x
% 4) != 0; x
++) /* 4-byte alignment */
575 void fsl_diu_clear_screen(void)
577 struct fb_info
*info
= &fsl_fb_info
;
579 memset(info
->screen_base
, 0, info
->smem_len
);