blob: a9c6d3a780862e5cc6c39ebc5a6eb90b197edaa5 [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;
17static u32 CBlinelength VAR16;
18
19struct vgamode_s *cbvga_find_mode(int mode)
20{
21 if (mode == GET_GLOBAL(CBmode))
22 return &CBmodeinfo;
23 return NULL;
24}
25
26void
27cbvga_list_modes(u16 seg, u16 *dest, u16 *last)
28{
29 if (dest<last) {
30 SET_FARVAR(seg, *dest, GET_GLOBAL(CBmode));
31 dest++;
32 }
33 SET_FARVAR(seg, *dest, 0xffff);
34}
35
36int
37cbvga_get_window(struct vgamode_s *vmode_g, int window)
38{
39 return -1;
40}
41
42int
43cbvga_set_window(struct vgamode_s *vmode_g, int window, int val)
44{
45 return -1;
46}
47
48int
49cbvga_get_linelength(struct vgamode_s *vmode_g)
50{
51 return GET_GLOBAL(CBlinelength);
52}
53
54int
55cbvga_set_linelength(struct vgamode_s *vmode_g, int val)
56{
57 return -1;
58}
59
60int
61cbvga_get_displaystart(struct vgamode_s *vmode_g)
62{
63 return 0;
64}
65
66int
67cbvga_set_displaystart(struct vgamode_s *vmode_g, int val)
68{
69 return -1;
70}
71
72int
73cbvga_get_dacformat(struct vgamode_s *vmode_g)
74{
75 return -1;
76}
77
78int
79cbvga_set_dacformat(struct vgamode_s *vmode_g, int val)
80{
81 return -1;
82}
83
84int
85cbvga_save_restore(int cmd, u16 seg, void *data)
86{
87 if (cmd & (SR_HARDWARE|SR_DAC|SR_REGISTERS))
88 return -1;
89 return bda_save_restore(cmd, seg, data);
90}
91
92int
93cbvga_set_mode(struct vgamode_s *vmode_g, int flags)
94{
95 if (!(flags & MF_NOCLEARMEM)) {
96 if (GET_GLOBAL(CBmodeinfo.memmodel) == MM_TEXT) {
97 memset16_far(SEG_CTEXT, (void*)0, 0x0720, 80*25*2);
98 return 0;
99 }
100 struct gfx_op op;
101 init_gfx_op(&op, vmode_g);
102 op.x = op.y = 0;
103 op.xlen = GET_GLOBAL(CBmodeinfo.width);
104 op.ylen = GET_GLOBAL(CBmodeinfo.height);
105 op.op = GO_MEMSET;
106 handle_gfx_op(&op);
107 }
108 return 0;
109}
110
111#define CB_TAG_FRAMEBUFFER 0x0012
112struct cb_framebuffer {
113 u32 tag;
114 u32 size;
115
116 u64 physical_address;
117 u32 x_resolution;
118 u32 y_resolution;
119 u32 bytes_per_line;
120 u8 bits_per_pixel;
121 u8 red_mask_pos;
122 u8 red_mask_size;
123 u8 green_mask_pos;
124 u8 green_mask_size;
125 u8 blue_mask_pos;
126 u8 blue_mask_size;
127 u8 reserved_mask_pos;
128 u8 reserved_mask_size;
129};
130
131int
132cbvga_setup(void)
133{
134 dprintf(1, "coreboot vga init\n");
135
136 if (GET_GLOBAL(HaveRunInit))
137 return 0;
138
139 struct cb_header *cbh = find_cb_table();
140 if (!cbh) {
141 dprintf(1, "Unable to find coreboot table\n");
142 return -1;
143 }
144 struct cb_framebuffer *cbfb = find_cb_subtable(cbh, CB_TAG_FRAMEBUFFER);
145 if (!cbfb) {
146 // Assume there is an EGA text framebuffer.
147 dprintf(1, "Did not find coreboot framebuffer - assuming EGA text\n");
148 SET_VGA(CBmode, 0x03);
149 SET_VGA(CBlinelength, 80*2);
150 SET_VGA(CBmodeinfo.memmodel, MM_TEXT);
151 SET_VGA(CBmodeinfo.width, 80);
152 SET_VGA(CBmodeinfo.height, 25);
153 SET_VGA(CBmodeinfo.depth, 4);
154 SET_VGA(CBmodeinfo.cwidth, 9);
155 SET_VGA(CBmodeinfo.cheight, 16);
156 SET_VGA(CBmodeinfo.sstart, SEG_CTEXT);
157 return 0;
158 }
159
160 u64 addr = GET_FARVAR(0, cbfb->physical_address);
161 u8 bpp = GET_FARVAR(0, cbfb->bits_per_pixel);
162 u32 xlines = GET_FARVAR(0, cbfb->x_resolution);
163 u32 ylines = GET_FARVAR(0, cbfb->y_resolution);
164 u32 linelength = GET_FARVAR(0, cbfb->bytes_per_line);
165 dprintf(1, "Found FB @ %llx %dx%d with %d bpp (%d stride)\n"
166 , addr, xlines, ylines, bpp, linelength);
167
168 if (!addr || addr > 0xffffffff
169 || (bpp != 15 && bpp != 16 && bpp != 24 && bpp != 32)) {
170 dprintf(1, "Unable to use FB\n");
171 return -1;
172 }
173
174 SET_VGA(CBmode, 0x140);
175 SET_VGA(VBE_framebuffer, addr);
176 SET_VGA(VBE_total_memory, linelength * ylines);
177 SET_VGA(CBlinelength, linelength);
178 SET_VGA(CBmodeinfo.memmodel, MM_DIRECT);
179 SET_VGA(CBmodeinfo.width, xlines);
180 SET_VGA(CBmodeinfo.height, ylines);
181 SET_VGA(CBmodeinfo.depth, bpp);
182 SET_VGA(CBmodeinfo.cwidth, 8);
183 SET_VGA(CBmodeinfo.cheight, 16);
184
185 // Setup BDA and clear screen.
186 vga_set_mode(GET_GLOBAL(CBmode), 0);
187
188 return 0;
189}