blob: 1cfb9d3773d63103915b880446f777c0a0250ea1 [file] [log] [blame]
Kevin O'Connor7c790292014-02-11 15:34:58 -05001// Simple framebuffer vgabios for use with coreboot native vga init.
2//
3// Copyright (C) 2014 Kevin O'Connor <kevin@koconnor.net>
4//
5// This file may be distributed under the terms of the GNU LGPLv3 license.
6
7#include "biosvar.h" // GET_BDA
8#include "cbvga.h" // cbvga_setup
9#include "output.h" // dprintf
10#include "stdvga.h" // SEG_CTEXT
11#include "string.h" // memset16_far
12#include "util.h" // find_cb_table
13#include "vgabios.h" // VGAREG_*
14
15static int CBmode VAR16;
16static struct vgamode_s CBmodeinfo VAR16;
Kevin O'Connor6fed3072014-10-30 12:09:19 -040017static struct vgamode_s CBemulinfo VAR16;
Kevin O'Connor7c790292014-02-11 15:34:58 -050018static u32 CBlinelength VAR16;
19
20struct vgamode_s *cbvga_find_mode(int mode)
21{
22 if (mode == GET_GLOBAL(CBmode))
23 return &CBmodeinfo;
Kevin O'Connor6fed3072014-10-30 12:09:19 -040024 if (mode == 0x03)
25 return &CBemulinfo;
Kevin O'Connor7c790292014-02-11 15:34:58 -050026 return NULL;
27}
28
29void
30cbvga_list_modes(u16 seg, u16 *dest, u16 *last)
31{
32 if (dest<last) {
33 SET_FARVAR(seg, *dest, GET_GLOBAL(CBmode));
34 dest++;
35 }
36 SET_FARVAR(seg, *dest, 0xffff);
37}
38
39int
40cbvga_get_window(struct vgamode_s *vmode_g, int window)
41{
42 return -1;
43}
44
45int
46cbvga_set_window(struct vgamode_s *vmode_g, int window, int val)
47{
48 return -1;
49}
50
51int
52cbvga_get_linelength(struct vgamode_s *vmode_g)
53{
54 return GET_GLOBAL(CBlinelength);
55}
56
57int
58cbvga_set_linelength(struct vgamode_s *vmode_g, int val)
59{
60 return -1;
61}
62
63int
64cbvga_get_displaystart(struct vgamode_s *vmode_g)
65{
66 return 0;
67}
68
69int
70cbvga_set_displaystart(struct vgamode_s *vmode_g, int val)
71{
72 return -1;
73}
74
75int
76cbvga_get_dacformat(struct vgamode_s *vmode_g)
77{
78 return -1;
79}
80
81int
82cbvga_set_dacformat(struct vgamode_s *vmode_g, int val)
83{
84 return -1;
85}
86
87int
88cbvga_save_restore(int cmd, u16 seg, void *data)
89{
90 if (cmd & (SR_HARDWARE|SR_DAC|SR_REGISTERS))
91 return -1;
92 return bda_save_restore(cmd, seg, data);
93}
94
95int
96cbvga_set_mode(struct vgamode_s *vmode_g, int flags)
97{
Kevin O'Connorb4eb6fc2014-10-17 22:15:42 -040098 u8 emul = vmode_g == &CBemulinfo || GET_GLOBAL(CBmode) == 0x03;
99 MASK_BDA_EXT(flags, BF_EMULATE_TEXT, emul ? BF_EMULATE_TEXT : 0);
Kevin O'Connor7c790292014-02-11 15:34:58 -0500100 if (!(flags & MF_NOCLEARMEM)) {
101 if (GET_GLOBAL(CBmodeinfo.memmodel) == MM_TEXT) {
102 memset16_far(SEG_CTEXT, (void*)0, 0x0720, 80*25*2);
103 return 0;
104 }
105 struct gfx_op op;
106 init_gfx_op(&op, vmode_g);
107 op.x = op.y = 0;
108 op.xlen = GET_GLOBAL(CBmodeinfo.width);
109 op.ylen = GET_GLOBAL(CBmodeinfo.height);
110 op.op = GO_MEMSET;
111 handle_gfx_op(&op);
112 }
113 return 0;
114}
115
116#define CB_TAG_FRAMEBUFFER 0x0012
117struct cb_framebuffer {
118 u32 tag;
119 u32 size;
120
121 u64 physical_address;
122 u32 x_resolution;
123 u32 y_resolution;
124 u32 bytes_per_line;
125 u8 bits_per_pixel;
126 u8 red_mask_pos;
127 u8 red_mask_size;
128 u8 green_mask_pos;
129 u8 green_mask_size;
130 u8 blue_mask_pos;
131 u8 blue_mask_size;
132 u8 reserved_mask_pos;
133 u8 reserved_mask_size;
134};
135
136int
137cbvga_setup(void)
138{
139 dprintf(1, "coreboot vga init\n");
140
141 if (GET_GLOBAL(HaveRunInit))
142 return 0;
143
144 struct cb_header *cbh = find_cb_table();
145 if (!cbh) {
146 dprintf(1, "Unable to find coreboot table\n");
147 return -1;
148 }
149 struct cb_framebuffer *cbfb = find_cb_subtable(cbh, CB_TAG_FRAMEBUFFER);
150 if (!cbfb) {
151 // Assume there is an EGA text framebuffer.
152 dprintf(1, "Did not find coreboot framebuffer - assuming EGA text\n");
153 SET_VGA(CBmode, 0x03);
154 SET_VGA(CBlinelength, 80*2);
155 SET_VGA(CBmodeinfo.memmodel, MM_TEXT);
156 SET_VGA(CBmodeinfo.width, 80);
157 SET_VGA(CBmodeinfo.height, 25);
158 SET_VGA(CBmodeinfo.depth, 4);
159 SET_VGA(CBmodeinfo.cwidth, 9);
160 SET_VGA(CBmodeinfo.cheight, 16);
161 SET_VGA(CBmodeinfo.sstart, SEG_CTEXT);
162 return 0;
163 }
164
165 u64 addr = GET_FARVAR(0, cbfb->physical_address);
166 u8 bpp = GET_FARVAR(0, cbfb->bits_per_pixel);
167 u32 xlines = GET_FARVAR(0, cbfb->x_resolution);
168 u32 ylines = GET_FARVAR(0, cbfb->y_resolution);
169 u32 linelength = GET_FARVAR(0, cbfb->bytes_per_line);
170 dprintf(1, "Found FB @ %llx %dx%d with %d bpp (%d stride)\n"
171 , addr, xlines, ylines, bpp, linelength);
172
173 if (!addr || addr > 0xffffffff
174 || (bpp != 15 && bpp != 16 && bpp != 24 && bpp != 32)) {
175 dprintf(1, "Unable to use FB\n");
176 return -1;
177 }
178
179 SET_VGA(CBmode, 0x140);
180 SET_VGA(VBE_framebuffer, addr);
181 SET_VGA(VBE_total_memory, linelength * ylines);
182 SET_VGA(CBlinelength, linelength);
183 SET_VGA(CBmodeinfo.memmodel, MM_DIRECT);
184 SET_VGA(CBmodeinfo.width, xlines);
185 SET_VGA(CBmodeinfo.height, ylines);
186 SET_VGA(CBmodeinfo.depth, bpp);
187 SET_VGA(CBmodeinfo.cwidth, 8);
188 SET_VGA(CBmodeinfo.cheight, 16);
Kevin O'Connor6fed3072014-10-30 12:09:19 -0400189 memcpy_far(get_global_seg(), &CBemulinfo
190 , get_global_seg(), &CBmodeinfo, sizeof(CBemulinfo));
Kevin O'Connor7c790292014-02-11 15:34:58 -0500191
192 return 0;
193}