Use a block cursor on VGA console :-)
[coreboot.git] / payloads / libpayload / drivers / video / vga.c
1 /*
2  * This file is part of the libpayload project.
3  *
4  * Copyright (C) 2008 Advanced Micro Devices, Inc.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <libpayload.h>
31 #include <video_console.h>
32
33 #define VGA_COLOR_WHITE 7
34
35 #define CRTC_INDEX      0x3d4
36 #define CRTC_DATA       0x3d5
37
38 #define VIDEO(_r, _c)\
39   ((u16 *) (phys_to_virt(0xB8000) + ((_r) * (VIDEO_COLS * 2)) + ((_c) * 2)))
40
41 static u8 crtc_read(u8 index)
42 {
43         outb(index, CRTC_INDEX);
44         return inb(CRTC_DATA);
45 }
46
47 static void crtc_write(u8 data, u8 index)
48 {
49         outb(index, CRTC_INDEX);
50         outb(data, CRTC_DATA);
51 }
52
53 static void vga_get_cursor(unsigned int *x, unsigned int *y, unsigned int *en)
54 {
55         unsigned int addr;
56         addr = ((unsigned int) crtc_read(0x0E)) << 8;
57         addr += crtc_read(0x0F);
58
59         *x = addr % VIDEO_COLS;
60         *y = addr / VIDEO_COLS;
61
62         *en = !(crtc_read(0x0A) & (1 << 5));
63 }
64
65 static void vga_set_cursor(unsigned int x, unsigned int y)
66 {
67         unsigned int addr;
68
69         addr = x + (VIDEO_COLS * y);
70         crtc_write(addr >> 8, 0x0E);
71         crtc_write(addr, 0x0F);
72 }
73
74 static void vga_enable_cursor(int state)
75 {
76         unsigned char tmp = crtc_read(0x0a);
77
78         if (state == 0)
79                 tmp |= (1 << 5);
80
81         else
82                 tmp &= ~(1 << 5);
83
84         crtc_write(tmp, 0x0a);
85 }
86
87 static void vga_clear_line(u8 row, u8 ch, u8 attr)
88 {
89         int col;
90         u16 *ptr = VIDEO(row, 0);
91
92         for(col = 0; col < VIDEO_COLS; col++)
93                 ptr[col] = ((attr & 0xFF) << 8) | (ch & 0xFF);
94 }
95
96 static void vga_scroll_up(void)
97 {
98         u16 *src = VIDEO(1,0);
99         u16 *dst = VIDEO(0,0);
100         int i;
101
102         for(i = 0; i < (VIDEO_ROWS - 1) * VIDEO_COLS; i++)
103                 *dst++ = *src++;
104
105         vga_clear_line(VIDEO_ROWS - 1, ' ', VGA_COLOR_WHITE);
106 }
107
108 static void vga_fill(u8 ch, u8 attr)
109 {
110         u8 row;
111         for(row = 0; row < VIDEO_ROWS; row++)
112                 vga_clear_line(row, ch, attr);
113 }
114
115 static void vga_clear(void)
116 {
117         vga_fill(' ', VGA_COLOR_WHITE);
118 }
119
120 static void vga_putc(u8 row, u8 col, unsigned int c)
121 {
122         u16 *ptr = VIDEO(row, col);
123         *ptr = (u16) (c & 0xFFFF);
124 }
125
126 static void vga_init_cursor(void)
127 {
128         u8 val;
129
130 #define CURSOR_MSL   0x09   /* cursor maximum scan line */
131 #define CURSOR_START 0x0A   /* cursor start */
132 #define CURSOR_END   0x0B   /* cursor end */
133
134         val = crtc_read(CURSOR_MSL) & 0x1f;
135         crtc_write(0, CURSOR_START);
136         crtc_write(val - 2, CURSOR_END);
137 }
138
139 static int vga_init(void)
140 {
141         vga_init_cursor();
142         return 0;
143 }
144
145 struct video_console vga_video_console = {
146         .init = vga_init,
147         .putc = vga_putc,
148         .clear = vga_clear,
149         .scroll_up = vga_scroll_up,
150
151         .get_cursor = vga_get_cursor,
152         .set_cursor = vga_set_cursor,
153         .enable_cursor = vga_enable_cursor,
154 };