vga_isa.c revision 47625
1/*-
2 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
3 * Copyright (c) 1992-1998 S�ren Schmidt
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
17 *
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.
28 *
29 * $Id: vga_isa.c,v 1.9 1999/05/30 11:12:30 dfr Exp $
30 */
31
32#include "vga.h"
33#include "opt_vga.h"
34#include "opt_fb.h"
35#include "opt_syscons.h"	/* should be removed in the future, XXX */
36
37#if NVGA > 0
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/kernel.h>
42#include <sys/bus.h>
43#include <sys/malloc.h>
44
45#include <vm/vm.h>
46#include <vm/pmap.h>
47
48#include <machine/console.h>
49#include <machine/md_var.h>
50#include <machine/pc/bios.h>
51
52#include <dev/fb/fbreg.h>
53#include <dev/fb/vgareg.h>
54
55#include <isa/isareg.h>
56#include <isa/isavar.h>
57
58#define DRIVER_NAME		"vga"
59
60/* cdev driver declaration */
61
62#define ISAVGA_UNIT(dev)	minor(dev)
63#define ISAVGA_MKMINOR(unit)	(unit)
64
65typedef struct isavga_softc {
66	video_adapter_t	*adp;
67} isavga_softc_t;
68
69#define ISAVGA_SOFTC(unit)		\
70	((isavga_softc_t *)devclass_get_softc(isavga_devclass, unit))
71
72devclass_t		isavga_devclass;
73
74static int		isavga_probe(device_t dev);
75static int		isavga_attach(device_t dev);
76
77static device_method_t isavga_methods[] = {
78	DEVMETHOD(device_probe,		isavga_probe),
79	DEVMETHOD(device_attach,	isavga_attach),
80	{ 0, 0 }
81};
82
83static driver_t isavga_driver = {
84	DRIVER_NAME,
85	isavga_methods,
86	sizeof(isavga_softc_t),
87};
88
89DRIVER_MODULE(vga, isa, isavga_driver, isavga_devclass, 0, 0);
90
91static int		isavga_probe_unit(int unit, isavga_softc_t *sc,
92					  int flags);
93static int		isavga_attach_unit(int unit, isavga_softc_t *sc,
94					   int flags);
95
96#ifdef FB_INSTALL_CDEV
97
98static d_open_t		isavgaopen;
99static d_close_t	isavgaclose;
100static d_read_t		isavgaread;
101static d_ioctl_t	isavgaioctl;
102
103static struct cdevsw vga_cdevsw = {
104	/* open */	isavgaopen,
105	/* close */	isavgaclose,
106	/* read */	noread,
107	/* write */	nowrite,
108	/* ioctl */	isavgaioctl,
109	/* stop */	nostop,
110	/* reset */	noreset,
111	/* devtotty */	nodevtotty,
112	/* poll */	nopoll,
113	/* mmap */	nommap,
114	/* strategy */	nostrategy,
115	/* name */	DRIVER_NAME,
116	/* parms */	noparms,
117	/* maj */	-1,
118	/* dump */	nodump,
119	/* psize */	nopsize,
120	/* flags */	0,
121	/* maxio */	0,
122	/* bmaj */	-1
123};
124
125#endif /* FB_INSTALL_CDEV */
126
127static int
128isavga_probe(device_t dev)
129{
130	isavga_softc_t *sc;
131
132	/* No pnp support */
133	if (isa_get_vendorid(dev))
134		return (ENXIO);
135
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));
139}
140
141static int
142isavga_attach(device_t dev)
143{
144	isavga_softc_t *sc;
145
146	sc = device_get_softc(dev);
147	return isavga_attach_unit(device_get_unit(dev), sc, isa_get_flags(dev));
148}
149
150static int
151isavga_probe_unit(int unit, isavga_softc_t *sc, int flags)
152{
153	video_switch_t *sw;
154
155	bzero(sc, sizeof(*sc));
156	sw = vid_get_switch(DRIVER_NAME);
157	if (sw == NULL)
158		return 0;
159	return (*sw->probe)(unit, &sc->adp, NULL, flags);
160}
161
162static int
163isavga_attach_unit(int unit, isavga_softc_t *sc, int flags)
164{
165	video_switch_t *sw;
166	int error;
167
168	sw = vid_get_switch(DRIVER_NAME);
169	if (sw == NULL)
170		return ENXIO;
171
172	error = (*sw->init)(unit, sc->adp, flags);
173	if (error)
174		return ENXIO;
175
176#ifdef FB_INSTALL_CDEV
177	/* attach a virtual frame buffer device */
178	error = fb_attach(makedev(0, ISAVGA_MKMINOR(unit)), scp->adp,
179			  &vga_cdevsw);
180	if (error)
181		return error;
182#endif /* FB_INSTALL_CDEV */
183
184	if (bootverbose)
185		(*vidsw[sc->adp->va_index]->diag)(sc->adp, bootverbose);
186
187	return 0;
188}
189
190/* LOW-LEVEL */
191
192#include <machine/clock.h>
193#include <machine/pc/vesa.h>
194
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)
198
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
204#endif
205
206#ifdef SLOW_VGA
207#undef SLOW_VGA
208#undef VGA_SLOW_IOACCESS
209#define VGA_SLOW_IOACCESS	1
210#endif
211
212/* architecture dependent option */
213#ifdef __alpha__
214#define VGA_NO_BIOS		1
215#endif
216
217/* this should really be in `rtc.h' */
218#define RTC_EQUIPMENT           0x14
219
220/* various sizes */
221#define V_MODE_MAP_SIZE		(M_VGA_CG320 + 1)
222#define V_MODE_PARAM_SIZE	64
223
224/* video adapter state buffer */
225struct adp_state {
226    int			sig;
227#define V_STATE_SIG	0x736f6962
228    u_char		regs[V_MODE_PARAM_SIZE];
229};
230typedef struct adp_state adp_state_t;
231
232/* video adapter information */
233#define DCC_MONO	0
234#define DCC_CGA40	1
235#define DCC_CGA80	2
236#define DCC_EGAMONO	3
237#define DCC_EGA40	4
238#define DCC_EGA80	5
239
240/*
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.
244 */
245static video_adapter_t adapter_init_value[] = {
246    /* DCC_MONO */
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,
249      0, 0, 0, 7, 0, },
250    /* DCC_CGA40 */
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,
253      0, 0, 0, 3, 0, },
254    /* DCC_CGA80 */
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,
257      0, 0, 0, 3, 0, },
258    /* DCC_EGAMONO */
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,
261      0, 0, 0, 7, 0, },
262    /* DCC_EGA40 */
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,
265      0, 0, 0, 3, 0, },
266    /* DCC_EGA80 */
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,
269      0, 0, 0, 3, 0, },
270};
271
272static video_adapter_t	biosadapter[2];
273static int		biosadapters = 0;
274
275/* video driver declarations */
276static int			vga_configure(int flags);
277       int			(*vga_sub_configure)(int flags);
278static int			vga_nop(void);
279static vi_probe_t		vga_probe;
280static vi_init_t		vga_init;
281static vi_get_info_t		vga_get_info;
282static vi_query_mode_t		vga_query_mode;
283static vi_set_mode_t		vga_set_mode;
284static vi_save_font_t		vga_save_font;
285static vi_load_font_t		vga_load_font;
286static vi_show_font_t		vga_show_font;
287static vi_save_palette_t	vga_save_palette;
288static vi_load_palette_t	vga_load_palette;
289static vi_set_border_t		vga_set_border;
290static vi_save_state_t		vga_save_state;
291static vi_load_state_t		vga_load_state;
292static vi_set_win_org_t		vga_set_origin;
293static vi_read_hw_cursor_t	vga_read_hw_cursor;
294static vi_set_hw_cursor_t	vga_set_hw_cursor;
295static vi_set_hw_cursor_shape_t	vga_set_hw_cursor_shape;
296static vi_mmap_t		vga_mmap;
297static vi_diag_t		vga_diag;
298
299static video_switch_t vgavidsw = {
300	vga_probe,
301	vga_init,
302	vga_get_info,
303	vga_query_mode,
304	vga_set_mode,
305	vga_save_font,
306	vga_load_font,
307	vga_show_font,
308	vga_save_palette,
309	vga_load_palette,
310	vga_set_border,
311	vga_save_state,
312	vga_load_state,
313	vga_set_origin,
314	vga_read_hw_cursor,
315	vga_set_hw_cursor,
316	vga_set_hw_cursor_shape,
317	(vi_blank_display_t *)vga_nop,
318	vga_mmap,
319	vga_diag,
320};
321
322VIDEO_DRIVER(mda, vgavidsw, NULL);
323VIDEO_DRIVER(cga, vgavidsw, NULL);
324VIDEO_DRIVER(ega, vgavidsw, NULL);
325VIDEO_DRIVER(vga, vgavidsw, vga_configure);
326
327/* VGA BIOS standard video modes */
328#define EOT		(-1)
329#define NA		(-2)
330
331static video_info_t bios_vmode[] = {
332    /* CGA */
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 },
341    /* EGA */
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 },
350    /* VGA */
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 },
357    /* MDA */
358    { M_EGAMONO80x25, 0,          80, 25, 8, 14, 2, 1,
359      MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0 },
360    /* EGA */
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 },
365    /* VGA */
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
379    /* CGA */
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 },
386    /* EGA */
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 },
399    /* VGA */
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 */
409
410    { EOT },
411};
412
413static int		init_done = FALSE;
414#if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
415static u_char		*video_mode_ptr = NULL;		/* EGA/VGA */
416static u_char		*video_mode_ptr2 = NULL;	/* CGA/MDA */
417#endif
418static u_char		*mode_map[V_MODE_MAP_SIZE];
419static adp_state_t	adpstate;
420static adp_state_t	adpstate2;
421static int		rows_offset = 1;
422
423/* local macros and functions */
424#define BIOS_SADDRTOLADDR(p) ((((p) & 0xffff0000) >> 12) + ((p) & 0x0000ffff))
425
426#if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
427static void map_mode_table(u_char *map[], u_char *table, int max);
428#endif
429static void clear_mode_map(video_adapter_t *adp, u_char *map[], int max,
430			   int color);
431#if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
432static int map_mode_num(int mode);
433#endif
434static int map_gen_mode_num(int type, int color, int mode);
435static int map_bios_mode_num(int type, int color, int bios_mode);
436static u_char *get_mode_param(int mode);
437#ifndef VGA_NO_BIOS
438static void fill_adapter_param(int code, video_adapter_t *adp);
439#endif
440static int verify_adapter(video_adapter_t *adp);
441static 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
446static int comp_adpregs(u_char *buf1, u_char *buf2);
447#endif
448static int probe_adapters(void);
449
450#ifndef VGA_NO_FONT_LOADING
451#define PARAM_BUFSIZE	6
452static void set_font_mode(video_adapter_t *adp, u_char *buf);
453static void set_normal_mode(video_adapter_t *adp, u_char *buf);
454#endif
455
456static void dump_buffer(u_char *buf, size_t len);
457
458#define	ISMAPPED(pa, width)				\
459	(((pa) <= (u_long)0x1000 - (width)) 		\
460	 || ((pa) >= ISA_HOLE_START && (pa) <= 0x100000 - (width)))
461
462#define	prologue(adp, flag, err)			\
463	if (!init_done || !((adp)->va_flags & (flag)))	\
464	    return (err)
465
466/* a backdoor for the console driver */
467static int
468vga_configure(int flags)
469{
470    int i;
471
472    probe_adapters();
473    for (i = 0; i < biosadapters; ++i) {
474	if (!probe_done(&biosadapter[i]))
475	    continue;
476	biosadapter[i].va_flags |= V_ADP_INITIALIZED;
477	if (!config_done(&biosadapter[i])) {
478	    if (vid_register(&biosadapter[i]) < 0)
479		continue;
480	    biosadapter[i].va_flags |= V_ADP_REGISTERED;
481	}
482    }
483    if (vga_sub_configure != NULL)
484	(*vga_sub_configure)(flags);
485
486    return biosadapters;
487}
488
489/* local subroutines */
490
491#if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
492/* construct the mode parameter map */
493static void
494map_mode_table(u_char *map[], u_char *table, int max)
495{
496    int i;
497
498    for(i = 0; i < max; ++i)
499	map[i] = table + i*V_MODE_PARAM_SIZE;
500    for(; i < V_MODE_MAP_SIZE; ++i)
501	map[i] = NULL;
502}
503#endif /* !VGA_NO_BIOS && !VGA_NO_MODE_CHANGE */
504
505static void
506clear_mode_map(video_adapter_t *adp, u_char *map[], int max, int color)
507{
508    video_info_t info;
509    int i;
510
511    /*
512     * NOTE: we don't touch `bios_vmode[]' because it is shared
513     * by all adapters.
514     */
515    for(i = 0; i < max; ++i) {
516	if (vga_get_info(adp, i, &info))
517	    continue;
518	if ((info.vi_flags & V_INFO_COLOR) != color)
519	    map[i] = NULL;
520    }
521}
522
523#if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
524/* map the non-standard video mode to a known mode number */
525static int
526map_mode_num(int mode)
527{
528    static struct {
529        int from;
530        int to;
531    } mode_map[] = {
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 },
541    };
542    int i;
543
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;
547    }
548    return mode;
549}
550#endif /* !VGA_NO_BIOS && !VGA_NO_MODE_CHANGE */
551
552/* map a generic video mode to a known mode number */
553static int
554map_gen_mode_num(int type, int color, int mode)
555{
556    static struct {
557	int from;
558	int to_color;
559	int to_mono;
560    } mode_map[] = {
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, },
565    };
566    int i;
567
568    if (mode == M_TEXT_80x25) {
569	switch (type) {
570
571	case KD_VGA:
572	    if (color)
573		return M_VGA_C80x25;
574	    else
575		return M_VGA_M80x25;
576	    break;
577
578	case KD_EGA:
579	    if (color)
580		return M_ENH_C80x25;
581	    else
582		return M_EGAMONO80x25;
583	    break;
584
585	case KD_CGA:
586	    return M_C80x25;
587
588	case KD_MONO:
589	case KD_HERCULES:
590	    return M_EGAMONO80x25;	/* XXX: this name is confusing */
591
592 	default:
593	    return -1;
594	}
595    }
596
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);
600    }
601    return mode;
602}
603
604/* turn the BIOS video number into our video mode number */
605static int
606map_bios_mode_num(int type, int color, int bios_mode)
607{
608    static int cga_modes[7] = {
609	M_B40x25, M_C40x25,		/* 0, 1 */
610	M_B80x25, M_C80x25,		/* 2, 3 */
611	M_BG320, M_CG320,
612	M_BG640,
613    };
614    static int ega_modes[17] = {
615	M_ENH_B40x25, M_ENH_C40x25,	/* 0, 1 */
616	M_ENH_B80x25, M_ENH_C80x25,	/* 2, 3 */
617	M_BG320, M_CG320,
618	M_BG640,
619	M_EGAMONO80x25,			/* 7 */
620	8, 9, 10, 11, 12,
621	M_CG320_D,
622	M_CG640_E,
623	M_ENHMONOAPA2,			/* XXX: video momery > 64K */
624	M_ENH_CG640,			/* XXX: video momery > 64K */
625    };
626    static int vga_modes[20] = {
627	M_VGA_C40x25, M_VGA_C40x25,	/* 0, 1 */
628	M_VGA_C80x25, M_VGA_C80x25,	/* 2, 3 */
629	M_BG320, M_CG320,
630	M_BG640,
631	M_VGA_M80x25,			/* 7 */
632	8, 9, 10, 11, 12,
633	M_CG320_D,
634	M_CG640_E,
635	M_ENHMONOAPA2,
636	M_ENH_CG640,
637	M_BG640x480, M_CG640x480,
638	M_VGA_CG320,
639    };
640
641    switch (type) {
642
643    case KD_VGA:
644	if (bios_mode < sizeof(vga_modes)/sizeof(vga_modes[0]))
645	    return vga_modes[bios_mode];
646	else if (color)
647	    return M_VGA_C80x25;
648	else
649	    return M_VGA_M80x25;
650	break;
651
652    case KD_EGA:
653	if (bios_mode < sizeof(ega_modes)/sizeof(ega_modes[0]))
654	    return ega_modes[bios_mode];
655	else if (color)
656	    return M_ENH_C80x25;
657	else
658	    return M_EGAMONO80x25;
659	break;
660
661    case KD_CGA:
662	if (bios_mode < sizeof(cga_modes)/sizeof(cga_modes[0]))
663	    return cga_modes[bios_mode];
664	else
665	    return M_C80x25;
666	break;
667
668    case KD_MONO:
669    case KD_HERCULES:
670	return M_EGAMONO80x25;		/* XXX: this name is confusing */
671
672    default:
673	break;
674    }
675    return -1;
676}
677
678/* look up a parameter table entry */
679static u_char
680*get_mode_param(int mode)
681{
682#if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
683    if (mode >= V_MODE_MAP_SIZE)
684	mode = map_mode_num(mode);
685#endif
686    if ((mode >= 0) && (mode < V_MODE_MAP_SIZE))
687	return mode_map[mode];
688    else
689	return NULL;
690}
691
692#ifndef VGA_NO_BIOS
693static void
694fill_adapter_param(int code, video_adapter_t *adp)
695{
696    static struct {
697	int primary;
698	int secondary;
699    } dcc[] = {
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 },
712    };
713
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];
717    } else {
718	adp[V_ADP_PRIMARY] = adapter_init_value[dcc[code].primary];
719	adp[V_ADP_SECONDARY] = adapter_init_value[dcc[code].secondary];
720    }
721}
722#endif /* VGA_NO_BIOS */
723
724static int
725verify_adapter(video_adapter_t *adp)
726{
727    vm_offset_t buf;
728    u_int16_t v;
729#if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
730    u_int32_t p;
731#endif
732
733    buf = BIOS_PADDRTOVADDR(adp->va_window);
734    v = readw(buf);
735    writew(buf, 0xA55A);
736    if (readw(buf) != 0xA55A)
737	return 1;
738    writew(buf, v);
739
740    switch (adp->va_type) {
741
742    case KD_EGA:
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;
748	}
749	adp->va_flags |= V_ADP_STATELOAD | V_ADP_BORDER;
750	/* the color adapter may be in the 40x25 mode... XXX */
751
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);
761	}
762#endif
763	break;
764
765    case KD_CGA:
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);
773#endif
774	break;
775
776    case KD_MONO:
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);
782#endif
783	break;
784    }
785
786    return 0;
787}
788
789static void
790update_adapter_info(video_adapter_t *adp, video_info_t *info)
791{
792    adp->va_flags &= ~V_ADP_COLOR;
793    adp->va_flags |=
794	(info->vi_flags & V_INFO_COLOR) ? V_ADP_COLOR : 0;
795    adp->va_crtc_addr =
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) {
801    	adp->va_buffer = 0;
802    	adp->va_buffer_size = 0;
803    } else {
804    	adp->va_buffer = BIOS_PADDRTOVADDR(info->vi_buffer);
805    	adp->va_buffer_size = info->vi_buffer_size;
806    }
807    if (info->vi_flags & V_INFO_GRAPHICS) {
808	switch (info->vi_depth/info->vi_planes) {
809	case 1:
810	    adp->va_line_width = info->vi_width/8;
811	    break;
812	case 2:
813	    adp->va_line_width = info->vi_width/4;
814	    break;
815	case 4:
816	    adp->va_line_width = info->vi_width/2;
817	    break;
818	case 8:
819	default: /* shouldn't happen */
820	    adp->va_line_width = info->vi_width;
821	    break;
822	}
823    } else {
824	adp->va_line_width = info->vi_width;
825    }
826    bcopy(info, &adp->va_info, sizeof(adp->va_info));
827}
828
829#if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
830/* compare two parameter table entries */
831static int
832comp_adpregs(u_char *buf1, u_char *buf2)
833{
834    static struct {
835        u_char mask;
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},
852    };
853    int identical = TRUE;
854    int i;
855
856    if ((buf1 == NULL) || (buf2 == NULL))
857	return COMP_DIFFERENT;
858
859    for (i = 0; i < sizeof(params)/sizeof(params[0]); ++i) {
860	if (params[i].mask == 0)	/* don't care */
861	    continue;
862	if ((buf1[i] & params[i].mask) != (buf2[i] & params[i].mask))
863	    return COMP_DIFFERENT;
864	if (buf1[i] != buf2[i])
865	    identical = FALSE;
866    }
867    return (identical) ? COMP_IDENTICAL : COMP_SIMILAR;
868}
869#endif /* !VGA_NO_BIOS && !VGA_NO_MODE_CHANGE */
870
871/* probe video adapters and return the number of detected adapters */
872static int
873probe_adapters(void)
874{
875    video_adapter_t *adp;
876    video_info_t info;
877    int i;
878
879    /* do this test only once */
880    if (init_done)
881	return biosadapters;
882    init_done = TRUE;
883
884    /*
885     * Locate display adapters.
886     * The AT architecture supports upto two adapters. `syscons' allows
887     * the following combinations of adapters:
888     *     1) MDA + CGA
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.
901     */
902
903    /*
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
909     * type detected.
910     */
911#ifndef VGA_NO_BIOS
912    switch ((rtcin(RTC_EQUIPMENT) >> 4) & 3) {	/* bit 4 and 5 */
913    case 0:
914	/* EGA/VGA */
915	fill_adapter_param(readb(BIOS_PADDRTOVADDR(0x488)) & 0x0f,
916			   biosadapter);
917	break;
918    case 1:
919	/* CGA 40x25 */
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];
923	break;
924    case 2:
925	/* CGA 80x25 */
926	biosadapter[V_ADP_PRIMARY] = adapter_init_value[DCC_CGA80];
927	biosadapter[V_ADP_SECONDARY] = adapter_init_value[DCC_MONO];
928	break;
929    case 3:
930	/* MDA */
931	biosadapter[V_ADP_PRIMARY] = adapter_init_value[DCC_MONO];
932	biosadapter[V_ADP_SECONDARY] = adapter_init_value[DCC_CGA80];
933	break;
934    }
935#else
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 */
940
941    biosadapters = 0;
942    if (verify_adapter(&biosadapter[V_ADP_SECONDARY]) == 0) {
943	++biosadapters;
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
949				  & V_ADP_COLOR,
950			      biosadapter[V_ADP_SECONDARY].va_initial_bios_mode);
951    } else {
952	biosadapter[V_ADP_SECONDARY].va_type = -1;
953    }
954    if (verify_adapter(&biosadapter[V_ADP_PRIMARY]) == 0) {
955	++biosadapters;
956	biosadapter[V_ADP_PRIMARY].va_flags |= V_ADP_PROBED;
957#ifndef VGA_NO_BIOS
958	biosadapter[V_ADP_PRIMARY].va_initial_bios_mode =
959	    readb(BIOS_PADDRTOVADDR(0x449));
960#else
961	biosadapter[V_ADP_PRIMARY].va_initial_bios_mode = 3;	/* XXX */
962#endif
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);
968    } else {
969	biosadapter[V_ADP_PRIMARY] = biosadapter[V_ADP_SECONDARY];
970	biosadapter[V_ADP_SECONDARY].va_type = -1;
971    }
972    if (biosadapters == 0)
973	return biosadapters;
974    biosadapter[V_ADP_PRIMARY].va_unit = V_ADP_PRIMARY;
975    biosadapter[V_ADP_SECONDARY].va_unit = V_ADP_SECONDARY;
976
977#if 0 /* we don't need these... */
978    fb_init_struct(&biosadapter[V_ADP_PRIMARY], ...);
979    fb_init_struct(&biosadapter[V_ADP_SECONDARY], ...);
980#endif
981
982#if 0
983    /*
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.
986     */
987    if (biosadapters > 1) {
988	if (!((biosadapter[0].va_flags ^ biosadapter[1].va_flags)
989	      & V_ADP_COLOR))
990	    /* we have two mono or color adapters!! */
991	    return (biosadapters = 0);
992    }
993#endif
994
995    /*
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
999     * memory.
1000     * This must be done before vga_save_state() for VGA.
1001     */
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);
1006
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.
1010     */
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];
1017    } else {
1018	adp = NULL;
1019    }
1020    bzero(mode_map, sizeof(mode_map));
1021    if (adp != NULL) {
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;
1026	    rows_offset = 1;
1027#else /* VGA_NO_BIOS || VGA_NO_MODE_CHANGE */
1028	    if (video_mode_ptr == NULL) {
1029		mode_map[adp->va_initial_mode] = adpstate.regs;
1030		rows_offset = 1;
1031	    } else {
1032		/* discard the table if we are not familiar with it... */
1033		u_char *mp;
1034		map_mode_table(mode_map, video_mode_ptr, M_VGA_CG320 + 1);
1035		mp = get_mode_param(adp->va_initial_mode);
1036		if (mp != NULL)
1037		    bcopy(mp, adpstate2.regs, sizeof(adpstate2.regs));
1038		switch (comp_adpregs(adpstate.regs, mp)) {
1039		case COMP_IDENTICAL:
1040		    /*
1041		     * OK, this parameter table looks reasonably familiar
1042		     * to us...
1043		     */
1044		    /*
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
1049		     */
1050		    rows_offset = adpstate.regs[1] + 1 - mp[1];
1051		    break;
1052
1053		case COMP_SIMILAR:
1054		    /*
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.
1059		     */
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;
1063		    break;
1064
1065		case COMP_DIFFERENT:
1066		default:
1067		    /*
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
1071		     * the initial mode.
1072		     */
1073		    video_mode_ptr = NULL;
1074		    bzero(mode_map, sizeof(mode_map));
1075		    mode_map[adp->va_initial_mode] = adpstate.regs;
1076		    rows_offset = 1;
1077		    break;
1078		}
1079	    }
1080#endif /* VGA_NO_BIOS || VGA_NO_MODE_CHANGE */
1081
1082#ifndef VGA_NO_MODE_CHANGE
1083	    adp->va_flags |= V_ADP_MODECHANGE;
1084#endif
1085#ifndef VGA_NO_FONT_LOADING
1086	    adp->va_flags |= V_ADP_FONT;
1087#endif
1088	} else if (adp->va_type == KD_EGA) {
1089#if defined(VGA_NO_BIOS) || defined(VGA_NO_MODE_CHANGE)
1090	    rows_offset = 1;
1091#else /* VGA_NO_BIOS || VGA_NO_MODE_CHANGE */
1092	    if (video_mode_ptr == NULL) {
1093		rows_offset = 1;
1094	    } else {
1095		u_char *mp;
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);
1099		if (mp != NULL) {
1100		    adp->va_flags |= V_ADP_MODECHANGE;
1101#ifndef VGA_NO_FONT_LOADING
1102		    adp->va_flags |= V_ADP_FONT;
1103#endif
1104		    rows_offset = 1;
1105		} else {
1106		    /*
1107		     * This is serious. We will not be able to switch video
1108		     * modes at all...
1109		     */
1110		    video_mode_ptr = NULL;
1111		    bzero(mode_map, sizeof(mode_map));
1112		    rows_offset = 1;
1113                }
1114	    }
1115#endif /* VGA_NO_BIOS || VGA_NO_MODE_CHANGE */
1116	}
1117    }
1118
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))
1123		continue;
1124	    clear_mode_map(&biosadapter[i], mode_map, M_VGA_CG320 + 1,
1125			   (biosadapter[i].va_flags & V_ADP_COLOR) ?
1126			       V_INFO_COLOR : 0);
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) ?
1131			IO_VGA : IO_MDA;
1132		biosadapter[i].va_io_size = 32;
1133	    }
1134	}
1135    }
1136
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);
1141
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);
1146    }
1147
1148    /*
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);
1157     */
1158
1159    return biosadapters;
1160}
1161
1162/* entry points */
1163
1164static int
1165vga_nop(void)
1166{
1167    return 0;
1168}
1169
1170static int
1171vga_probe(int unit, video_adapter_t **adpp, void *arg, int flags)
1172{
1173    probe_adapters();
1174    if (unit >= biosadapters)
1175	return ENXIO;
1176
1177    *adpp = &biosadapter[unit];
1178
1179    return 0;
1180}
1181
1182static int
1183vga_init(int unit, video_adapter_t *adp, int flags)
1184{
1185    if ((unit >= biosadapters) || (adp == NULL) || !probe_done(adp))
1186	return ENXIO;
1187
1188    if (!init_done(adp)) {
1189	/* nothing to do really... */
1190	adp->va_flags |= V_ADP_INITIALIZED;
1191    }
1192
1193    if (!config_done(adp)) {
1194	if (vid_register(adp) < 0)
1195		return ENXIO;
1196	adp->va_flags |= V_ADP_REGISTERED;
1197    }
1198    if (vga_sub_configure != NULL)
1199	(*vga_sub_configure)(0);
1200
1201    return 0;
1202}
1203
1204/*
1205 * get_info():
1206 * Return the video_info structure of the requested video mode.
1207 *
1208 * all adapters
1209 */
1210static int
1211vga_get_info(video_adapter_t *adp, int mode, video_info_t *info)
1212{
1213    int i;
1214
1215    if (!init_done)
1216	return 1;
1217
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) {
1221	/*
1222	 * If the parameter table entry for this mode is not found,
1223	 * the mode is not supported...
1224	 */
1225	if (get_mode_param(mode) == NULL)
1226	    return 1;
1227    } else
1228#endif /* VGA_NO_MODE_CHANGE */
1229    {
1230	/*
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.
1234	 */
1235	if (mode != adp->va_initial_mode)
1236	    return 1;
1237    }
1238
1239    for (i = 0; bios_vmode[i].vi_mode != EOT; ++i) {
1240	if (bios_vmode[i].vi_mode == NA)
1241	    continue;
1242	if (mode == bios_vmode[i].vi_mode) {
1243	    *info = bios_vmode[i];
1244	    return 0;
1245	}
1246    }
1247    return 1;
1248}
1249
1250/*
1251 * query_mode():
1252 * Find a video mode matching the requested parameters.
1253 * Fields filled with 0 are considered "don't care" fields and
1254 * match any modes.
1255 *
1256 * all adapters
1257 */
1258static int
1259vga_query_mode(video_adapter_t *adp, video_info_t *info)
1260{
1261    video_info_t buf;
1262    int i;
1263
1264    if (!init_done)
1265	return -1;
1266
1267    for (i = 0; bios_vmode[i].vi_mode != EOT; ++i) {
1268	if (bios_vmode[i].vi_mode == NA)
1269	    continue;
1270
1271	if ((info->vi_width != 0)
1272	    && (info->vi_width != bios_vmode[i].vi_width))
1273		continue;
1274	if ((info->vi_height != 0)
1275	    && (info->vi_height != bios_vmode[i].vi_height))
1276		continue;
1277	if ((info->vi_cwidth != 0)
1278	    && (info->vi_cwidth != bios_vmode[i].vi_cwidth))
1279		continue;
1280	if ((info->vi_cheight != 0)
1281	    && (info->vi_cheight != bios_vmode[i].vi_cheight))
1282		continue;
1283	if ((info->vi_depth != 0)
1284	    && (info->vi_depth != bios_vmode[i].vi_depth))
1285		continue;
1286	if ((info->vi_planes != 0)
1287	    && (info->vi_planes != bios_vmode[i].vi_planes))
1288		continue;
1289	/* XXX: should check pixel format, memory model */
1290	if ((info->vi_flags != 0)
1291	    && (info->vi_flags != bios_vmode[i].vi_flags))
1292		continue;
1293
1294	/* verify if this mode is supported on this adapter */
1295	if (vga_get_info(adp, bios_vmode[i].vi_mode, &buf))
1296		continue;
1297	return bios_vmode[i].vi_mode;
1298    }
1299    return -1;
1300}
1301
1302/*
1303 * set_mode():
1304 * Change the video mode.
1305 *
1306 * EGA/VGA
1307 */
1308static int
1309vga_set_mode(video_adapter_t *adp, int mode)
1310{
1311#ifndef VGA_NO_MODE_CHANGE
1312    video_info_t info;
1313    adp_state_t params;
1314
1315    prologue(adp, V_ADP_MODECHANGE, 1);
1316
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))
1320	return 1;
1321    params.sig = V_STATE_SIG;
1322    bcopy(get_mode_param(mode), params.regs, sizeof(params.regs));
1323
1324    switch (mode) {
1325    case M_VGA_C80x60: case M_VGA_M80x60:
1326	params.regs[2]  = 0x08;
1327	params.regs[19] = 0x47;
1328	goto special_480l;
1329
1330    case M_VGA_C80x30: case M_VGA_M80x30:
1331	params.regs[19] = 0x4f;
1332special_480l:
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;
1340	goto setup_mode;
1341
1342    case M_ENH_C80x43: case M_ENH_B80x43:
1343	params.regs[28] = 87;
1344	goto special_80x50;
1345
1346    case M_VGA_C80x50: case M_VGA_M80x50:
1347special_80x50:
1348	params.regs[2] = 8;
1349	params.regs[19] = 7;
1350	goto setup_mode;
1351
1352    case M_VGA_C40x25: case M_VGA_C80x25:
1353    case M_VGA_M80x25:
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:
1359
1360setup_mode:
1361	vga_load_state(adp, &params);
1362	break;
1363
1364    case M_VGA_MODEX:
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;
1374	/* set 240 lines */
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;
1385	goto setup_grmode;
1386
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:
1391
1392setup_grmode:
1393	vga_load_state(adp, &params);
1394	break;
1395
1396    default:
1397	return 1;
1398    }
1399
1400    adp->va_mode = mode;
1401    update_adapter_info(adp, &info);
1402
1403    /* move hardware cursor out of the way */
1404    (*vidsw[adp->va_index]->set_hw_cursor)(adp, -1, -1);
1405
1406    return 0;
1407#else /* VGA_NO_MODE_CHANGE */
1408    return 1;
1409#endif /* VGA_NO_MODE_CHANGE */
1410}
1411
1412#ifndef VGA_NO_FONT_LOADING
1413
1414static void
1415set_font_mode(video_adapter_t *adp, u_char *buf)
1416{
1417    u_char *mp;
1418    int s;
1419
1420    s = splhigh();
1421
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) */ {
1432	/*
1433	 * EGA cannot be read; copy parameters from the mode parameter
1434	 * table.
1435	 */
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];
1443    }
1444
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 */
1450
1451#if VGA_SLOW_IOACCESS
1452#ifdef VGA_ALT_SEQACCESS
1453    outb(TSIDX, 0x00); outb(TSREG, 0x01);
1454#endif
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);
1459#endif
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);
1466#endif
1467    outw(TSIDX, 0x0402);
1468    outw(TSIDX, 0x0704);
1469#ifdef VGA_ALT_SEQACCESS
1470    outw(TSIDX, 0x0300);
1471#endif
1472    outw(GDCIDX, 0x0204);
1473    outw(GDCIDX, 0x0005);
1474    outw(GDCIDX, 0x0406);               /* addr = a0000, 64kb */
1475#endif /* VGA_SLOW_IOACCESS */
1476
1477    splx(s);
1478}
1479
1480static void
1481set_normal_mode(video_adapter_t *adp, u_char *buf)
1482{
1483    int s;
1484
1485    s = splhigh();
1486
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 */
1492
1493#if VGA_SLOW_IOACCESS
1494#ifdef VGA_ALT_SEQACCESS
1495    outb(TSIDX, 0x00); outb(TSREG, 0x01);
1496#endif
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);
1501#endif
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);
1506    } else {
1507	outb(GDCIDX, 0x06); outb(GDCREG,(buf[4] & 0x03) | 0x0c);
1508    }
1509#else /* VGA_SLOW_IOACCESS */
1510#ifdef VGA_ALT_SEQACCESS
1511    outw(TSIDX, 0x0100);
1512#endif
1513    outw(TSIDX, 0x0002 | (buf[0] << 8));
1514    outw(TSIDX, 0x0004 | (buf[1] << 8));
1515#ifdef VGA_ALT_SEQACCESS
1516    outw(TSIDX, 0x0300);
1517#endif
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));
1522    else
1523        outw(GDCIDX, 0x0006 | (((buf[4] & 0x03) | 0x0c)<<8));
1524#endif /* VGA_SLOW_IOACCESS */
1525
1526    splx(s);
1527}
1528
1529#endif /* VGA_NO_FONT_LOADING */
1530
1531/*
1532 * save_font():
1533 * Read the font data in the requested font page from the video adapter.
1534 *
1535 * EGA/VGA
1536 */
1537static int
1538vga_save_font(video_adapter_t *adp, int page, int fontsize, u_char *data,
1539	      int ch, int count)
1540{
1541#ifndef VGA_NO_FONT_LOADING
1542    u_char buf[PARAM_BUFSIZE];
1543    u_int32_t segment;
1544    int c;
1545#ifdef VGA_ALT_SEQACCESS
1546    int s;
1547    u_char val = 0;
1548#endif
1549
1550    prologue(adp, V_ADP_FONT, 1);
1551
1552    if (fontsize < 14) {
1553	/* FONT_8 */
1554	fontsize = 8;
1555    } else if (fontsize >= 32) {
1556	fontsize = 32;
1557    } else if (fontsize >= 16) {
1558	/* FONT_16 */
1559	fontsize = 16;
1560    } else {
1561	/* FONT_14 */
1562	fontsize = 14;
1563    }
1564
1565    if (page < 0 || page >= 8)
1566	return 1;
1567    segment = FONT_BUF + 0x4000*page;
1568    if (page > 3)
1569	segment -= 0xe000;
1570
1571#ifdef VGA_ALT_SEQACCESS
1572    if (adp->va_type == KD_VGA) {	/* what about EGA? XXX */
1573	s = splhigh();
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);
1578	splx(s);
1579    }
1580#endif
1581
1582    set_font_mode(adp, buf);
1583    if (fontsize == 32) {
1584	bcopy_fromio(segment + ch*32, data, fontsize*count);
1585    } else {
1586	for (c = ch; count > 0; ++c, --count) {
1587	    bcopy_fromio(segment + c*32, data, fontsize);
1588	    data += fontsize;
1589	}
1590    }
1591    set_normal_mode(adp, buf);
1592
1593#ifdef VGA_ALT_SEQACCESS
1594    if (adp->va_type == KD_VGA) {
1595	s = splhigh();
1596	outb(TSIDX, 0x00); outb(TSREG, 0x01);
1597	outb(TSIDX, 0x01); outb(TSREG, val & 0xdf);	/* enable screen */
1598	outb(TSIDX, 0x00); outb(TSREG, 0x03);
1599	splx(s);
1600    }
1601#endif
1602
1603    return 0;
1604#else /* VGA_NO_FONT_LOADING */
1605    return 1;
1606#endif /* VGA_NO_FONT_LOADING */
1607}
1608
1609/*
1610 * load_font():
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
1614 *
1615 * EGA/VGA
1616 */
1617static int
1618vga_load_font(video_adapter_t *adp, int page, int fontsize, u_char *data,
1619	      int ch, int count)
1620{
1621#ifndef VGA_NO_FONT_LOADING
1622    u_char buf[PARAM_BUFSIZE];
1623    u_int32_t segment;
1624    int c;
1625#ifdef VGA_ALT_SEQACCESS
1626    int s;
1627    u_char val = 0;
1628#endif
1629
1630    prologue(adp, V_ADP_FONT, 1);
1631
1632    if (fontsize < 14) {
1633	/* FONT_8 */
1634	fontsize = 8;
1635    } else if (fontsize >= 32) {
1636	fontsize = 32;
1637    } else if (fontsize >= 16) {
1638	/* FONT_16 */
1639	fontsize = 16;
1640    } else {
1641	/* FONT_14 */
1642	fontsize = 14;
1643    }
1644
1645    if (page < 0 || page >= 8)
1646	return 1;
1647    segment = FONT_BUF + 0x4000*page;
1648    if (page > 3)
1649	segment -= 0xe000;
1650
1651#ifdef VGA_ALT_SEQACCESS
1652    if (adp->va_type == KD_VGA) {	/* what about EGA? XXX */
1653	s = splhigh();
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);
1658	splx(s);
1659    }
1660#endif
1661
1662    set_font_mode(adp, buf);
1663    if (fontsize == 32) {
1664	bcopy_toio(data, segment + ch*32, fontsize*count);
1665    } else {
1666	for (c = ch; count > 0; ++c, --count) {
1667	    bcopy_toio(data, segment + c*32, fontsize);
1668	    data += fontsize;
1669	}
1670    }
1671    set_normal_mode(adp, buf);
1672
1673#ifdef VGA_ALT_SEQACCESS
1674    if (adp->va_type == KD_VGA) {
1675	s = splhigh();
1676	outb(TSIDX, 0x00); outb(TSREG, 0x01);
1677	outb(TSIDX, 0x01); outb(TSREG, val & 0xdf);	/* enable screen */
1678	outb(TSIDX, 0x00); outb(TSREG, 0x03);
1679	splx(s);
1680    }
1681#endif
1682
1683    return 0;
1684#else /* VGA_NO_FONT_LOADING */
1685    return 1;
1686#endif /* VGA_NO_FONT_LOADING */
1687}
1688
1689/*
1690 * show_font():
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
1694 *
1695 * EGA/VGA
1696 */
1697static int
1698vga_show_font(video_adapter_t *adp, int page)
1699{
1700#ifndef VGA_NO_FONT_LOADING
1701    static u_char cg[] = { 0x00, 0x05, 0x0a, 0x0f, 0x30, 0x35, 0x3a, 0x3f };
1702    int s;
1703
1704    prologue(adp, V_ADP_FONT, 1);
1705    if (page < 0 || page >= 8)
1706	return 1;
1707
1708    s = splhigh();
1709    outb(TSIDX, 0x03); outb(TSREG, cg[page]);
1710    splx(s);
1711
1712    return 0;
1713#else /* VGA_NO_FONT_LOADING */
1714    return 1;
1715#endif /* VGA_NO_FONT_LOADING */
1716}
1717
1718/*
1719 * save_palette():
1720 * Read DAC values. The values have expressed in 8 bits.
1721 *
1722 * VGA
1723 */
1724static int
1725vga_save_palette(video_adapter_t *adp, u_char *palette)
1726{
1727    int i;
1728
1729    prologue(adp, V_ADP_PALETTE, 1);
1730
1731    /*
1732     * We store 8 bit values in the palette buffer, while the standard
1733     * VGA has 6 bit DAC .
1734     */
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 */
1739    return 0;
1740}
1741
1742/*
1743 * load_palette():
1744 * Set DAC values.
1745 *
1746 * VGA
1747 */
1748static int
1749vga_load_palette(video_adapter_t *adp, u_char *palette)
1750{
1751    int i;
1752
1753    prologue(adp, V_ADP_PALETTE, 1);
1754
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 */
1761    return 0;
1762}
1763
1764/*
1765 * set_border():
1766 * Change the border color.
1767 *
1768 * CGA/EGA/VGA
1769 */
1770static int
1771vga_set_border(video_adapter_t *adp, int color)
1772{
1773    prologue(adp, V_ADP_BORDER, 1);
1774
1775    switch (adp->va_type) {
1776    case KD_EGA:
1777    case KD_VGA:
1778	inb(adp->va_crtc_addr + 6);	/* reset flip-flop */
1779	outb(ATC, 0x31); outb(ATC, color & 0xff);
1780	break;
1781    case KD_CGA:
1782	outb(adp->va_crtc_addr + 5, color & 0x0f); /* color select register */
1783	break;
1784    case KD_MONO:
1785    case KD_HERCULES:
1786    default:
1787	break;
1788    }
1789    return 0;
1790}
1791
1792/*
1793 * save_state():
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.
1797 *
1798 * VGA
1799 */
1800static int
1801vga_save_state(video_adapter_t *adp, void *p, size_t size)
1802{
1803    video_info_t info;
1804    u_char *buf;
1805    int crtc_addr;
1806    int i, j;
1807    int s;
1808
1809    if (size == 0) {
1810	/* return the required buffer size */
1811	prologue(adp, V_ADP_STATESAVE, 0);
1812	return sizeof(adp_state_t);
1813    } else {
1814	prologue(adp, V_ADP_STATESAVE, 1);
1815	if (size < sizeof(adp_state_t))
1816	    return 1;
1817    }
1818
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;
1823
1824    s = splhigh();
1825
1826    outb(TSIDX, 0x00); outb(TSREG, 0x01);	/* stop sequencer */
1827    for (i = 0, j = 5; i < 4; i++) {
1828	outb(TSIDX, i + 1);
1829	buf[j++]  =  inb(TSREG);
1830    }
1831    buf[9]  =  inb(MISC + 10);			/* dot-clock */
1832    outb(TSIDX, 0x00); outb(TSREG, 0x03);	/* start sequencer */
1833
1834    for (i = 0, j = 10; i < 25; i++) {		/* crtc */
1835	outb(crtc_addr, i);
1836	buf[j++]  =  inb(crtc_addr + 1);
1837    }
1838    for (i = 0, j = 35; i < 20; i++) {		/* attribute ctrl */
1839        inb(crtc_addr + 6);			/* reset flip-flop */
1840	outb(ATC, i);
1841	buf[j++]  =  inb(ATC + 1);
1842    }
1843    for (i = 0, j = 55; i < 9; i++) {		/* graph data ctrl */
1844	outb(GDCIDX, i);
1845	buf[j++]  =  inb(GDCREG);
1846    }
1847    inb(crtc_addr + 6);				/* reset flip-flop */
1848    outb(ATC, 0x20);				/* enable palette */
1849
1850    splx(s);
1851
1852#if 1
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 */
1857	} else {
1858	    buf[0] = info.vi_width;		/* COLS */
1859	    buf[1] = info.vi_height - 1;	/* ROWS */
1860	}
1861	buf[2] = info.vi_cheight;		/* POINTS */
1862    } else {
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);
1866    }
1867#else
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));
1873#endif
1874
1875    return 0;
1876}
1877
1878/*
1879 * load_state():
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.
1883 *
1884 * EGA/VGA
1885 */
1886static int
1887vga_load_state(video_adapter_t *adp, void *p)
1888{
1889    u_char *buf;
1890    int crtc_addr;
1891    int s;
1892    int i;
1893
1894    prologue(adp, V_ADP_STATELOAD, 1);
1895    if (((adp_state_t *)p)->sig != V_STATE_SIG)
1896	return 1;
1897
1898    buf = ((adp_state_t *)p)->regs;
1899    crtc_addr = adp->va_crtc_addr;
1900
1901    s = splhigh();
1902
1903    outb(TSIDX, 0x00); outb(TSREG, 0x01);	/* stop sequencer */
1904    for (i = 0; i < 4; ++i) {			/* program sequencer */
1905	outb(TSIDX, i + 1);
1906	outb(TSREG, buf[i + 5]);
1907    }
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 */
1913	outb(crtc_addr, i);
1914	outb(crtc_addr + 1, buf[i + 10]);
1915    }
1916    inb(crtc_addr+6);				/* reset flip-flop */
1917    for (i = 0; i < 20; ++i) {			/* program attribute ctrl */
1918	outb(ATC, i);
1919	outb(ATC, buf[i + 35]);
1920    }
1921    for (i = 0; i < 9; ++i) {			/* program graph data ctrl */
1922	outb(GDCIDX, i);
1923	outb(GDCREG, buf[i + 55]);
1924    }
1925    inb(crtc_addr + 6);				/* reset flip-flop */
1926    outb(ATC, 0x20);				/* enable palette */
1927
1928#if notyet /* a temporary workaround for kernel panic, XXX */
1929#ifndef VGA_NO_BIOS
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 */
1934#if 0
1935	writeb(BIOS_PADDRTOVADDR(0x44c), buf[3]);
1936	writeb(BIOS_PADDRTOVADDR(0x44d), buf[4]);
1937#endif
1938    }
1939#endif /* VGA_NO_BIOS */
1940#endif /* notyet */
1941
1942    splx(s);
1943    return 0;
1944}
1945
1946/*
1947 * set_origin():
1948 * Change the origin (window mapping) of the banked frame buffer.
1949 */
1950static int
1951vga_set_origin(video_adapter_t *adp, off_t offset)
1952{
1953    /*
1954     * The standard video modes do not require window mapping;
1955     * always return error.
1956     */
1957    return 1;
1958}
1959
1960/*
1961 * read_hw_cursor():
1962 * Read the position of the hardware text cursor.
1963 *
1964 * all adapters
1965 */
1966static int
1967vga_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
1968{
1969    u_int16_t off;
1970    int s;
1971
1972    if (!init_done)
1973	return 1;
1974
1975    if (adp->va_info.vi_flags & V_INFO_GRAPHICS)
1976	return 1;
1977
1978    s = spltty();
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);
1983    splx(s);
1984
1985    *row = off / adp->va_info.vi_width;
1986    *col = off % adp->va_info.vi_width;
1987
1988    return 0;
1989}
1990
1991/*
1992 * set_hw_cursor():
1993 * Move the hardware text cursor.  If col and row are both -1,
1994 * the cursor won't be shown.
1995 *
1996 * all adapters
1997 */
1998static int
1999vga_set_hw_cursor(video_adapter_t *adp, int col, int row)
2000{
2001    u_int16_t off;
2002    int s;
2003
2004    if (!init_done)
2005	return 1;
2006
2007    if ((col == -1) && (row == -1)) {
2008	off = -1;
2009    } else {
2010	if (adp->va_info.vi_flags & V_INFO_GRAPHICS)
2011	    return 1;
2012	off = row*adp->va_info.vi_width + col;
2013    }
2014
2015    s = spltty();
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);
2020    splx(s);
2021
2022    return 0;
2023}
2024
2025/*
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.
2029 *
2030 * all adapters
2031 */
2032static int
2033vga_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
2034			int celsize, int blink)
2035{
2036    int s;
2037
2038    if (!init_done)
2039	return 1;
2040
2041    s = spltty();
2042    switch (adp->va_type) {
2043    case KD_VGA:
2044    case KD_CGA:
2045    case KD_MONO:
2046    case KD_HERCULES:
2047    default:
2048	if (height <= 0) {
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);
2054	} else {
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);
2059	}
2060	break;
2061    case KD_EGA:
2062	if (height <= 0) {
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);
2068	} else {
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);
2073	}
2074	break;
2075    }
2076    splx(s);
2077
2078    return 0;
2079}
2080
2081/*
2082 * mmap():
2083 * Mmap frame buffer.
2084 *
2085 * all adapters
2086 */
2087static int
2088vga_mmap(video_adapter_t *adp, vm_offset_t offset)
2089{
2090    if (offset > 0x20000 - PAGE_SIZE)
2091	return -1;
2092#ifdef __i386__
2093    return i386_btop((VIDEO_BUF_BASE + offset));
2094#endif
2095#ifdef __alpha__
2096    return alpha_btop((VIDEO_BUF_BASE + offset));
2097#endif
2098}
2099
2100static void
2101dump_buffer(u_char *buf, size_t len)
2102{
2103    int i;
2104
2105    for(i = 0; i < len;) {
2106	printf("%02x ", buf[i]);
2107	if ((++i % 16) == 0)
2108	    printf("\n");
2109    }
2110}
2111
2112/*
2113 * diag():
2114 * Print some information about the video adapter and video modes,
2115 * with requested level of details.
2116 *
2117 * all adapters
2118 */
2119static int
2120vga_diag(video_adapter_t *adp, int level)
2121{
2122#if FB_DEBUG > 1
2123    video_info_t info;
2124#endif
2125    u_char *mp;
2126
2127    if (!init_done)
2128	return 1;
2129
2130#if FB_DEBUG > 1
2131#ifndef VGA_NO_BIOS
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);
2145#endif
2146    printf("vga: rows_offset:%d\n", rows_offset);
2147#endif /* FB_DEBUG > 1 */
2148
2149    fb_dump_adp_info(DRIVER_NAME, adp, level);
2150
2151#if FB_DEBUG > 1
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)
2155		continue;
2156	    if (get_mode_param(bios_vmode[i].vi_mode) == NULL)
2157		continue;
2158	    fb_dump_mode_info(DRIVER_NAME, adp, &bios_vmode[i], level);
2159	}
2160    } else {
2161	vga_get_info(adp, adp->va_initial_mode, &info);	/* shouldn't fail */
2162	fb_dump_mode_info(DRIVER_NAME, adp, &info, level);
2163    }
2164#endif /* FB_DEBUG > 1 */
2165
2166    if ((adp->va_type != KD_EGA) && (adp->va_type != KD_VGA))
2167	return 0;
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);
2173#endif
2174    if (level <= 0)
2175	return 0;
2176
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));
2182    }
2183
2184    mp = get_mode_param(adp->va_initial_mode);
2185    if (mp == NULL)	/* this shouldn't be happening */
2186	return 0;
2187    printf("EGA/VGA parameters to be used for mode %d\n", adp->va_initial_mode);
2188    dump_buffer(mp, V_MODE_PARAM_SIZE);
2189
2190    return 0;
2191}
2192
2193#endif /* NVGA > 0 */
2194