Pull sbs into release branch
[pandora-kernel.git] / arch / i386 / boot / video.c
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.c
13  *
14  * Select video mode
15  */
16
17 #include "boot.h"
18 #include "video.h"
19 #include "vesa.h"
20
21 /*
22  * Mode list variables
23  */
24 static struct card_info cards[];    /* List of cards to probe for */
25
26 /*
27  * Common variables
28  */
29 int adapter;                    /* 0=CGA/MDA/HGC, 1=EGA, 2=VGA+ */
30 u16 video_segment;
31 int force_x, force_y;   /* Don't query the BIOS for cols/rows */
32
33 int do_restore = 0;     /* Screen contents changed during mode flip */
34 int graphic_mode;       /* Graphic mode with linear frame buffer */
35
36 static void store_cursor_position(void)
37 {
38         u16 curpos;
39         u16 ax, bx;
40
41         ax = 0x0300;
42         bx = 0;
43         asm(INT10
44             : "=d" (curpos), "+a" (ax), "+b" (bx)
45             : : "ecx", "esi", "edi");
46
47         boot_params.screen_info.orig_x = curpos;
48         boot_params.screen_info.orig_y = curpos >> 8;
49 }
50
51 static void store_video_mode(void)
52 {
53         u16 ax, page;
54
55         /* N.B.: the saving of the video page here is a bit silly,
56            since we pretty much assume page 0 everywhere. */
57         ax = 0x0f00;
58         asm(INT10
59             : "+a" (ax), "=b" (page)
60             : : "ecx", "edx", "esi", "edi");
61
62         /* Not all BIOSes are clean with respect to the top bit */
63         boot_params.screen_info.orig_video_mode = ax & 0x7f;
64         boot_params.screen_info.orig_video_page = page;
65 }
66
67 /*
68  * Store the video mode parameters for later usage by the kernel.
69  * This is done by asking the BIOS except for the rows/columns
70  * parameters in the default 80x25 mode -- these are set directly,
71  * because some very obscure BIOSes supply insane values.
72  */
73 static void store_mode_params(void)
74 {
75         u16 font_size;
76         int x, y;
77
78         /* For graphics mode, it is up to the mode-setting driver
79            (currently only video-vesa.c) to store the parameters */
80         if (graphic_mode)
81                 return;
82
83         store_cursor_position();
84         store_video_mode();
85
86         if (boot_params.screen_info.orig_video_mode == 0x07) {
87                 /* MDA, HGC, or VGA in monochrome mode */
88                 video_segment = 0xb000;
89         } else {
90                 /* CGA, EGA, VGA and so forth */
91                 video_segment = 0xb800;
92         }
93
94         set_fs(0);
95         font_size = rdfs16(0x485); /* Font size, BIOS area */
96         boot_params.screen_info.orig_video_points = font_size;
97
98         x = rdfs16(0x44a);
99         y = (adapter == ADAPTER_CGA) ? 25 : rdfs8(0x484)+1;
100
101         if (force_x)
102                 x = force_x;
103         if (force_y)
104                 y = force_y;
105
106         boot_params.screen_info.orig_video_cols  = x;
107         boot_params.screen_info.orig_video_lines = y;
108 }
109
110 /* Probe the video drivers and have them generate their mode lists. */
111 static void probe_cards(int unsafe)
112 {
113         struct card_info *card;
114         static u8 probed[2];
115
116         if (probed[unsafe])
117                 return;
118
119         probed[unsafe] = 1;
120
121         for (card = video_cards; card < video_cards_end; card++) {
122                 if (card->unsafe == unsafe) {
123                         if (card->probe)
124                                 card->nmodes = card->probe();
125                         else
126                                 card->nmodes = 0;
127                 }
128         }
129 }
130
131 /* Test if a mode is defined */
132 int mode_defined(u16 mode)
133 {
134         struct card_info *card;
135         struct mode_info *mi;
136         int i;
137
138         for (card = video_cards; card < video_cards_end; card++) {
139                 mi = card->modes;
140                 for (i = 0; i < card->nmodes; i++, mi++) {
141                         if (mi->mode == mode)
142                                 return 1;
143                 }
144         }
145
146         return 0;
147 }
148
149 /* Set mode (without recalc) */
150 static int raw_set_mode(u16 mode)
151 {
152         int nmode, i;
153         struct card_info *card;
154         struct mode_info *mi;
155
156         /* Drop the recalc bit if set */
157         mode &= ~VIDEO_RECALC;
158
159         /* Scan for mode based on fixed ID, position, or resolution */
160         nmode = 0;
161         for (card = video_cards; card < video_cards_end; card++) {
162                 mi = card->modes;
163                 for (i = 0; i < card->nmodes; i++, mi++) {
164                         int visible = mi->x || mi->y;
165
166                         if ((mode == nmode && visible) ||
167                             mode == mi->mode ||
168                             mode == (mi->y << 8)+mi->x)
169                                 return card->set_mode(mi);
170
171                         if (visible)
172                                 nmode++;
173                 }
174         }
175
176         /* Nothing found?  Is it an "exceptional" (unprobed) mode? */
177         for (card = video_cards; card < video_cards_end; card++) {
178                 if (mode >= card->xmode_first &&
179                     mode < card->xmode_first+card->xmode_n) {
180                         struct mode_info mix;
181                         mix.mode = mode;
182                         mix.x = mix.y = 0;
183                         return card->set_mode(&mix);
184                 }
185         }
186
187         /* Otherwise, failure... */
188         return -1;
189 }
190
191 /*
192  * Recalculate the vertical video cutoff (hack!)
193  */
194 static void vga_recalc_vertical(void)
195 {
196         unsigned int font_size, rows;
197         u16 crtc;
198         u8 pt, ov;
199
200         set_fs(0);
201         font_size = rdfs8(0x485); /* BIOS: font size (pixels) */
202         rows = force_y ? force_y : rdfs8(0x484)+1; /* Text rows */
203
204         rows *= font_size;      /* Visible scan lines */
205         rows--;                 /* ... minus one */
206
207         crtc = vga_crtc();
208
209         pt = in_idx(crtc, 0x11);
210         pt &= ~0x80;            /* Unlock CR0-7 */
211         out_idx(pt, crtc, 0x11);
212
213         out_idx((u8)rows, crtc, 0x12); /* Lower height register */
214
215         ov = in_idx(crtc, 0x07); /* Overflow register */
216         ov &= 0xbd;
217         ov |= (rows >> (8-1)) & 0x02;
218         ov |= (rows >> (9-6)) & 0x40;
219         out_idx(ov, crtc, 0x07);
220 }
221
222 /* Set mode (with recalc if specified) */
223 static int set_mode(u16 mode)
224 {
225         int rv;
226
227         /* Very special mode numbers... */
228         if (mode == VIDEO_CURRENT_MODE)
229                 return 0;       /* Nothing to do... */
230         else if (mode == NORMAL_VGA)
231                 mode = VIDEO_80x25;
232         else if (mode == EXTENDED_VGA)
233                 mode = VIDEO_8POINT;
234
235         rv = raw_set_mode(mode);
236         if (rv)
237                 return rv;
238
239         if (mode & VIDEO_RECALC)
240                 vga_recalc_vertical();
241
242         return 0;
243 }
244
245 static unsigned int get_entry(void)
246 {
247         char entry_buf[4];
248         int i, len = 0;
249         int key;
250         unsigned int v;
251
252         do {
253                 key = getchar();
254
255                 if (key == '\b') {
256                         if (len > 0) {
257                                 puts("\b \b");
258                                 len--;
259                         }
260                 } else if ((key >= '0' && key <= '9') ||
261                            (key >= 'A' && key <= 'Z') ||
262                            (key >= 'a' && key <= 'z')) {
263                         if (len < sizeof entry_buf) {
264                                 entry_buf[len++] = key;
265                                 putchar(key);
266                         }
267                 }
268         } while (key != '\r');
269         putchar('\n');
270
271         if (len == 0)
272                 return VIDEO_CURRENT_MODE; /* Default */
273
274         v = 0;
275         for (i = 0; i < len; i++) {
276                 v <<= 4;
277                 key = entry_buf[i] | 0x20;
278                 v += (key > '9') ? key-'a'+10 : key-'0';
279         }
280
281         return v;
282 }
283
284 static void display_menu(void)
285 {
286         struct card_info *card;
287         struct mode_info *mi;
288         char ch;
289         int i;
290
291         puts("Mode:    COLSxROWS:\n");
292
293         ch = '0';
294         for (card = video_cards; card < video_cards_end; card++) {
295                 mi = card->modes;
296                 for (i = 0; i < card->nmodes; i++, mi++) {
297                         int visible = mi->x && mi->y;
298                         u16 mode_id = mi->mode ? mi->mode :
299                                 (mi->y << 8)+mi->x;
300
301                         if (!visible)
302                                 continue; /* Hidden mode */
303
304                         printf("%c  %04X  %3dx%-3d  %s\n",
305                                ch, mode_id, mi->x, mi->y, card->card_name);
306
307                         if (ch == '9')
308                                 ch = 'a';
309                         else if (ch == 'z' || ch == ' ')
310                                 ch = ' '; /* Out of keys... */
311                         else
312                                 ch++;
313                 }
314         }
315 }
316
317 #define H(x)    ((x)-'a'+10)
318 #define SCAN    ((H('s')<<12)+(H('c')<<8)+(H('a')<<4)+H('n'))
319
320 static unsigned int mode_menu(void)
321 {
322         int key;
323         unsigned int sel;
324
325         puts("Press <ENTER> to see video modes available, "
326              "<SPACE> to continue, or wait 30 sec\n");
327
328         kbd_flush();
329         while (1) {
330                 key = getchar_timeout();
331                 if (key == ' ' || key == 0)
332                         return VIDEO_CURRENT_MODE; /* Default */
333                 if (key == '\r')
334                         break;
335                 putchar('\a');  /* Beep! */
336         }
337
338
339         for (;;) {
340                 display_menu();
341
342                 puts("Enter a video mode or \"scan\" to scan for "
343                      "additional modes: ");
344                 sel = get_entry();
345                 if (sel != SCAN)
346                         return sel;
347
348                 probe_cards(1);
349         }
350 }
351
352 #ifdef CONFIG_VIDEO_RETAIN
353 /* Save screen content to the heap */
354 struct saved_screen {
355         int x, y;
356         int curx, cury;
357         u16 *data;
358 } saved;
359
360 static void save_screen(void)
361 {
362         /* Should be called after store_mode_params() */
363         saved.x = boot_params.screen_info.orig_video_cols;
364         saved.y = boot_params.screen_info.orig_video_lines;
365         saved.curx = boot_params.screen_info.orig_x;
366         saved.cury = boot_params.screen_info.orig_y;
367
368         if (heap_free() < saved.x*saved.y*sizeof(u16)+512)
369                 return;         /* Not enough heap to save the screen */
370
371         saved.data = GET_HEAP(u16, saved.x*saved.y);
372
373         set_fs(video_segment);
374         copy_from_fs(saved.data, 0, saved.x*saved.y*sizeof(u16));
375 }
376
377 static void restore_screen(void)
378 {
379         /* Should be called after store_mode_params() */
380         int xs = boot_params.screen_info.orig_video_cols;
381         int ys = boot_params.screen_info.orig_video_lines;
382         int y;
383         addr_t dst = 0;
384         u16 *src = saved.data;
385         u16 ax, bx, dx;
386
387         if (graphic_mode)
388                 return;         /* Can't restore onto a graphic mode */
389
390         if (!src)
391                 return;         /* No saved screen contents */
392
393         /* Restore screen contents */
394
395         set_fs(video_segment);
396         for (y = 0; y < ys; y++) {
397                 int npad;
398
399                 if (y < saved.y) {
400                         int copy = (xs < saved.x) ? xs : saved.x;
401                         copy_to_fs(dst, src, copy*sizeof(u16));
402                         dst += copy*sizeof(u16);
403                         src += saved.x;
404                         npad = (xs < saved.x) ? 0 : xs-saved.x;
405                 } else {
406                         npad = xs;
407                 }
408
409                 /* Writes "npad" blank characters to
410                    video_segment:dst and advances dst */
411                 asm volatile("pushw %%es ; "
412                              "movw %2,%%es ; "
413                              "shrw %%cx ; "
414                              "jnc 1f ; "
415                              "stosw \n\t"
416                              "1: rep;stosl ; "
417                              "popw %%es"
418                              : "+D" (dst), "+c" (npad)
419                              : "bdS" (video_segment),
420                                "a" (0x07200720));
421         }
422
423         /* Restore cursor position */
424         ax = 0x0200;            /* Set cursor position */
425         bx = 0;                 /* Page number (<< 8) */
426         dx = (saved.cury << 8)+saved.curx;
427         asm volatile(INT10
428                      : "+a" (ax), "+b" (bx), "+d" (dx)
429                      : : "ecx", "esi", "edi");
430 }
431 #else
432 #define save_screen()           ((void)0)
433 #define restore_screen()        ((void)0)
434 #endif
435
436 void set_video(void)
437 {
438         u16 mode = boot_params.hdr.vid_mode;
439
440         RESET_HEAP();
441
442         store_mode_params();
443         save_screen();
444         probe_cards(0);
445
446         for (;;) {
447                 if (mode == ASK_VGA)
448                         mode = mode_menu();
449
450                 if (!set_mode(mode))
451                         break;
452
453                 printf("Undefined video mode number: %x\n", mode);
454                 mode = ASK_VGA;
455         }
456         vesa_store_edid();
457         store_mode_params();
458
459         if (do_restore)
460                 restore_screen();
461 }