2 * This file is part of the libpayload project.
4 * Copyright (C) 2008 Advanced Micro Devices, Inc.
5 * Copyright (C) 2010 coresystems GmbH
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <libpayload.h>
32 #include <coreboot_tables.h>
34 #include <video_console.h>
38 struct video_console coreboot_video_console;
40 static unsigned int cursor_x = 0, cursor_y = 0, cursor_en = 0;
42 /* color definitions for the 16 standard colors */
44 #define VGA_COLOR_DEFAULT 7
45 static const u32 vga_colors[] = {
47 (0x00 << 16) | (0x00 << 8) | 0x00,
48 (0xAA << 16) | (0x00 << 8) | 0x00,
49 (0x00 << 16) | (0xAA << 8) | 0x00,
50 (0xAA << 16) | (0x55 << 8) | 0x00,
51 (0x00 << 16) | (0x00 << 8) | 0xAA,
52 (0xAA << 16) | (0x00 << 8) | 0xAA,
53 (0x00 << 16) | (0xAA << 8) | 0xAA,
54 (0xAA << 16) | (0xAA << 8) | 0xAA,
55 (0x55 << 16) | (0x55 << 8) | 0x55,
56 (0xFF << 16) | (0x55 << 8) | 0x55,
57 (0x55 << 16) | (0xFF << 8) | 0x55,
58 (0xFF << 16) | (0xFF << 8) | 0x55,
59 (0x55 << 16) | (0x55 << 8) | 0xFF,
60 (0xFF << 16) | (0x55 << 8) | 0xFF,
61 (0x55 << 16) | (0xFF << 8) | 0xFF,
62 (0xFF << 16) | (0xFF << 8) | 0xFF,
65 /* Addresses for the various components */
66 static unsigned long fbinfo;
67 static unsigned long fbaddr;
68 static unsigned long chars;
70 #define FI ((struct cb_framebuffer *) phys_to_virt(fbinfo))
71 #define FB ((unsigned char *) phys_to_virt(fbaddr))
72 #define CHARS ((unsigned short *) phys_to_virt(chars))
74 static void corebootfb_scroll_up(void)
76 unsigned char *dst = FB;
77 unsigned char *src = FB + (FI->bytes_per_line * FONT_HEIGHT);
80 /* Scroll all lines up */
81 for(y = 0; y < FI->y_resolution - FONT_HEIGHT; y++) {
82 memcpy(dst, src, FI->x_resolution * (FI->bits_per_pixel >> 3));
84 dst += FI->bytes_per_line;
85 src += FI->bytes_per_line;
89 dst = FB + (FI->y_resolution - FONT_HEIGHT) * FI->bytes_per_line;
91 for(; y < FI->y_resolution; y++) {
92 memset(dst, 0, FI->x_resolution * (FI->bits_per_pixel >> 3));
93 dst += FI->bytes_per_line;
96 /* And update the char buffer */
97 dst = (unsigned char *) CHARS;
98 src = (unsigned char *) (CHARS + coreboot_video_console.columns);
99 memcpy(dst, src, coreboot_video_console.columns *
100 (coreboot_video_console.rows - 1) * 2);
102 for (column = 0; column < coreboot_video_console.columns; column++)
103 CHARS[(coreboot_video_console.rows - 1) * coreboot_video_console.columns + column] = (VGA_COLOR_DEFAULT << 8);
108 static void corebootfb_clear(void)
111 unsigned char *ptr = FB;
113 /* Clear the screen */
114 for(row = 0; row < FI->y_resolution; row++) {
115 memset(ptr, 0, FI->x_resolution * (FI->bits_per_pixel >> 3));
116 ptr += FI->bytes_per_line;
119 /* And update the char buffer */
120 for(row = 0; row < coreboot_video_console.rows; row++)
121 for (column = 0; column < coreboot_video_console.columns; column++)
122 CHARS[row * coreboot_video_console.columns + column] = (VGA_COLOR_DEFAULT << 8);
125 static void corebootfb_putchar(u8 row, u8 col, unsigned int ch)
128 unsigned char *glyph = font8x16 + ((ch & 0xFF) * FONT_HEIGHT);
130 unsigned char bg = (ch >> 12) & 0xF;
131 unsigned char fg = (ch >> 8) & 0xF;
132 u32 fgval = 0, bgval = 0;
138 if (FI->bits_per_pixel > 8) {
139 bgval = ((((vga_colors[bg] >> 0) & 0xff) >> (8 - FI->blue_mask_size)) << FI->blue_mask_pos) |
140 ((((vga_colors[bg] >> 8) & 0xff) >> (8 - FI->green_mask_size)) << FI->green_mask_pos) |
141 ((((vga_colors[bg] >> 16) & 0xff) >> (8 - FI->red_mask_size)) << FI->red_mask_pos);
142 fgval = ((((vga_colors[fg] >> 0) & 0xff) >> (8 - FI->blue_mask_size)) << FI->blue_mask_pos) |
143 ((((vga_colors[fg] >> 8) & 0xff) >> (8 - FI->green_mask_size)) << FI->green_mask_pos) |
144 ((((vga_colors[fg] >> 16) & 0xff) >> (8 - FI->red_mask_size)) << FI->red_mask_pos);
148 dst = FB + ((row * FONT_HEIGHT) * FI->bytes_per_line);
149 dst += (col * FONT_WIDTH * (FI->bits_per_pixel >> 3));
151 for(y = 0; y < FONT_HEIGHT; y++) {
152 for(x = FONT_WIDTH - 1; x >= 0; x--) {
154 switch (FI->bits_per_pixel) {
155 case 8: /* Indexed */
156 dst[(FONT_WIDTH - x) * (FI->bits_per_pixel >> 3)] = (*glyph & (1 << x)) ? fg : bg;
158 case 16: /* 16 bpp */
159 dst16 = (u16 *)(dst + (FONT_WIDTH - x) * (FI->bits_per_pixel >> 3));
160 *dst16 = (*glyph & (1 << x)) ? fgval : bgval;
162 case 24: /* 24 bpp */
163 if (*glyph & (1 << x)) {
164 dst[(FONT_WIDTH - x) * (FI->bits_per_pixel >> 3) + 0] = fgval & 0xff;
165 dst[(FONT_WIDTH - x) * (FI->bits_per_pixel >> 3) + 1] = (fgval >> 8) & 0xff;
166 dst[(FONT_WIDTH - x) * (FI->bits_per_pixel >> 3) + 2] = (fgval >> 16) & 0xff;
168 dst[(FONT_WIDTH - x) * (FI->bits_per_pixel >> 3) + 0] = bgval & 0xff;
169 dst[(FONT_WIDTH - x) * (FI->bits_per_pixel >> 3) + 1] = (bgval >> 8) & 0xff;
170 dst[(FONT_WIDTH - x) * (FI->bits_per_pixel >> 3) + 2] = (bgval >> 16) & 0xff;
173 case 32: /* 32 bpp */
174 dst32 = (u32 *)(dst + (FONT_WIDTH - x) * (FI->bits_per_pixel >> 3));
175 *dst32 = (*glyph & (1 << x)) ? fgval : bgval;
180 dst += FI->bytes_per_line;
185 static void corebootfb_putc(u8 row, u8 col, unsigned int ch)
187 CHARS[row * coreboot_video_console.columns + col] = ch;
188 corebootfb_putchar(row, col, ch);
191 static void corebootfb_update_cursor(void)
195 ch = CHARS[cursor_y * coreboot_video_console.columns + cursor_x];
196 paint = (ch & 0xff) | ((ch<<4) & 0xf000) | ((ch >> 4) & 0x0f00);
198 paint = CHARS[cursor_y * coreboot_video_console.columns + cursor_x];
201 if (cursor_y >= 0 && cursor_y < coreboot_video_console.rows)
202 corebootfb_putchar(cursor_y, cursor_x, paint);
205 static void corebootfb_enable_cursor(int state)
208 corebootfb_update_cursor();
211 static void corebootfb_get_cursor(unsigned int *x, unsigned int *y, unsigned int *en)
218 static void corebootfb_set_cursor(unsigned int x, unsigned int y)
220 int cursor_remember = cursor_en;
222 corebootfb_enable_cursor(0);
228 corebootfb_enable_cursor(1);
231 static int corebootfb_init(void)
233 if (lib_sysinfo.framebuffer == NULL)
236 fbinfo = (unsigned long)lib_sysinfo.framebuffer;
238 fbaddr = FI->physical_address;
240 coreboot_video_console.columns = FI->x_resolution / FONT_WIDTH;
241 coreboot_video_console.rows = FI->y_resolution / FONT_HEIGHT;
243 chars = (unsigned long) malloc(coreboot_video_console.rows *
244 coreboot_video_console.columns * 2);
246 // clear boot splash screen if there is one.
249 corebootfb_set_cursor(0, 0);
250 corebootfb_enable_cursor(1);
255 struct video_console coreboot_video_console = {
256 .init = corebootfb_init,
257 .putc = corebootfb_putc,
258 .clear = corebootfb_clear,
259 .scroll_up = corebootfb_scroll_up,
261 .get_cursor = corebootfb_get_cursor,
262 .set_cursor = corebootfb_set_cursor,
263 .enable_cursor = corebootfb_enable_cursor,