2 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
3 * Copyright (c) 1992-1998 Søren Schmidt
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer as
11 * the first lines of this file unmodified.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * $Id: vga_isa.c,v 1.9 1999/05/30 11:12:30 dfr Exp $
35 #include "opt_syscons.h" /* should be removed in the future, XXX */
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
43 #include <sys/malloc.h>
48 #include <machine/console.h>
49 #include <machine/md_var.h>
50 #include <machine/pc/bios.h>
52 #include <dev/fb/fbreg.h>
53 #include <dev/fb/vgareg.h>
55 #include <isa/isareg.h>
56 #include <isa/isavar.h>
58 #define DRIVER_NAME "vga"
60 /* cdev driver declaration */
62 #define ISAVGA_UNIT(dev) minor(dev)
63 #define ISAVGA_MKMINOR(unit) (unit)
65 typedef struct isavga_softc {
69 #define ISAVGA_SOFTC(unit) \
70 ((isavga_softc_t *)devclass_get_softc(isavga_devclass, unit))
72 devclass_t isavga_devclass;
74 static int isavga_probe(device_t dev);
75 static int isavga_attach(device_t dev);
77 static device_method_t isavga_methods[] = {
78 DEVMETHOD(device_probe, isavga_probe),
79 DEVMETHOD(device_attach, isavga_attach),
83 static driver_t isavga_driver = {
86 sizeof(isavga_softc_t),
89 DRIVER_MODULE(vga, isa, isavga_driver, isavga_devclass, 0, 0);
91 static int isavga_probe_unit(int unit, isavga_softc_t *sc,
93 static int isavga_attach_unit(int unit, isavga_softc_t *sc,
96 #ifdef FB_INSTALL_CDEV
98 static d_open_t isavgaopen;
99 static d_close_t isavgaclose;
100 static d_read_t isavgaread;
101 static d_ioctl_t isavgaioctl;
103 static struct cdevsw vga_cdevsw = {
104 /* open */ isavgaopen,
105 /* close */ isavgaclose,
108 /* ioctl */ isavgaioctl,
111 /* devtotty */ nodevtotty,
114 /* strategy */ nostrategy,
115 /* name */ DRIVER_NAME,
125 #endif /* FB_INSTALL_CDEV */
128 isavga_probe(device_t dev)
133 if (isa_get_vendorid(dev))
136 device_set_desc(dev, "Generic ISA VGA");
137 sc = device_get_softc(dev);
138 return isavga_probe_unit(device_get_unit(dev), sc, isa_get_flags(dev));
142 isavga_attach(device_t dev)
146 sc = device_get_softc(dev);
147 return isavga_attach_unit(device_get_unit(dev), sc, isa_get_flags(dev));
151 isavga_probe_unit(int unit, isavga_softc_t *sc, int flags)
155 bzero(sc, sizeof(*sc));
156 sw = vid_get_switch(DRIVER_NAME);
159 return (*sw->probe)(unit, &sc->adp, NULL, flags);
163 isavga_attach_unit(int unit, isavga_softc_t *sc, int flags)
168 sw = vid_get_switch(DRIVER_NAME);
172 error = (*sw->init)(unit, sc->adp, flags);
176 #ifdef FB_INSTALL_CDEV
177 /* attach a virtual frame buffer device */
178 error = fb_attach(makedev(0, ISAVGA_MKMINOR(unit)), scp->adp,
182 #endif /* FB_INSTALL_CDEV */
185 (*vidsw[sc->adp->va_index]->diag)(sc->adp, bootverbose);
192 #include <machine/clock.h>
193 #include <machine/pc/vesa.h>
195 #define probe_done(adp) ((adp)->va_flags & V_ADP_PROBED)
196 #define init_done(adp) ((adp)->va_flags & V_ADP_INITIALIZED)
197 #define config_done(adp) ((adp)->va_flags & V_ADP_REGISTERED)
199 /* for compatibility with old kernel options */
200 #ifdef SC_ALT_SEQACCESS
201 #undef SC_ALT_SEQACCESS
202 #undef VGA_ALT_SEQACCESS
203 #define VGA_ALT_SEQACCESS 1
208 #undef VGA_SLOW_IOACCESS
209 #define VGA_SLOW_IOACCESS 1
212 /* architecture dependent option */
214 #define VGA_NO_BIOS 1
217 /* this should really be in `rtc.h' */
218 #define RTC_EQUIPMENT 0x14
221 #define V_MODE_MAP_SIZE (M_VGA_CG320 + 1)
222 #define V_MODE_PARAM_SIZE 64
224 /* video adapter state buffer */
227 #define V_STATE_SIG 0x736f6962
228 u_char regs[V_MODE_PARAM_SIZE];
230 typedef struct adp_state adp_state_t;
232 /* video adapter information */
236 #define DCC_EGAMONO 3
241 * NOTE: `va_window' should have a virtual address, but is initialized
242 * with a physical address in the following table, as verify_adapter()
243 * will perform address conversion at run-time.
245 static video_adapter_t adapter_init_value[] = {
247 { 0, KD_MONO, "mda", 0, 0, 0, IO_MDA, IO_MDASIZE, MONO_CRTC,
248 MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE,
251 { 0, KD_CGA, "cga", 0, 0, V_ADP_COLOR, IO_CGA, IO_CGASIZE, COLOR_CRTC,
252 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE,
255 { 0, KD_CGA, "cga", 0, 0, V_ADP_COLOR, IO_CGA, IO_CGASIZE, COLOR_CRTC,
256 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE,
259 { 0, KD_EGA, "ega", 0, 0, 0, IO_MDA, 48, MONO_CRTC,
260 EGA_BUF_BASE, EGA_BUF_SIZE, MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE,
263 { 0, KD_EGA, "ega", 0, 0, V_ADP_COLOR, IO_MDA, 48, COLOR_CRTC,
264 EGA_BUF_BASE, EGA_BUF_SIZE, CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE,
267 { 0, KD_EGA, "ega", 0, 0, V_ADP_COLOR, IO_MDA, 48, COLOR_CRTC,
268 EGA_BUF_BASE, EGA_BUF_SIZE, CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE,
272 static video_adapter_t biosadapter[2];
273 static int biosadapters = 0;
275 /* video driver declarations */
276 static int vga_configure(int flags);
277 int (*vga_sub_configure)(int flags);
278 static int vga_nop(void);
279 static vi_probe_t vga_probe;
280 static vi_init_t vga_init;
281 static vi_get_info_t vga_get_info;
282 static vi_query_mode_t vga_query_mode;
283 static vi_set_mode_t vga_set_mode;
284 static vi_save_font_t vga_save_font;
285 static vi_load_font_t vga_load_font;
286 static vi_show_font_t vga_show_font;
287 static vi_save_palette_t vga_save_palette;
288 static vi_load_palette_t vga_load_palette;
289 static vi_set_border_t vga_set_border;
290 static vi_save_state_t vga_save_state;
291 static vi_load_state_t vga_load_state;
292 static vi_set_win_org_t vga_set_origin;
293 static vi_read_hw_cursor_t vga_read_hw_cursor;
294 static vi_set_hw_cursor_t vga_set_hw_cursor;
295 static vi_set_hw_cursor_shape_t vga_set_hw_cursor_shape;
296 static vi_mmap_t vga_mmap;
297 static vi_diag_t vga_diag;
299 static video_switch_t vgavidsw = {
316 vga_set_hw_cursor_shape,
317 (vi_blank_display_t *)vga_nop,
322 VIDEO_DRIVER(mda, vgavidsw, NULL);
323 VIDEO_DRIVER(cga, vgavidsw, NULL);
324 VIDEO_DRIVER(ega, vgavidsw, NULL);
325 VIDEO_DRIVER(vga, vgavidsw, vga_configure);
327 /* VGA BIOS standard video modes */
331 static video_info_t bios_vmode[] = {
333 { M_B40x25, V_INFO_COLOR, 40, 25, 8, 8, 2, 1,
334 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0 },
335 { M_C40x25, V_INFO_COLOR, 40, 25, 8, 8, 4, 1,
336 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0 },
337 { M_B80x25, V_INFO_COLOR, 80, 25, 8, 8, 2, 1,
338 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0 },
339 { M_C80x25, V_INFO_COLOR, 80, 25, 8, 8, 4, 1,
340 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0 },
342 { M_ENH_B40x25, V_INFO_COLOR, 40, 25, 8, 14, 2, 1,
343 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0 },
344 { M_ENH_C40x25, V_INFO_COLOR, 40, 25, 8, 14, 4, 1,
345 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0 },
346 { M_ENH_B80x25, V_INFO_COLOR, 80, 25, 8, 14, 2, 1,
347 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0 },
348 { M_ENH_C80x25, V_INFO_COLOR, 80, 25, 8, 14, 4, 1,
349 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0 },
351 { M_VGA_C40x25, V_INFO_COLOR, 40, 25, 8, 16, 4, 1,
352 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0 },
353 { M_VGA_M80x25, 0, 80, 25, 8, 16, 2, 1,
354 MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0 },
355 { M_VGA_C80x25, V_INFO_COLOR, 80, 25, 8, 16, 4, 1,
356 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0 },
358 { M_EGAMONO80x25, 0, 80, 25, 8, 14, 2, 1,
359 MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0 },
361 { M_ENH_B80x43, V_INFO_COLOR, 80, 43, 8, 8, 2, 1,
362 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0 },
363 { M_ENH_C80x43, V_INFO_COLOR, 80, 43, 8, 8, 4, 1,
364 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0 },
366 { M_VGA_M80x30, 0, 80, 30, 8, 16, 2, 1,
367 MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0 },
368 { M_VGA_C80x30, V_INFO_COLOR, 80, 30, 8, 16, 4, 1,
369 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0 },
370 { M_VGA_M80x50, 0, 80, 50, 8, 8, 2, 1,
371 MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0 },
372 { M_VGA_C80x50, V_INFO_COLOR, 80, 50, 8, 8, 4, 1,
373 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0 },
374 { M_VGA_M80x60, 0, 80, 60, 8, 8, 2, 1,
375 MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0 },
376 { M_VGA_C80x60, V_INFO_COLOR, 80, 60, 8, 8, 4, 1,
377 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0 },
378 #ifndef VGA_NO_MODE_CHANGE
380 { M_BG320, V_INFO_COLOR | V_INFO_GRAPHICS, 320, 200, 8, 8, 2, 1,
381 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0 },
382 { M_CG320, V_INFO_COLOR | V_INFO_GRAPHICS, 320, 200, 8, 8, 2, 1,
383 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0 },
384 { M_BG640, V_INFO_COLOR | V_INFO_GRAPHICS, 640, 200, 8, 8, 1, 1,
385 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0 },
387 { M_CG320_D, V_INFO_COLOR | V_INFO_GRAPHICS, 320, 200, 8, 8, 4, 4,
388 GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 },
389 { M_CG640_E, V_INFO_COLOR | V_INFO_GRAPHICS, 640, 200, 8, 8, 4, 4,
390 GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 },
391 { M_EGAMONOAPA, V_INFO_GRAPHICS, 640, 350, 8, 14, 4, 4,
392 GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, 64*1024, 0, 0 },
393 { M_ENHMONOAPA2,V_INFO_GRAPHICS, 640, 350, 8, 14, 4, 4,
394 GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 },
395 { M_CG640x350, V_INFO_COLOR | V_INFO_GRAPHICS, 640, 350, 8, 14, 2, 2,
396 GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 },
397 { M_ENH_CG640, V_INFO_COLOR | V_INFO_GRAPHICS, 640, 350, 8, 14, 4, 4,
398 GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 },
400 { M_BG640x480, V_INFO_COLOR | V_INFO_GRAPHICS, 640, 480, 8, 16, 4, 4,
401 GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 },
402 { M_CG640x480, V_INFO_COLOR | V_INFO_GRAPHICS, 640, 480, 8, 16, 4, 4,
403 GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 },
404 { M_VGA_CG320, V_INFO_COLOR | V_INFO_GRAPHICS, 320, 200, 8, 8, 8, 1,
405 GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 },
406 { M_VGA_MODEX, V_INFO_COLOR | V_INFO_GRAPHICS, 320, 240, 8, 8, 8, 1,
407 GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 },
408 #endif /* VGA_NO_MODE_CHANGE */
413 static int init_done = FALSE;
414 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
415 static u_char *video_mode_ptr = NULL; /* EGA/VGA */
416 static u_char *video_mode_ptr2 = NULL; /* CGA/MDA */
418 static u_char *mode_map[V_MODE_MAP_SIZE];
419 static adp_state_t adpstate;
420 static adp_state_t adpstate2;
421 static int rows_offset = 1;
423 /* local macros and functions */
424 #define BIOS_SADDRTOLADDR(p) ((((p) & 0xffff0000) >> 12) + ((p) & 0x0000ffff))
426 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
427 static void map_mode_table(u_char *map[], u_char *table, int max);
429 static void clear_mode_map(video_adapter_t *adp, u_char *map[], int max,
431 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
432 static int map_mode_num(int mode);
434 static int map_gen_mode_num(int type, int color, int mode);
435 static int map_bios_mode_num(int type, int color, int bios_mode);
436 static u_char *get_mode_param(int mode);
438 static void fill_adapter_param(int code, video_adapter_t *adp);
440 static int verify_adapter(video_adapter_t *adp);
441 static void update_adapter_info(video_adapter_t *adp, video_info_t *info);
442 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
443 #define COMP_IDENTICAL 0
444 #define COMP_SIMILAR 1
445 #define COMP_DIFFERENT 2
446 static int comp_adpregs(u_char *buf1, u_char *buf2);
448 static int probe_adapters(void);
450 #ifndef VGA_NO_FONT_LOADING
451 #define PARAM_BUFSIZE 6
452 static void set_font_mode(video_adapter_t *adp, u_char *buf);
453 static void set_normal_mode(video_adapter_t *adp, u_char *buf);
456 static void dump_buffer(u_char *buf, size_t len);
458 #define ISMAPPED(pa, width) \
459 (((pa) <= (u_long)0x1000 - (width)) \
460 || ((pa) >= ISA_HOLE_START && (pa) <= 0x100000 - (width)))
462 #define prologue(adp, flag, err) \
463 if (!init_done || !((adp)->va_flags & (flag))) \
466 /* a backdoor for the console driver */
468 vga_configure(int flags)
473 for (i = 0; i < biosadapters; ++i) {
474 if (!probe_done(&biosadapter[i]))
476 biosadapter[i].va_flags |= V_ADP_INITIALIZED;
477 if (!config_done(&biosadapter[i])) {
478 if (vid_register(&biosadapter[i]) < 0)
480 biosadapter[i].va_flags |= V_ADP_REGISTERED;
483 if (vga_sub_configure != NULL)
484 (*vga_sub_configure)(flags);
489 /* local subroutines */
491 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
492 /* construct the mode parameter map */
494 map_mode_table(u_char *map[], u_char *table, int max)
498 for(i = 0; i < max; ++i)
499 map[i] = table + i*V_MODE_PARAM_SIZE;
500 for(; i < V_MODE_MAP_SIZE; ++i)
503 #endif /* !VGA_NO_BIOS && !VGA_NO_MODE_CHANGE */
506 clear_mode_map(video_adapter_t *adp, u_char *map[], int max, int color)
512 * NOTE: we don't touch `bios_vmode[]' because it is shared
515 for(i = 0; i < max; ++i) {
516 if (vga_get_info(adp, i, &info))
518 if ((info.vi_flags & V_INFO_COLOR) != color)
523 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
524 /* map the non-standard video mode to a known mode number */
526 map_mode_num(int mode)
532 { M_ENH_B80x43, M_ENH_B80x25 },
533 { M_ENH_C80x43, M_ENH_C80x25 },
534 { M_VGA_M80x30, M_VGA_M80x25 },
535 { M_VGA_C80x30, M_VGA_C80x25 },
536 { M_VGA_M80x50, M_VGA_M80x25 },
537 { M_VGA_C80x50, M_VGA_C80x25 },
538 { M_VGA_M80x60, M_VGA_M80x25 },
539 { M_VGA_C80x60, M_VGA_C80x25 },
540 { M_VGA_MODEX, M_VGA_CG320 },
544 for (i = 0; i < sizeof(mode_map)/sizeof(mode_map[0]); ++i) {
545 if (mode_map[i].from == mode)
546 return mode_map[i].to;
550 #endif /* !VGA_NO_BIOS && !VGA_NO_MODE_CHANGE */
552 /* map a generic video mode to a known mode number */
554 map_gen_mode_num(int type, int color, int mode)
561 { M_TEXT_80x30, M_VGA_C80x30, M_VGA_M80x30, },
562 { M_TEXT_80x43, M_ENH_C80x43, M_ENH_B80x43, },
563 { M_TEXT_80x50, M_VGA_C80x50, M_VGA_M80x50, },
564 { M_TEXT_80x60, M_VGA_C80x60, M_VGA_M80x60, },
568 if (mode == M_TEXT_80x25) {
582 return M_EGAMONO80x25;
590 return M_EGAMONO80x25; /* XXX: this name is confusing */
597 for (i = 0; i < sizeof(mode_map)/sizeof(mode_map[0]); ++i) {
598 if (mode_map[i].from == mode)
599 return ((color) ? mode_map[i].to_color : mode_map[i].to_mono);
604 /* turn the BIOS video number into our video mode number */
606 map_bios_mode_num(int type, int color, int bios_mode)
608 static int cga_modes[7] = {
609 M_B40x25, M_C40x25, /* 0, 1 */
610 M_B80x25, M_C80x25, /* 2, 3 */
614 static int ega_modes[17] = {
615 M_ENH_B40x25, M_ENH_C40x25, /* 0, 1 */
616 M_ENH_B80x25, M_ENH_C80x25, /* 2, 3 */
619 M_EGAMONO80x25, /* 7 */
623 M_ENHMONOAPA2, /* XXX: video momery > 64K */
624 M_ENH_CG640, /* XXX: video momery > 64K */
626 static int vga_modes[20] = {
627 M_VGA_C40x25, M_VGA_C40x25, /* 0, 1 */
628 M_VGA_C80x25, M_VGA_C80x25, /* 2, 3 */
631 M_VGA_M80x25, /* 7 */
637 M_BG640x480, M_CG640x480,
644 if (bios_mode < sizeof(vga_modes)/sizeof(vga_modes[0]))
645 return vga_modes[bios_mode];
653 if (bios_mode < sizeof(ega_modes)/sizeof(ega_modes[0]))
654 return ega_modes[bios_mode];
658 return M_EGAMONO80x25;
662 if (bios_mode < sizeof(cga_modes)/sizeof(cga_modes[0]))
663 return cga_modes[bios_mode];
670 return M_EGAMONO80x25; /* XXX: this name is confusing */
678 /* look up a parameter table entry */
680 *get_mode_param(int mode)
682 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
683 if (mode >= V_MODE_MAP_SIZE)
684 mode = map_mode_num(mode);
686 if ((mode >= 0) && (mode < V_MODE_MAP_SIZE))
687 return mode_map[mode];
694 fill_adapter_param(int code, video_adapter_t *adp)
700 { DCC_MONO, DCC_EGA40 /* CGA monitor */ },
701 { DCC_MONO, DCC_EGA80 /* CGA monitor */ },
702 { DCC_MONO, DCC_EGA80 /* CGA emulation */ },
703 { DCC_MONO, DCC_EGA80 },
704 { DCC_CGA40, DCC_EGAMONO },
705 { DCC_CGA80, DCC_EGAMONO },
706 { DCC_EGA40 /* CGA monitor */, DCC_MONO},
707 { DCC_EGA80 /* CGA monitor */, DCC_MONO},
708 { DCC_EGA80 /* CGA emulation */,DCC_MONO },
709 { DCC_EGA80, DCC_MONO },
710 { DCC_EGAMONO, DCC_CGA40 },
711 { DCC_EGAMONO, DCC_CGA40 },
714 if ((code < 0) || (code >= sizeof(dcc)/sizeof(dcc[0]))) {
715 adp[V_ADP_PRIMARY] = adapter_init_value[DCC_MONO];
716 adp[V_ADP_SECONDARY] = adapter_init_value[DCC_CGA80];
718 adp[V_ADP_PRIMARY] = adapter_init_value[dcc[code].primary];
719 adp[V_ADP_SECONDARY] = adapter_init_value[dcc[code].secondary];
722 #endif /* VGA_NO_BIOS */
725 verify_adapter(video_adapter_t *adp)
729 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
733 buf = BIOS_PADDRTOVADDR(adp->va_window);
736 if (readw(buf) != 0xA55A)
740 switch (adp->va_type) {
743 outb(adp->va_crtc_addr, 7);
744 if (inb(adp->va_crtc_addr) == 7) {
745 adp->va_type = KD_VGA;
746 adp->va_name = "vga";
747 adp->va_flags |= V_ADP_STATESAVE | V_ADP_PALETTE;
749 adp->va_flags |= V_ADP_STATELOAD | V_ADP_BORDER;
750 /* the color adapter may be in the 40x25 mode... XXX */
752 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
753 /* get the BIOS video mode pointer */
754 p = *(u_int32_t *)BIOS_PADDRTOVADDR(0x4a8);
755 p = BIOS_SADDRTOLADDR(p);
756 if (ISMAPPED(p, sizeof(u_int32_t))) {
757 p = *(u_int32_t *)BIOS_PADDRTOVADDR(p);
758 p = BIOS_SADDRTOLADDR(p);
759 if (ISMAPPED(p, V_MODE_PARAM_SIZE))
760 video_mode_ptr = (u_char *)BIOS_PADDRTOVADDR(p);
766 adp->va_flags |= V_ADP_COLOR | V_ADP_BORDER;
767 /* may be in the 40x25 mode... XXX */
768 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
769 /* get the BIOS video mode pointer */
770 p = *(u_int32_t *)BIOS_PADDRTOVADDR(0x1d*4);
771 p = BIOS_SADDRTOLADDR(p);
772 video_mode_ptr2 = (u_char *)BIOS_PADDRTOVADDR(p);
777 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
778 /* get the BIOS video mode pointer */
779 p = *(u_int32_t *)BIOS_PADDRTOVADDR(0x1d*4);
780 p = BIOS_SADDRTOLADDR(p);
781 video_mode_ptr2 = (u_char *)BIOS_PADDRTOVADDR(p);
790 update_adapter_info(video_adapter_t *adp, video_info_t *info)
792 adp->va_flags &= ~V_ADP_COLOR;
794 (info->vi_flags & V_INFO_COLOR) ? V_ADP_COLOR : 0;
796 (adp->va_flags & V_ADP_COLOR) ? COLOR_CRTC : MONO_CRTC;
797 adp->va_window = BIOS_PADDRTOVADDR(info->vi_window);
798 adp->va_window_size = info->vi_window_size;
799 adp->va_window_gran = info->vi_window_gran;
800 if (info->vi_buffer_size == 0) {
802 adp->va_buffer_size = 0;
804 adp->va_buffer = BIOS_PADDRTOVADDR(info->vi_buffer);
805 adp->va_buffer_size = info->vi_buffer_size;
807 if (info->vi_flags & V_INFO_GRAPHICS) {
808 switch (info->vi_depth/info->vi_planes) {
810 adp->va_line_width = info->vi_width/8;
813 adp->va_line_width = info->vi_width/4;
816 adp->va_line_width = info->vi_width/2;
819 default: /* shouldn't happen */
820 adp->va_line_width = info->vi_width;
824 adp->va_line_width = info->vi_width;
826 bcopy(info, &adp->va_info, sizeof(adp->va_info));
829 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
830 /* compare two parameter table entries */
832 comp_adpregs(u_char *buf1, u_char *buf2)
836 } params[V_MODE_PARAM_SIZE] = {
837 {0xff}, {0x00}, {0xff}, /* COLS, ROWS, POINTS */
838 {0x00}, {0x00}, /* page length */
839 {0xfe}, {0xff}, {0xff}, {0xff}, /* sequencer registers */
840 {0xf3}, /* misc register */
841 {0xff}, {0xff}, {0xff}, {0x7f}, {0xff}, /* CRTC */
842 {0xff}, {0xff}, {0xff}, {0x7f}, {0xff},
843 {0x00}, {0x00}, {0x00}, {0x00}, {0x00},
844 {0x00}, {0xff}, {0x7f}, {0xff}, {0xff},
845 {0x7f}, {0xff}, {0xff}, {0xef}, {0xff},
846 {0xff}, {0xff}, {0xff}, {0xff}, {0xff}, /* attribute controller regs */
847 {0xff}, {0xff}, {0xff}, {0xff}, {0xff},
848 {0xff}, {0xff}, {0xff}, {0xff}, {0xff},
849 {0xff}, {0xff}, {0xff}, {0xff}, {0xf0},
850 {0xff}, {0xff}, {0xff}, {0xff}, {0xff}, /* GDC register */
851 {0xff}, {0xff}, {0xff}, {0xff},
853 int identical = TRUE;
856 if ((buf1 == NULL) || (buf2 == NULL))
857 return COMP_DIFFERENT;
859 for (i = 0; i < sizeof(params)/sizeof(params[0]); ++i) {
860 if (params[i].mask == 0) /* don't care */
862 if ((buf1[i] & params[i].mask) != (buf2[i] & params[i].mask))
863 return COMP_DIFFERENT;
864 if (buf1[i] != buf2[i])
867 return (identical) ? COMP_IDENTICAL : COMP_SIMILAR;
869 #endif /* !VGA_NO_BIOS && !VGA_NO_MODE_CHANGE */
871 /* probe video adapters and return the number of detected adapters */
875 video_adapter_t *adp;
879 /* do this test only once */
885 * Locate display adapters.
886 * The AT architecture supports upto two adapters. `syscons' allows
887 * the following combinations of adapters:
889 * 2) MDA + EGA/VGA color
890 * 3) CGA + EGA/VGA mono
891 * Note that `syscons' doesn't bother with MCGA as it is only
892 * avaiable for low end PS/2 models which has 80286 or earlier CPUs,
893 * thus, they are not running FreeBSD!
894 * When there are two adapaters in the system, one becomes `primary'
895 * and the other `secondary'. The EGA adapter has a set of DIP
896 * switches on board for this information and the EGA BIOS copies
897 * it in the BIOS data area BIOSDATA_VIDEOSWITCH (40:88).
898 * The VGA BIOS has more sophisticated mechanism and has this
899 * information in BIOSDATA_DCCINDEX (40:8a), but it also maintains
900 * compatibility with the EGA BIOS by updating BIOSDATA_VIDEOSWITCH.
904 * Check rtc and BIOS data area.
905 * XXX: we don't use BIOSDATA_EQUIPMENT, since it is not a dead
906 * copy of RTC_EQUIPMENT. Bits 4 and 5 of ETC_EQUIPMENT are
907 * zeros for EGA and VGA. However, the EGA/VGA BIOS sets
908 * these bits in BIOSDATA_EQUIPMENT according to the monitor
912 switch ((rtcin(RTC_EQUIPMENT) >> 4) & 3) { /* bit 4 and 5 */
915 fill_adapter_param(readb(BIOS_PADDRTOVADDR(0x488)) & 0x0f,
920 /* FIXME: switch to the 80x25 mode? XXX */
921 biosadapter[V_ADP_PRIMARY] = adapter_init_value[DCC_CGA40];
922 biosadapter[V_ADP_SECONDARY] = adapter_init_value[DCC_MONO];
926 biosadapter[V_ADP_PRIMARY] = adapter_init_value[DCC_CGA80];
927 biosadapter[V_ADP_SECONDARY] = adapter_init_value[DCC_MONO];
931 biosadapter[V_ADP_PRIMARY] = adapter_init_value[DCC_MONO];
932 biosadapter[V_ADP_SECONDARY] = adapter_init_value[DCC_CGA80];
936 /* assume EGA/VGA? XXX */
937 biosadapter[V_ADP_PRIMARY] = adapter_init_value[DCC_EGA80];
938 biosadapter[V_ADP_SECONDARY] = adapter_init_value[DCC_MONO];
939 #endif /* VGA_NO_BIOS */
942 if (verify_adapter(&biosadapter[V_ADP_SECONDARY]) == 0) {
944 biosadapter[V_ADP_SECONDARY].va_flags |= V_ADP_PROBED;
945 biosadapter[V_ADP_SECONDARY].va_mode =
946 biosadapter[V_ADP_SECONDARY].va_initial_mode =
947 map_bios_mode_num(biosadapter[V_ADP_SECONDARY].va_type,
948 biosadapter[V_ADP_SECONDARY].va_flags
950 biosadapter[V_ADP_SECONDARY].va_initial_bios_mode);
952 biosadapter[V_ADP_SECONDARY].va_type = -1;
954 if (verify_adapter(&biosadapter[V_ADP_PRIMARY]) == 0) {
956 biosadapter[V_ADP_PRIMARY].va_flags |= V_ADP_PROBED;
958 biosadapter[V_ADP_PRIMARY].va_initial_bios_mode =
959 readb(BIOS_PADDRTOVADDR(0x449));
961 biosadapter[V_ADP_PRIMARY].va_initial_bios_mode = 3; /* XXX */
963 biosadapter[V_ADP_PRIMARY].va_mode =
964 biosadapter[V_ADP_PRIMARY].va_initial_mode =
965 map_bios_mode_num(biosadapter[V_ADP_PRIMARY].va_type,
966 biosadapter[V_ADP_PRIMARY].va_flags & V_ADP_COLOR,
967 biosadapter[V_ADP_PRIMARY].va_initial_bios_mode);
969 biosadapter[V_ADP_PRIMARY] = biosadapter[V_ADP_SECONDARY];
970 biosadapter[V_ADP_SECONDARY].va_type = -1;
972 if (biosadapters == 0)
974 biosadapter[V_ADP_PRIMARY].va_unit = V_ADP_PRIMARY;
975 biosadapter[V_ADP_SECONDARY].va_unit = V_ADP_SECONDARY;
977 #if 0 /* we don't need these... */
978 fb_init_struct(&biosadapter[V_ADP_PRIMARY], ...);
979 fb_init_struct(&biosadapter[V_ADP_SECONDARY], ...);
984 * We cannot have two video adapter of the same type; there must be
985 * only one of color or mono adapter, or one each of them.
987 if (biosadapters > 1) {
988 if (!((biosadapter[0].va_flags ^ biosadapter[1].va_flags)
990 /* we have two mono or color adapters!! */
991 return (biosadapters = 0);
996 * Ensure a zero start address. This is mainly to recover after
997 * switching from pcvt using userconfig(). The registers are w/o
998 * for old hardware so it's too hard to relocate the active screen
1000 * This must be done before vga_save_state() for VGA.
1002 outb(biosadapter[V_ADP_PRIMARY].va_crtc_addr, 12);
1003 outb(biosadapter[V_ADP_PRIMARY].va_crtc_addr + 1, 0);
1004 outb(biosadapter[V_ADP_PRIMARY].va_crtc_addr, 13);
1005 outb(biosadapter[V_ADP_PRIMARY].va_crtc_addr + 1, 0);
1007 /* the video mode parameter table in EGA/VGA BIOS */
1008 /* NOTE: there can be only one EGA/VGA, wheather color or mono,
1009 * recognized by the video BIOS.
1011 if ((biosadapter[V_ADP_PRIMARY].va_type == KD_EGA) ||
1012 (biosadapter[V_ADP_PRIMARY].va_type == KD_VGA)) {
1013 adp = &biosadapter[V_ADP_PRIMARY];
1014 } else if ((biosadapter[V_ADP_SECONDARY].va_type == KD_EGA) ||
1015 (biosadapter[V_ADP_SECONDARY].va_type == KD_VGA)) {
1016 adp = &biosadapter[V_ADP_SECONDARY];
1020 bzero(mode_map, sizeof(mode_map));
1022 if (adp->va_type == KD_VGA) {
1023 vga_save_state(adp, &adpstate, sizeof(adpstate));
1024 #if defined(VGA_NO_BIOS) || defined(VGA_NO_MODE_CHANGE)
1025 mode_map[adp->va_initial_mode] = adpstate.regs;
1027 #else /* VGA_NO_BIOS || VGA_NO_MODE_CHANGE */
1028 if (video_mode_ptr == NULL) {
1029 mode_map[adp->va_initial_mode] = adpstate.regs;
1032 /* discard the table if we are not familiar with it... */
1034 map_mode_table(mode_map, video_mode_ptr, M_VGA_CG320 + 1);
1035 mp = get_mode_param(adp->va_initial_mode);
1037 bcopy(mp, adpstate2.regs, sizeof(adpstate2.regs));
1038 switch (comp_adpregs(adpstate.regs, mp)) {
1039 case COMP_IDENTICAL:
1041 * OK, this parameter table looks reasonably familiar
1045 * This is a kludge for Toshiba DynaBook SS433
1046 * whose BIOS video mode table entry has the actual #
1047 * of rows at the offset 1; BIOSes from other
1048 * manufacturers store the # of rows - 1 there. XXX
1050 rows_offset = adpstate.regs[1] + 1 - mp[1];
1055 * Not exactly the same, but similar enough to be
1056 * trusted. However, use the saved register values
1057 * for the initial mode and other modes which are
1058 * based on the initial mode.
1060 mode_map[adp->va_initial_mode] = adpstate.regs;
1061 rows_offset = adpstate.regs[1] + 1 - mp[1];
1062 adpstate.regs[1] -= rows_offset - 1;
1065 case COMP_DIFFERENT:
1068 * Don't use the paramter table in BIOS. It doesn't
1069 * look familiar to us. Video mode switching is allowed
1070 * only if the new mode is the same as or based on
1073 video_mode_ptr = NULL;
1074 bzero(mode_map, sizeof(mode_map));
1075 mode_map[adp->va_initial_mode] = adpstate.regs;
1080 #endif /* VGA_NO_BIOS || VGA_NO_MODE_CHANGE */
1082 #ifndef VGA_NO_MODE_CHANGE
1083 adp->va_flags |= V_ADP_MODECHANGE;
1085 #ifndef VGA_NO_FONT_LOADING
1086 adp->va_flags |= V_ADP_FONT;
1088 } else if (adp->va_type == KD_EGA) {
1089 #if defined(VGA_NO_BIOS) || defined(VGA_NO_MODE_CHANGE)
1091 #else /* VGA_NO_BIOS || VGA_NO_MODE_CHANGE */
1092 if (video_mode_ptr == NULL) {
1096 map_mode_table(mode_map, video_mode_ptr, M_ENH_C80x25 + 1);
1097 /* XXX how can one validate the EGA table... */
1098 mp = get_mode_param(adp->va_initial_mode);
1100 adp->va_flags |= V_ADP_MODECHANGE;
1101 #ifndef VGA_NO_FONT_LOADING
1102 adp->va_flags |= V_ADP_FONT;
1107 * This is serious. We will not be able to switch video
1110 video_mode_ptr = NULL;
1111 bzero(mode_map, sizeof(mode_map));
1115 #endif /* VGA_NO_BIOS || VGA_NO_MODE_CHANGE */
1119 /* remove conflicting modes if we have more than one adapter */
1120 if (biosadapters > 1) {
1121 for (i = 0; i < biosadapters; ++i) {
1122 if (!(biosadapter[i].va_flags & V_ADP_MODECHANGE))
1124 clear_mode_map(&biosadapter[i], mode_map, M_VGA_CG320 + 1,
1125 (biosadapter[i].va_flags & V_ADP_COLOR) ?
1127 if ((biosadapter[i].va_type == KD_VGA)
1128 || (biosadapter[i].va_type == KD_EGA)) {
1129 biosadapter[i].va_io_base =
1130 (biosadapter[i].va_flags & V_ADP_COLOR) ?
1132 biosadapter[i].va_io_size = 32;
1137 /* buffer address */
1138 vga_get_info(&biosadapter[V_ADP_PRIMARY],
1139 biosadapter[V_ADP_PRIMARY].va_initial_mode, &info);
1140 update_adapter_info(&biosadapter[V_ADP_PRIMARY], &info);
1142 if (biosadapters > 1) {
1143 vga_get_info(&biosadapter[V_ADP_SECONDARY],
1144 biosadapter[V_ADP_SECONDARY].va_initial_mode, &info);
1145 update_adapter_info(&biosadapter[V_ADP_SECONDARY], &info);
1149 * XXX: we should verify the following values for the primary adapter...
1150 * crtc I/O port address: *(u_int16_t *)BIOS_PADDRTOVADDR(0x463);
1151 * color/mono display: (*(u_int8_t *)BIOS_PADDRTOVADDR(0x487) & 0x02)
1152 * ? 0 : V_ADP_COLOR;
1153 * columns: *(u_int8_t *)BIOS_PADDRTOVADDR(0x44a);
1154 * rows: *(u_int8_t *)BIOS_PADDRTOVADDR(0x484);
1155 * font size: *(u_int8_t *)BIOS_PADDRTOVADDR(0x485);
1156 * buffer size: *(u_int16_t *)BIOS_PADDRTOVADDR(0x44c);
1159 return biosadapters;
1171 vga_probe(int unit, video_adapter_t **adpp, void *arg, int flags)
1174 if (unit >= biosadapters)
1177 *adpp = &biosadapter[unit];
1183 vga_init(int unit, video_adapter_t *adp, int flags)
1185 if ((unit >= biosadapters) || (adp == NULL) || !probe_done(adp))
1188 if (!init_done(adp)) {
1189 /* nothing to do really... */
1190 adp->va_flags |= V_ADP_INITIALIZED;
1193 if (!config_done(adp)) {
1194 if (vid_register(adp) < 0)
1196 adp->va_flags |= V_ADP_REGISTERED;
1198 if (vga_sub_configure != NULL)
1199 (*vga_sub_configure)(0);
1206 * Return the video_info structure of the requested video mode.
1211 vga_get_info(video_adapter_t *adp, int mode, video_info_t *info)
1218 mode = map_gen_mode_num(adp->va_type, adp->va_flags & V_ADP_COLOR, mode);
1219 #ifndef VGA_NO_MODE_CHANGE
1220 if (adp->va_flags & V_ADP_MODECHANGE) {
1222 * If the parameter table entry for this mode is not found,
1223 * the mode is not supported...
1225 if (get_mode_param(mode) == NULL)
1228 #endif /* VGA_NO_MODE_CHANGE */
1231 * Even if we don't support video mode switching on this adapter,
1232 * the information on the initial (thus current) video mode
1233 * should be made available.
1235 if (mode != adp->va_initial_mode)
1239 for (i = 0; bios_vmode[i].vi_mode != EOT; ++i) {
1240 if (bios_vmode[i].vi_mode == NA)
1242 if (mode == bios_vmode[i].vi_mode) {
1243 *info = bios_vmode[i];
1252 * Find a video mode matching the requested parameters.
1253 * Fields filled with 0 are considered "don't care" fields and
1259 vga_query_mode(video_adapter_t *adp, video_info_t *info)
1267 for (i = 0; bios_vmode[i].vi_mode != EOT; ++i) {
1268 if (bios_vmode[i].vi_mode == NA)
1271 if ((info->vi_width != 0)
1272 && (info->vi_width != bios_vmode[i].vi_width))
1274 if ((info->vi_height != 0)
1275 && (info->vi_height != bios_vmode[i].vi_height))
1277 if ((info->vi_cwidth != 0)
1278 && (info->vi_cwidth != bios_vmode[i].vi_cwidth))
1280 if ((info->vi_cheight != 0)
1281 && (info->vi_cheight != bios_vmode[i].vi_cheight))
1283 if ((info->vi_depth != 0)
1284 && (info->vi_depth != bios_vmode[i].vi_depth))
1286 if ((info->vi_planes != 0)
1287 && (info->vi_planes != bios_vmode[i].vi_planes))
1289 /* XXX: should check pixel format, memory model */
1290 if ((info->vi_flags != 0)
1291 && (info->vi_flags != bios_vmode[i].vi_flags))
1294 /* verify if this mode is supported on this adapter */
1295 if (vga_get_info(adp, bios_vmode[i].vi_mode, &buf))
1297 return bios_vmode[i].vi_mode;
1304 * Change the video mode.
1309 vga_set_mode(video_adapter_t *adp, int mode)
1311 #ifndef VGA_NO_MODE_CHANGE
1315 prologue(adp, V_ADP_MODECHANGE, 1);
1317 mode = map_gen_mode_num(adp->va_type,
1318 adp->va_flags & V_ADP_COLOR, mode);
1319 if (vga_get_info(adp, mode, &info))
1321 params.sig = V_STATE_SIG;
1322 bcopy(get_mode_param(mode), params.regs, sizeof(params.regs));
1325 case M_VGA_C80x60: case M_VGA_M80x60:
1326 params.regs[2] = 0x08;
1327 params.regs[19] = 0x47;
1330 case M_VGA_C80x30: case M_VGA_M80x30:
1331 params.regs[19] = 0x4f;
1333 params.regs[9] |= 0xc0;
1334 params.regs[16] = 0x08;
1335 params.regs[17] = 0x3e;
1336 params.regs[26] = 0xea;
1337 params.regs[28] = 0xdf;
1338 params.regs[31] = 0xe7;
1339 params.regs[32] = 0x04;
1342 case M_ENH_C80x43: case M_ENH_B80x43:
1343 params.regs[28] = 87;
1346 case M_VGA_C80x50: case M_VGA_M80x50:
1349 params.regs[19] = 7;
1352 case M_VGA_C40x25: case M_VGA_C80x25:
1354 case M_B40x25: case M_C40x25:
1355 case M_B80x25: case M_C80x25:
1356 case M_ENH_B40x25: case M_ENH_C40x25:
1357 case M_ENH_B80x25: case M_ENH_C80x25:
1358 case M_EGAMONO80x25:
1361 vga_load_state(adp, ¶ms);
1365 /* "unchain" the VGA mode */
1366 params.regs[5-1+0x04] &= 0xf7;
1367 params.regs[5-1+0x04] |= 0x04;
1368 /* turn off doubleword mode */
1369 params.regs[10+0x14] &= 0xbf;
1370 /* turn off word adressing */
1371 params.regs[10+0x17] |= 0x40;
1372 /* set logical screen width */
1373 params.regs[10+0x13] = 80;
1375 params.regs[10+0x11] = 0x2c;
1376 params.regs[10+0x06] = 0x0d;
1377 params.regs[10+0x07] = 0x3e;
1378 params.regs[10+0x10] = 0xea;
1379 params.regs[10+0x11] = 0xac;
1380 params.regs[10+0x12] = 0xdf;
1381 params.regs[10+0x15] = 0xe7;
1382 params.regs[10+0x16] = 0x06;
1383 /* set vertical sync polarity to reflect aspect ratio */
1384 params.regs[9] = 0xe3;
1387 case M_BG320: case M_CG320: case M_BG640:
1388 case M_CG320_D: case M_CG640_E:
1389 case M_CG640x350: case M_ENH_CG640:
1390 case M_BG640x480: case M_CG640x480: case M_VGA_CG320:
1393 vga_load_state(adp, ¶ms);
1400 adp->va_mode = mode;
1401 update_adapter_info(adp, &info);
1403 /* move hardware cursor out of the way */
1404 (*vidsw[adp->va_index]->set_hw_cursor)(adp, -1, -1);
1407 #else /* VGA_NO_MODE_CHANGE */
1409 #endif /* VGA_NO_MODE_CHANGE */
1412 #ifndef VGA_NO_FONT_LOADING
1415 set_font_mode(video_adapter_t *adp, u_char *buf)
1422 /* save register values */
1423 if (adp->va_type == KD_VGA) {
1424 outb(TSIDX, 0x02); buf[0] = inb(TSREG);
1425 outb(TSIDX, 0x04); buf[1] = inb(TSREG);
1426 outb(GDCIDX, 0x04); buf[2] = inb(GDCREG);
1427 outb(GDCIDX, 0x05); buf[3] = inb(GDCREG);
1428 outb(GDCIDX, 0x06); buf[4] = inb(GDCREG);
1429 inb(adp->va_crtc_addr + 6);
1430 outb(ATC, 0x10); buf[5] = inb(ATC + 1);
1431 } else /* if (adp->va_type == KD_EGA) */ {
1433 * EGA cannot be read; copy parameters from the mode parameter
1436 mp = get_mode_param(adp->va_mode);
1437 buf[0] = mp[5 + 0x02 - 1];
1438 buf[1] = mp[5 + 0x04 - 1];
1439 buf[2] = mp[55 + 0x04];
1440 buf[3] = mp[55 + 0x05];
1441 buf[4] = mp[55 + 0x06];
1442 buf[5] = mp[35 + 0x10];
1445 /* setup vga for loading fonts */
1446 inb(adp->va_crtc_addr + 6); /* reset flip-flop */
1447 outb(ATC, 0x10); outb(ATC, buf[5] & ~0x01);
1448 inb(adp->va_crtc_addr + 6); /* reset flip-flop */
1449 outb(ATC, 0x20); /* enable palette */
1451 #if VGA_SLOW_IOACCESS
1452 #ifdef VGA_ALT_SEQACCESS
1453 outb(TSIDX, 0x00); outb(TSREG, 0x01);
1455 outb(TSIDX, 0x02); outb(TSREG, 0x04);
1456 outb(TSIDX, 0x04); outb(TSREG, 0x07);
1457 #ifdef VGA_ALT_SEQACCESS
1458 outb(TSIDX, 0x00); outb(TSREG, 0x03);
1460 outb(GDCIDX, 0x04); outb(GDCREG, 0x02);
1461 outb(GDCIDX, 0x05); outb(GDCREG, 0x00);
1462 outb(GDCIDX, 0x06); outb(GDCREG, 0x04);
1463 #else /* VGA_SLOW_IOACCESS */
1464 #ifdef VGA_ALT_SEQACCESS
1465 outw(TSIDX, 0x0100);
1467 outw(TSIDX, 0x0402);
1468 outw(TSIDX, 0x0704);
1469 #ifdef VGA_ALT_SEQACCESS
1470 outw(TSIDX, 0x0300);
1472 outw(GDCIDX, 0x0204);
1473 outw(GDCIDX, 0x0005);
1474 outw(GDCIDX, 0x0406); /* addr = a0000, 64kb */
1475 #endif /* VGA_SLOW_IOACCESS */
1481 set_normal_mode(video_adapter_t *adp, u_char *buf)
1487 /* setup vga for normal operation mode again */
1488 inb(adp->va_crtc_addr + 6); /* reset flip-flop */
1489 outb(ATC, 0x10); outb(ATC, buf[5]);
1490 inb(adp->va_crtc_addr + 6); /* reset flip-flop */
1491 outb(ATC, 0x20); /* enable palette */
1493 #if VGA_SLOW_IOACCESS
1494 #ifdef VGA_ALT_SEQACCESS
1495 outb(TSIDX, 0x00); outb(TSREG, 0x01);
1497 outb(TSIDX, 0x02); outb(TSREG, buf[0]);
1498 outb(TSIDX, 0x04); outb(TSREG, buf[1]);
1499 #ifdef VGA_ALT_SEQACCESS
1500 outb(TSIDX, 0x00); outb(TSREG, 0x03);
1502 outb(GDCIDX, 0x04); outb(GDCREG, buf[2]);
1503 outb(GDCIDX, 0x05); outb(GDCREG, buf[3]);
1504 if (adp->va_crtc_addr == MONO_CRTC) {
1505 outb(GDCIDX, 0x06); outb(GDCREG,(buf[4] & 0x03) | 0x08);
1507 outb(GDCIDX, 0x06); outb(GDCREG,(buf[4] & 0x03) | 0x0c);
1509 #else /* VGA_SLOW_IOACCESS */
1510 #ifdef VGA_ALT_SEQACCESS
1511 outw(TSIDX, 0x0100);
1513 outw(TSIDX, 0x0002 | (buf[0] << 8));
1514 outw(TSIDX, 0x0004 | (buf[1] << 8));
1515 #ifdef VGA_ALT_SEQACCESS
1516 outw(TSIDX, 0x0300);
1518 outw(GDCIDX, 0x0004 | (buf[2] << 8));
1519 outw(GDCIDX, 0x0005 | (buf[3] << 8));
1520 if (adp->va_crtc_addr == MONO_CRTC)
1521 outw(GDCIDX, 0x0006 | (((buf[4] & 0x03) | 0x08)<<8));
1523 outw(GDCIDX, 0x0006 | (((buf[4] & 0x03) | 0x0c)<<8));
1524 #endif /* VGA_SLOW_IOACCESS */
1529 #endif /* VGA_NO_FONT_LOADING */
1533 * Read the font data in the requested font page from the video adapter.
1538 vga_save_font(video_adapter_t *adp, int page, int fontsize, u_char *data,
1541 #ifndef VGA_NO_FONT_LOADING
1542 u_char buf[PARAM_BUFSIZE];
1545 #ifdef VGA_ALT_SEQACCESS
1550 prologue(adp, V_ADP_FONT, 1);
1552 if (fontsize < 14) {
1555 } else if (fontsize >= 32) {
1557 } else if (fontsize >= 16) {
1565 if (page < 0 || page >= 8)
1567 segment = FONT_BUF + 0x4000*page;
1571 #ifdef VGA_ALT_SEQACCESS
1572 if (adp->va_type == KD_VGA) { /* what about EGA? XXX */
1574 outb(TSIDX, 0x00); outb(TSREG, 0x01);
1575 outb(TSIDX, 0x01); val = inb(TSREG); /* disable screen */
1576 outb(TSIDX, 0x01); outb(TSREG, val | 0x20);
1577 outb(TSIDX, 0x00); outb(TSREG, 0x03);
1582 set_font_mode(adp, buf);
1583 if (fontsize == 32) {
1584 bcopy_fromio(segment + ch*32, data, fontsize*count);
1586 for (c = ch; count > 0; ++c, --count) {
1587 bcopy_fromio(segment + c*32, data, fontsize);
1591 set_normal_mode(adp, buf);
1593 #ifdef VGA_ALT_SEQACCESS
1594 if (adp->va_type == KD_VGA) {
1596 outb(TSIDX, 0x00); outb(TSREG, 0x01);
1597 outb(TSIDX, 0x01); outb(TSREG, val & 0xdf); /* enable screen */
1598 outb(TSIDX, 0x00); outb(TSREG, 0x03);
1604 #else /* VGA_NO_FONT_LOADING */
1606 #endif /* VGA_NO_FONT_LOADING */
1611 * Set the font data in the requested font page.
1612 * NOTE: it appears that some recent video adapters do not support
1613 * the font page other than 0... XXX
1618 vga_load_font(video_adapter_t *adp, int page, int fontsize, u_char *data,
1621 #ifndef VGA_NO_FONT_LOADING
1622 u_char buf[PARAM_BUFSIZE];
1625 #ifdef VGA_ALT_SEQACCESS
1630 prologue(adp, V_ADP_FONT, 1);
1632 if (fontsize < 14) {
1635 } else if (fontsize >= 32) {
1637 } else if (fontsize >= 16) {
1645 if (page < 0 || page >= 8)
1647 segment = FONT_BUF + 0x4000*page;
1651 #ifdef VGA_ALT_SEQACCESS
1652 if (adp->va_type == KD_VGA) { /* what about EGA? XXX */
1654 outb(TSIDX, 0x00); outb(TSREG, 0x01);
1655 outb(TSIDX, 0x01); val = inb(TSREG); /* disable screen */
1656 outb(TSIDX, 0x01); outb(TSREG, val | 0x20);
1657 outb(TSIDX, 0x00); outb(TSREG, 0x03);
1662 set_font_mode(adp, buf);
1663 if (fontsize == 32) {
1664 bcopy_toio(data, segment + ch*32, fontsize*count);
1666 for (c = ch; count > 0; ++c, --count) {
1667 bcopy_toio(data, segment + c*32, fontsize);
1671 set_normal_mode(adp, buf);
1673 #ifdef VGA_ALT_SEQACCESS
1674 if (adp->va_type == KD_VGA) {
1676 outb(TSIDX, 0x00); outb(TSREG, 0x01);
1677 outb(TSIDX, 0x01); outb(TSREG, val & 0xdf); /* enable screen */
1678 outb(TSIDX, 0x00); outb(TSREG, 0x03);
1684 #else /* VGA_NO_FONT_LOADING */
1686 #endif /* VGA_NO_FONT_LOADING */
1691 * Activate the requested font page.
1692 * NOTE: it appears that some recent video adapters do not support
1693 * the font page other than 0... XXX
1698 vga_show_font(video_adapter_t *adp, int page)
1700 #ifndef VGA_NO_FONT_LOADING
1701 static u_char cg[] = { 0x00, 0x05, 0x0a, 0x0f, 0x30, 0x35, 0x3a, 0x3f };
1704 prologue(adp, V_ADP_FONT, 1);
1705 if (page < 0 || page >= 8)
1709 outb(TSIDX, 0x03); outb(TSREG, cg[page]);
1713 #else /* VGA_NO_FONT_LOADING */
1715 #endif /* VGA_NO_FONT_LOADING */
1720 * Read DAC values. The values have expressed in 8 bits.
1725 vga_save_palette(video_adapter_t *adp, u_char *palette)
1729 prologue(adp, V_ADP_PALETTE, 1);
1732 * We store 8 bit values in the palette buffer, while the standard
1733 * VGA has 6 bit DAC .
1735 outb(PALRADR, 0x00);
1736 for (i = 0; i < 256*3; ++i)
1737 palette[i] = inb(PALDATA) << 2;
1738 inb(adp->va_crtc_addr + 6); /* reset flip/flop */
1749 vga_load_palette(video_adapter_t *adp, u_char *palette)
1753 prologue(adp, V_ADP_PALETTE, 1);
1755 outb(PIXMASK, 0xff); /* no pixelmask */
1756 outb(PALWADR, 0x00);
1757 for (i = 0; i < 256*3; ++i)
1758 outb(PALDATA, palette[i] >> 2);
1759 inb(adp->va_crtc_addr + 6); /* reset flip/flop */
1760 outb(ATC, 0x20); /* enable palette */
1766 * Change the border color.
1771 vga_set_border(video_adapter_t *adp, int color)
1773 prologue(adp, V_ADP_BORDER, 1);
1775 switch (adp->va_type) {
1778 inb(adp->va_crtc_addr + 6); /* reset flip-flop */
1779 outb(ATC, 0x31); outb(ATC, color & 0xff);
1782 outb(adp->va_crtc_addr + 5, color & 0x0f); /* color select register */
1794 * Read video register values.
1795 * NOTE: this function only reads the standard EGA/VGA registers.
1796 * any extra/extended registers of SVGA adapters are not saved.
1801 vga_save_state(video_adapter_t *adp, void *p, size_t size)
1810 /* return the required buffer size */
1811 prologue(adp, V_ADP_STATESAVE, 0);
1812 return sizeof(adp_state_t);
1814 prologue(adp, V_ADP_STATESAVE, 1);
1815 if (size < sizeof(adp_state_t))
1819 ((adp_state_t *)p)->sig = V_STATE_SIG;
1820 buf = ((adp_state_t *)p)->regs;
1821 bzero(buf, V_MODE_PARAM_SIZE);
1822 crtc_addr = adp->va_crtc_addr;
1826 outb(TSIDX, 0x00); outb(TSREG, 0x01); /* stop sequencer */
1827 for (i = 0, j = 5; i < 4; i++) {
1829 buf[j++] = inb(TSREG);
1831 buf[9] = inb(MISC + 10); /* dot-clock */
1832 outb(TSIDX, 0x00); outb(TSREG, 0x03); /* start sequencer */
1834 for (i = 0, j = 10; i < 25; i++) { /* crtc */
1836 buf[j++] = inb(crtc_addr + 1);
1838 for (i = 0, j = 35; i < 20; i++) { /* attribute ctrl */
1839 inb(crtc_addr + 6); /* reset flip-flop */
1841 buf[j++] = inb(ATC + 1);
1843 for (i = 0, j = 55; i < 9; i++) { /* graph data ctrl */
1845 buf[j++] = inb(GDCREG);
1847 inb(crtc_addr + 6); /* reset flip-flop */
1848 outb(ATC, 0x20); /* enable palette */
1853 if (vga_get_info(adp, adp->va_mode, &info) == 0) {
1854 if (info.vi_flags & V_INFO_GRAPHICS) {
1855 buf[0] = info.vi_width/info.vi_cwidth; /* COLS */
1856 buf[1] = info.vi_height/info.vi_cheight - 1; /* ROWS */
1858 buf[0] = info.vi_width; /* COLS */
1859 buf[1] = info.vi_height - 1; /* ROWS */
1861 buf[2] = info.vi_cheight; /* POINTS */
1863 /* XXX: shouldn't be happening... */
1864 printf("vga%d: %s: failed to obtain mode info. (vga_save_state())\n",
1865 adp->va_unit, adp->va_name);
1868 buf[0] = readb(BIOS_PADDRTOVADDR(0x44a)); /* COLS */
1869 buf[1] = readb(BIOS_PADDRTOVADDR(0x484)); /* ROWS */
1870 buf[2] = readb(BIOS_PADDRTOVADDR(0x485)); /* POINTS */
1871 buf[3] = readb(BIOS_PADDRTOVADDR(0x44c));
1872 buf[4] = readb(BIOS_PADDRTOVADDR(0x44d));
1880 * Set video registers at once.
1881 * NOTE: this function only updates the standard EGA/VGA registers.
1882 * any extra/extended registers of SVGA adapters are not changed.
1887 vga_load_state(video_adapter_t *adp, void *p)
1894 prologue(adp, V_ADP_STATELOAD, 1);
1895 if (((adp_state_t *)p)->sig != V_STATE_SIG)
1898 buf = ((adp_state_t *)p)->regs;
1899 crtc_addr = adp->va_crtc_addr;
1903 outb(TSIDX, 0x00); outb(TSREG, 0x01); /* stop sequencer */
1904 for (i = 0; i < 4; ++i) { /* program sequencer */
1906 outb(TSREG, buf[i + 5]);
1908 outb(MISC, buf[9]); /* set dot-clock */
1909 outb(TSIDX, 0x00); outb(TSREG, 0x03); /* start sequencer */
1910 outb(crtc_addr, 0x11);
1911 outb(crtc_addr + 1, inb(crtc_addr + 1) & 0x7F);
1912 for (i = 0; i < 25; ++i) { /* program crtc */
1914 outb(crtc_addr + 1, buf[i + 10]);
1916 inb(crtc_addr+6); /* reset flip-flop */
1917 for (i = 0; i < 20; ++i) { /* program attribute ctrl */
1919 outb(ATC, buf[i + 35]);
1921 for (i = 0; i < 9; ++i) { /* program graph data ctrl */
1923 outb(GDCREG, buf[i + 55]);
1925 inb(crtc_addr + 6); /* reset flip-flop */
1926 outb(ATC, 0x20); /* enable palette */
1928 #if notyet /* a temporary workaround for kernel panic, XXX */
1930 if (adp->va_unit == V_ADP_PRIMARY) {
1931 writeb(BIOS_PADDRTOVADDR(0x44a), buf[0]); /* COLS */
1932 writeb(BIOS_PADDRTOVADDR(0x484), buf[1] + rows_offset - 1); /* ROWS */
1933 writeb(BIOS_PADDRTOVADDR(0x485), buf[2]); /* POINTS */
1935 writeb(BIOS_PADDRTOVADDR(0x44c), buf[3]);
1936 writeb(BIOS_PADDRTOVADDR(0x44d), buf[4]);
1939 #endif /* VGA_NO_BIOS */
1948 * Change the origin (window mapping) of the banked frame buffer.
1951 vga_set_origin(video_adapter_t *adp, off_t offset)
1954 * The standard video modes do not require window mapping;
1955 * always return error.
1962 * Read the position of the hardware text cursor.
1967 vga_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
1975 if (adp->va_info.vi_flags & V_INFO_GRAPHICS)
1979 outb(adp->va_crtc_addr, 14);
1980 off = inb(adp->va_crtc_addr + 1);
1981 outb(adp->va_crtc_addr, 15);
1982 off = (off << 8) | inb(adp->va_crtc_addr + 1);
1985 *row = off / adp->va_info.vi_width;
1986 *col = off % adp->va_info.vi_width;
1993 * Move the hardware text cursor. If col and row are both -1,
1994 * the cursor won't be shown.
1999 vga_set_hw_cursor(video_adapter_t *adp, int col, int row)
2007 if ((col == -1) && (row == -1)) {
2010 if (adp->va_info.vi_flags & V_INFO_GRAPHICS)
2012 off = row*adp->va_info.vi_width + col;
2016 outb(adp->va_crtc_addr, 14);
2017 outb(adp->va_crtc_addr + 1, off >> 8);
2018 outb(adp->va_crtc_addr, 15);
2019 outb(adp->va_crtc_addr + 1, off & 0x00ff);
2026 * set_hw_cursor_shape():
2027 * Change the shape of the hardware text cursor. If the height is
2028 * zero or negative, the cursor won't be shown.
2033 vga_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
2034 int celsize, int blink)
2042 switch (adp->va_type) {
2049 /* make the cursor invisible */
2050 outb(adp->va_crtc_addr, 10);
2051 outb(adp->va_crtc_addr + 1, 32);
2052 outb(adp->va_crtc_addr, 11);
2053 outb(adp->va_crtc_addr + 1, 0);
2055 outb(adp->va_crtc_addr, 10);
2056 outb(adp->va_crtc_addr + 1, celsize - base - height);
2057 outb(adp->va_crtc_addr, 11);
2058 outb(adp->va_crtc_addr + 1, celsize - base - 1);
2063 /* make the cursor invisible */
2064 outb(adp->va_crtc_addr, 10);
2065 outb(adp->va_crtc_addr + 1, celsize);
2066 outb(adp->va_crtc_addr, 11);
2067 outb(adp->va_crtc_addr + 1, 0);
2069 outb(adp->va_crtc_addr, 10);
2070 outb(adp->va_crtc_addr + 1, celsize - base - height);
2071 outb(adp->va_crtc_addr, 11);
2072 outb(adp->va_crtc_addr + 1, celsize - base);
2083 * Mmap frame buffer.
2088 vga_mmap(video_adapter_t *adp, vm_offset_t offset)
2090 if (offset > 0x20000 - PAGE_SIZE)
2093 return i386_btop((VIDEO_BUF_BASE + offset));
2096 return alpha_btop((VIDEO_BUF_BASE + offset));
2101 dump_buffer(u_char *buf, size_t len)
2105 for(i = 0; i < len;) {
2106 printf("%02x ", buf[i]);
2107 if ((++i % 16) == 0)
2114 * Print some information about the video adapter and video modes,
2115 * with requested level of details.
2120 vga_diag(video_adapter_t *adp, int level)
2132 printf("vga: RTC equip. code:0x%02x, DCC code:0x%02x\n",
2133 rtcin(RTC_EQUIPMENT), readb(BIOS_PADDRTOVADDR(0x488)));
2134 printf("vga: CRTC:0x%x, video option:0x%02x, ",
2135 readw(BIOS_PADDRTOVADDR(0x463)),
2136 readb(BIOS_PADDRTOVADDR(0x487)));
2137 printf("rows:%d, cols:%d, font height:%d\n",
2138 readb(BIOS_PADDRTOVADDR(0x44a)),
2139 readb(BIOS_PADDRTOVADDR(0x484)) + 1,
2140 readb(BIOS_PADDRTOVADDR(0x485)));
2141 #endif /* VGA_NO_BIOS */
2142 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
2143 printf("vga: param table EGA/VGA:%p", video_mode_ptr);
2144 printf(", CGA/MDA:%p\n", video_mode_ptr2);
2146 printf("vga: rows_offset:%d\n", rows_offset);
2147 #endif /* FB_DEBUG > 1 */
2149 fb_dump_adp_info(DRIVER_NAME, adp, level);
2152 if (adp->va_flags & V_ADP_MODECHANGE) {
2153 for (i = 0; bios_vmode[i].vi_mode != EOT; ++i) {
2154 if (bios_vmode[i].vi_mode == NA)
2156 if (get_mode_param(bios_vmode[i].vi_mode) == NULL)
2158 fb_dump_mode_info(DRIVER_NAME, adp, &bios_vmode[i], level);
2161 vga_get_info(adp, adp->va_initial_mode, &info); /* shouldn't fail */
2162 fb_dump_mode_info(DRIVER_NAME, adp, &info, level);
2164 #endif /* FB_DEBUG > 1 */
2166 if ((adp->va_type != KD_EGA) && (adp->va_type != KD_VGA))
2168 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
2169 if (video_mode_ptr == NULL)
2170 printf("vga%d: %s: WARNING: video mode switching is not "
2171 "fully supported on this adapter\n",
2172 adp->va_unit, adp->va_name);
2177 if (adp->va_type == KD_VGA) {
2178 printf("VGA parameters upon power-up\n");
2179 dump_buffer(adpstate.regs, sizeof(adpstate.regs));
2180 printf("VGA parameters in BIOS for mode %d\n", adp->va_initial_mode);
2181 dump_buffer(adpstate2.regs, sizeof(adpstate2.regs));
2184 mp = get_mode_param(adp->va_initial_mode);
2185 if (mp == NULL) /* this shouldn't be happening */
2187 printf("EGA/VGA parameters to be used for mode %d\n", adp->va_initial_mode);
2188 dump_buffer(mp, V_MODE_PARAM_SIZE);
2193 #endif /* NVGA > 0 */