Since some people disapprove of white space cleanups mixed in regular commits
[coreboot.git] / payloads / libpayload / drivers / video / corebootfb.c
1 /*
2  * This file is part of the libpayload project.
3  *
4  * Copyright (C) 2008 Advanced Micro Devices, Inc.
5  * Copyright (C) 2010 coresystems GmbH
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
17  *
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
28  * SUCH DAMAGE.
29  */
30
31 #include <libpayload.h>
32 #include <coreboot_tables.h>
33 #include <pci.h>
34 #include <video_console.h>
35 #include <arch/msr.h>
36 #include "font8x16.h"
37
38 struct video_console coreboot_video_console;
39
40 static unsigned int cursor_x = 0, cursor_y = 0, cursor_en = 0;
41
42 /* color definitions for the 16 standard colors */
43
44 #define VGA_COLOR_DEFAULT 7
45 static const u32 vga_colors[] = {
46         // BLUE      | GREEN       | RED
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,
63 };
64
65 /* Addresses for the various components */
66 static unsigned long fbinfo;
67 static unsigned long fbaddr;
68 static unsigned long chars;
69
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))
73
74 static void corebootfb_scroll_up(void)
75 {
76         unsigned char *dst = FB;
77         unsigned char *src = FB + (FI->bytes_per_line * FONT_HEIGHT);
78         int y;
79
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));
83
84                 dst += FI->bytes_per_line;
85                 src += FI->bytes_per_line;
86         }
87
88         /* Erase last line */
89         dst = FB + (FI->y_resolution - FONT_HEIGHT) * FI->bytes_per_line;
90
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;
94         }
95
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);
101         int column;
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);
104
105         cursor_y--;
106 }
107
108 static void corebootfb_clear(void)
109 {
110         int row, column;
111         unsigned char *ptr = FB;
112
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;
117         }
118
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);
123 }
124
125 static void corebootfb_putchar(u8 row, u8 col, unsigned int ch)
126 {
127         unsigned char *dst;
128         unsigned char *glyph = font8x16 + ((ch & 0xFF) * FONT_HEIGHT);
129
130         unsigned char bg = (ch >> 12) & 0xF;
131         unsigned char fg = (ch >> 8) & 0xF;
132         u32 fgval = 0, bgval = 0;
133         u16 *dst16;
134         u32 *dst32;
135
136         int x, y;
137
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);
145         }
146
147
148         dst = FB + ((row * FONT_HEIGHT) * FI->bytes_per_line);
149         dst += (col * FONT_WIDTH * (FI->bits_per_pixel >> 3));
150
151         for(y = 0; y < FONT_HEIGHT; y++) {
152                 for(x = FONT_WIDTH - 1; x >= 0; x--) {
153
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;
157                                 break;
158                         case 16: /* 16 bpp */
159                                 dst16 = (u16 *)(dst + (FONT_WIDTH - x) * (FI->bits_per_pixel >> 3));
160                                 *dst16 = (*glyph & (1 << x)) ? fgval : bgval;
161                                 break;
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;
167                                 } else {
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;
171                                 }
172                                 break;
173                         case 32: /* 32 bpp */
174                                 dst32 = (u32 *)(dst + (FONT_WIDTH - x) * (FI->bits_per_pixel >> 3));
175                                 *dst32 = (*glyph & (1 << x)) ? fgval : bgval;
176                                 break;
177                         }
178                 }
179
180                 dst += FI->bytes_per_line;
181                 glyph++;
182         }
183 }
184
185 static void corebootfb_putc(u8 row, u8 col, unsigned int ch)
186 {
187         CHARS[row * coreboot_video_console.columns + col] = ch;
188         corebootfb_putchar(row, col, ch);
189 }
190
191 static void corebootfb_update_cursor(void)
192 {
193         int ch, paint;
194         if(cursor_en) {
195                 ch = CHARS[cursor_y * coreboot_video_console.columns + cursor_x];
196                 paint = (ch & 0xff) | ((ch<<4) & 0xf000) | ((ch >> 4) & 0x0f00);
197         } else {
198                 paint = CHARS[cursor_y * coreboot_video_console.columns + cursor_x];
199         }
200
201         if (cursor_y >= 0 && cursor_y < coreboot_video_console.rows)
202                 corebootfb_putchar(cursor_y, cursor_x, paint);
203 }
204
205 static void corebootfb_enable_cursor(int state)
206 {
207         cursor_en = state;
208         corebootfb_update_cursor();
209 }
210
211 static void corebootfb_get_cursor(unsigned int *x, unsigned int *y, unsigned int *en)
212 {
213         *x = cursor_x;
214         *y = cursor_y;
215         *en = cursor_en;
216 }
217
218 static void corebootfb_set_cursor(unsigned int x, unsigned int y)
219 {
220         int cursor_remember = cursor_en;
221         if (cursor_remember)
222                 corebootfb_enable_cursor(0);
223
224         cursor_x = x;
225         cursor_y = y;
226
227         if (cursor_remember)
228                 corebootfb_enable_cursor(1);
229 }
230
231 static int corebootfb_init(void)
232 {
233         if (lib_sysinfo.framebuffer == NULL)
234                 return -1;
235
236         fbinfo = (unsigned long)lib_sysinfo.framebuffer;
237
238         fbaddr = FI->physical_address;
239
240         coreboot_video_console.columns = FI->x_resolution / FONT_WIDTH;
241         coreboot_video_console.rows = FI->y_resolution / FONT_HEIGHT;
242
243         chars = (unsigned long) malloc(coreboot_video_console.rows *
244                         coreboot_video_console.columns * 2);
245
246         // clear boot splash screen if there is one.
247         corebootfb_clear();
248
249         corebootfb_set_cursor(0, 0);
250         corebootfb_enable_cursor(1);
251
252         return 0;
253 }
254
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,
260
261         .get_cursor = corebootfb_get_cursor,
262         .set_cursor = corebootfb_set_cursor,
263         .enable_cursor = corebootfb_enable_cursor,
264
265         .columns = 80,
266         .rows    = 25
267 };
268