xref: /openbmc/linux/arch/x86/boot/video-vesa.c (revision f42b3800)
1 /* -*- linux-c -*- ------------------------------------------------------- *
2  *
3  *   Copyright (C) 1991, 1992 Linus Torvalds
4  *   Copyright 2007 rPath, Inc. - All Rights Reserved
5  *
6  *   This file is part of the Linux kernel, and is made available under
7  *   the terms of the GNU General Public License version 2.
8  *
9  * ----------------------------------------------------------------------- */
10 
11 /*
12  * arch/i386/boot/video-vesa.c
13  *
14  * VESA text modes
15  */
16 
17 #include "boot.h"
18 #include "video.h"
19 #include "vesa.h"
20 
21 /* VESA information */
22 static struct vesa_general_info vginfo;
23 static struct vesa_mode_info vminfo;
24 
25 __videocard video_vesa;
26 
27 #ifndef _WAKEUP
28 static void vesa_store_mode_params_graphics(void);
29 #else /* _WAKEUP */
30 static inline void vesa_store_mode_params_graphics(void) {}
31 #endif /* _WAKEUP */
32 
33 static int vesa_probe(void)
34 {
35 #if defined(CONFIG_VIDEO_VESA) || defined(CONFIG_FIRMWARE_EDID)
36 	u16 ax, cx, di;
37 	u16 mode;
38 	addr_t mode_ptr;
39 	struct mode_info *mi;
40 	int nmodes = 0;
41 
42 	video_vesa.modes = GET_HEAP(struct mode_info, 0);
43 
44 	ax = 0x4f00;
45 	di = (size_t)&vginfo;
46 	asm(INT10
47 	    : "+a" (ax), "+D" (di), "=m" (vginfo)
48 	    : : "ebx", "ecx", "edx", "esi");
49 
50 	if (ax != 0x004f ||
51 	    vginfo.signature != VESA_MAGIC ||
52 	    vginfo.version < 0x0102)
53 		return 0;	/* Not present */
54 #endif /* CONFIG_VIDEO_VESA || CONFIG_FIRMWARE_EDID */
55 #ifdef CONFIG_VIDEO_VESA
56 	set_fs(vginfo.video_mode_ptr.seg);
57 	mode_ptr = vginfo.video_mode_ptr.off;
58 
59 	while ((mode = rdfs16(mode_ptr)) != 0xffff) {
60 		mode_ptr += 2;
61 
62 		if (!heap_free(sizeof(struct mode_info)))
63 			break;	/* Heap full, can't save mode info */
64 
65 		if (mode & ~0x1ff)
66 			continue;
67 
68 		memset(&vminfo, 0, sizeof vminfo); /* Just in case... */
69 
70 		ax = 0x4f01;
71 		cx = mode;
72 		di = (size_t)&vminfo;
73 		asm(INT10
74 		    : "+a" (ax), "+c" (cx), "+D" (di), "=m" (vminfo)
75 		    : : "ebx", "edx", "esi");
76 
77 		if (ax != 0x004f)
78 			continue;
79 
80 		if ((vminfo.mode_attr & 0x15) == 0x05) {
81 			/* Text Mode, TTY BIOS supported,
82 			   supported by hardware */
83 			mi = GET_HEAP(struct mode_info, 1);
84 			mi->mode  = mode + VIDEO_FIRST_VESA;
85 			mi->depth = 0; /* text */
86 			mi->x     = vminfo.h_res;
87 			mi->y     = vminfo.v_res;
88 			nmodes++;
89 		} else if ((vminfo.mode_attr & 0x99) == 0x99 &&
90 			   (vminfo.memory_layout == 4 ||
91 			    vminfo.memory_layout == 6) &&
92 			   vminfo.memory_planes == 1) {
93 #ifdef CONFIG_FB
94 			/* Graphics mode, color, linear frame buffer
95 			   supported.  Only register the mode if
96 			   if framebuffer is configured, however,
97 			   otherwise the user will be left without a screen.
98 			   We don't require CONFIG_FB_VESA, however, since
99 			   some of the other framebuffer drivers can use
100 			   this mode-setting, too. */
101 			mi = GET_HEAP(struct mode_info, 1);
102 			mi->mode = mode + VIDEO_FIRST_VESA;
103 			mi->depth = vminfo.bpp;
104 			mi->x = vminfo.h_res;
105 			mi->y = vminfo.v_res;
106 			nmodes++;
107 #endif
108 		}
109 	}
110 
111 	return nmodes;
112 #else
113 	return 0;
114 #endif /* CONFIG_VIDEO_VESA */
115 }
116 
117 static int vesa_set_mode(struct mode_info *mode)
118 {
119 	u16 ax, bx, cx, di;
120 	int is_graphic;
121 	u16 vesa_mode = mode->mode - VIDEO_FIRST_VESA;
122 
123 	memset(&vminfo, 0, sizeof vminfo); /* Just in case... */
124 
125 	ax = 0x4f01;
126 	cx = vesa_mode;
127 	di = (size_t)&vminfo;
128 	asm(INT10
129 	    : "+a" (ax), "+c" (cx), "+D" (di), "=m" (vminfo)
130 	    : : "ebx", "edx", "esi");
131 
132 	if (ax != 0x004f)
133 		return -1;
134 
135 	if ((vminfo.mode_attr & 0x15) == 0x05) {
136 		/* It's a supported text mode */
137 		is_graphic = 0;
138 	} else if ((vminfo.mode_attr & 0x99) == 0x99) {
139 		/* It's a graphics mode with linear frame buffer */
140 		is_graphic = 1;
141 		vesa_mode |= 0x4000; /* Request linear frame buffer */
142 	} else {
143 		return -1;	/* Invalid mode */
144 	}
145 
146 
147 	ax = 0x4f02;
148 	bx = vesa_mode;
149 	di = 0;
150 	asm volatile(INT10
151 		     : "+a" (ax), "+b" (bx), "+D" (di)
152 		     : : "ecx", "edx", "esi");
153 
154 	if (ax != 0x004f)
155 		return -1;
156 
157 	graphic_mode = is_graphic;
158 	if (!is_graphic) {
159 		/* Text mode */
160 		force_x = mode->x;
161 		force_y = mode->y;
162 		do_restore = 1;
163 	} else {
164 		/* Graphics mode */
165 		vesa_store_mode_params_graphics();
166 	}
167 
168 	return 0;
169 }
170 
171 
172 #ifndef _WAKEUP
173 
174 /* Switch DAC to 8-bit mode */
175 static void vesa_dac_set_8bits(void)
176 {
177 	u8 dac_size = 6;
178 
179 	/* If possible, switch the DAC to 8-bit mode */
180 	if (vginfo.capabilities & 1) {
181 		u16 ax, bx;
182 
183 		ax = 0x4f08;
184 		bx = 0x0800;
185 		asm volatile(INT10
186 			     : "+a" (ax), "+b" (bx)
187 			     : : "ecx", "edx", "esi", "edi");
188 
189 		if (ax == 0x004f)
190 			dac_size = bx >> 8;
191 	}
192 
193 	/* Set the color sizes to the DAC size, and offsets to 0 */
194 	boot_params.screen_info.red_size = dac_size;
195 	boot_params.screen_info.green_size = dac_size;
196 	boot_params.screen_info.blue_size = dac_size;
197 	boot_params.screen_info.rsvd_size = dac_size;
198 
199 	boot_params.screen_info.red_pos = 0;
200 	boot_params.screen_info.green_pos = 0;
201 	boot_params.screen_info.blue_pos = 0;
202 	boot_params.screen_info.rsvd_pos = 0;
203 }
204 
205 /* Save the VESA protected mode info */
206 static void vesa_store_pm_info(void)
207 {
208 	u16 ax, bx, di, es;
209 
210 	ax = 0x4f0a;
211 	bx = di = 0;
212 	asm("pushw %%es; "INT10"; movw %%es,%0; popw %%es"
213 	    : "=d" (es), "+a" (ax), "+b" (bx), "+D" (di)
214 	    : : "ecx", "esi");
215 
216 	if (ax != 0x004f)
217 		return;
218 
219 	boot_params.screen_info.vesapm_seg = es;
220 	boot_params.screen_info.vesapm_off = di;
221 }
222 
223 /*
224  * Save video mode parameters for graphics mode
225  */
226 static void vesa_store_mode_params_graphics(void)
227 {
228 	/* Tell the kernel we're in VESA graphics mode */
229 	boot_params.screen_info.orig_video_isVGA = 0x23;
230 
231 	/* Mode parameters */
232 	boot_params.screen_info.vesa_attributes = vminfo.mode_attr;
233 	boot_params.screen_info.lfb_linelength = vminfo.logical_scan;
234 	boot_params.screen_info.lfb_width = vminfo.h_res;
235 	boot_params.screen_info.lfb_height = vminfo.v_res;
236 	boot_params.screen_info.lfb_depth = vminfo.bpp;
237 	boot_params.screen_info.pages = vminfo.image_planes;
238 	boot_params.screen_info.lfb_base = vminfo.lfb_ptr;
239 	memcpy(&boot_params.screen_info.red_size,
240 	       &vminfo.rmask, 8);
241 
242 	/* General parameters */
243 	boot_params.screen_info.lfb_size = vginfo.total_memory;
244 
245 	if (vminfo.bpp <= 8)
246 		vesa_dac_set_8bits();
247 
248 	vesa_store_pm_info();
249 }
250 
251 /*
252  * Save EDID information for the kernel; this is invoked, separately,
253  * after mode-setting.
254  */
255 void vesa_store_edid(void)
256 {
257 #ifdef CONFIG_FIRMWARE_EDID
258 	u16 ax, bx, cx, dx, di;
259 
260 	/* Apparently used as a nonsense token... */
261 	memset(&boot_params.edid_info, 0x13, sizeof boot_params.edid_info);
262 
263 	if (vginfo.version < 0x0200)
264 		return;		/* EDID requires VBE 2.0+ */
265 
266 	ax = 0x4f15;		/* VBE DDC */
267 	bx = 0x0000;		/* Report DDC capabilities */
268 	cx = 0;			/* Controller 0 */
269 	di = 0;			/* ES:DI must be 0 by spec */
270 
271 	/* Note: The VBE DDC spec is different from the main VESA spec;
272 	   we genuinely have to assume all registers are destroyed here. */
273 
274 	asm("pushw %%es; movw %2,%%es; "INT10"; popw %%es"
275 	    : "+a" (ax), "+b" (bx)
276 	    :  "c" (cx), "D" (di)
277 	    : "esi");
278 
279 	if (ax != 0x004f)
280 		return;		/* No EDID */
281 
282 	/* BH = time in seconds to transfer EDD information */
283 	/* BL = DDC level supported */
284 
285 	ax = 0x4f15;		/* VBE DDC */
286 	bx = 0x0001;		/* Read EDID */
287 	cx = 0;			/* Controller 0 */
288 	dx = 0;			/* EDID block number */
289 	di =(size_t) &boot_params.edid_info; /* (ES:)Pointer to block */
290 	asm(INT10
291 	    : "+a" (ax), "+b" (bx), "+d" (dx), "=m" (boot_params.edid_info)
292 	    : "c" (cx), "D" (di)
293 	    : "esi");
294 #endif /* CONFIG_FIRMWARE_EDID */
295 }
296 
297 #endif /* not _WAKEUP */
298 
299 __videocard video_vesa =
300 {
301 	.card_name	= "VESA",
302 	.probe		= vesa_probe,
303 	.set_mode	= vesa_set_mode,
304 	.xmode_first	= VIDEO_FIRST_VESA,
305 	.xmode_n	= 0x200,
306 };
307