vga_isa.c revision 46805
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.7 1999/05/08 21:59:32 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	isavgaopen,	isavgaclose,	noread,		nowrite,	/* ?? */
105	isavgaioctl,	nostop,		nullreset,	nodevtotty,
106	seltrue,	nommap,		NULL,		DRIVER_NAME,
107	NULL,		-1,		nodump,		nopsize,
108};
109
110#endif /* FB_INSTALL_CDEV */
111
112static int
113isavga_probe(device_t dev)
114{
115	isavga_softc_t *sc;
116
117	device_set_desc(dev, "Generic ISA VGA");
118	sc = device_get_softc(dev);
119	return isavga_probe_unit(device_get_unit(dev), sc, isa_get_flags(dev));
120}
121
122static int
123isavga_attach(device_t dev)
124{
125	isavga_softc_t *sc;
126
127	sc = device_get_softc(dev);
128	return isavga_attach_unit(device_get_unit(dev), sc, isa_get_flags(dev));
129}
130
131static int
132isavga_probe_unit(int unit, isavga_softc_t *sc, int flags)
133{
134	video_switch_t *sw;
135
136	bzero(sc, sizeof(*sc));
137	sw = vid_get_switch(DRIVER_NAME);
138	if (sw == NULL)
139		return 0;
140	return (*sw->probe)(unit, &sc->adp, NULL, flags);
141}
142
143static int
144isavga_attach_unit(int unit, isavga_softc_t *sc, int flags)
145{
146	video_switch_t *sw;
147	int error;
148
149	sw = vid_get_switch(DRIVER_NAME);
150	if (sw == NULL)
151		return ENXIO;
152
153	error = (*sw->init)(unit, sc->adp, flags);
154	if (error)
155		return ENXIO;
156
157#ifdef FB_INSTALL_CDEV
158	/* attach a virtual frame buffer device */
159	error = fb_attach(makedev(0, ISAVGA_MKMINOR(unit)), scp->adp,
160			  &vga_cdevsw);
161	if (error)
162		return error;
163#endif /* FB_INSTALL_CDEV */
164
165	if (bootverbose)
166		(*vidsw[sc->adp->va_index]->diag)(sc->adp, bootverbose);
167
168	return 0;
169}
170
171/* LOW-LEVEL */
172
173#include <machine/clock.h>
174#include <machine/pc/vesa.h>
175
176#define probe_done(adp)		((adp)->va_flags & V_ADP_PROBED)
177#define init_done(adp)		((adp)->va_flags & V_ADP_INITIALIZED)
178#define config_done(adp)	((adp)->va_flags & V_ADP_REGISTERED)
179
180/* for compatibility with old kernel options */
181#ifdef SC_ALT_SEQACCESS
182#undef SC_ALT_SEQACCESS
183#undef VGA_ALT_SEQACCESS
184#define VGA_ALT_SEQACCESS	1
185#endif
186
187#ifdef SLOW_VGA
188#undef SLOW_VGA
189#undef VGA_SLOW_IOACCESS
190#define VGA_SLOW_IOACCESS	1
191#endif
192
193/* architecture dependent option */
194#ifdef __alpha__
195#define VGA_NO_BIOS		1
196#endif
197
198/* this should really be in `rtc.h' */
199#define RTC_EQUIPMENT           0x14
200
201/* various sizes */
202#define V_MODE_MAP_SIZE		(M_VGA_CG320 + 1)
203#define V_MODE_PARAM_SIZE	64
204
205/* video adapter state buffer */
206struct adp_state {
207    int			sig;
208#define V_STATE_SIG	0x736f6962
209    u_char		regs[V_MODE_PARAM_SIZE];
210};
211typedef struct adp_state adp_state_t;
212
213/* video adapter information */
214#define DCC_MONO	0
215#define DCC_CGA40	1
216#define DCC_CGA80	2
217#define DCC_EGAMONO	3
218#define DCC_EGA40	4
219#define DCC_EGA80	5
220
221/*
222 * NOTE: `va_window' should have a virtual address, but is initialized
223 * with a physical address in the following table, as verify_adapter()
224 * will perform address conversion at run-time.
225 */
226static video_adapter_t adapter_init_value[] = {
227    /* DCC_MONO */
228    { 0, KD_MONO, "mda", 0, 0, 0, 	    IO_MDA, IO_MDASIZE, MONO_CRTC,
229      MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE,
230      0, 0, 0, 7, 0, },
231    /* DCC_CGA40 */
232    { 0, KD_CGA,  "cga", 0, 0, V_ADP_COLOR, IO_CGA, IO_CGASIZE, COLOR_CRTC,
233      CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE,
234      0, 0, 0, 3, 0, },
235    /* DCC_CGA80 */
236    { 0, KD_CGA,  "cga", 0, 0, V_ADP_COLOR, IO_CGA, IO_CGASIZE, COLOR_CRTC,
237      CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE,
238      0, 0, 0, 3, 0, },
239    /* DCC_EGAMONO */
240    { 0, KD_EGA,  "ega", 0, 0, 0,	    IO_MDA, 48,	  MONO_CRTC,
241      EGA_BUF_BASE, EGA_BUF_SIZE, MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE,
242      0, 0, 0, 7, 0, },
243    /* DCC_EGA40 */
244    { 0, KD_EGA,  "ega", 0, 0, V_ADP_COLOR, IO_MDA, 48,	  COLOR_CRTC,
245      EGA_BUF_BASE, EGA_BUF_SIZE, CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE,
246      0, 0, 0, 3, 0, },
247    /* DCC_EGA80 */
248    { 0, KD_EGA,  "ega", 0, 0, V_ADP_COLOR, IO_MDA, 48,	  COLOR_CRTC,
249      EGA_BUF_BASE, EGA_BUF_SIZE, CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE,
250      0, 0, 0, 3, 0, },
251};
252
253static video_adapter_t	biosadapter[2];
254static int		biosadapters = 0;
255
256/* video driver declarations */
257static int			vga_configure(int flags);
258       int			(*vga_sub_configure)(int flags);
259static int			vga_nop(void);
260static vi_probe_t		vga_probe;
261static vi_init_t		vga_init;
262static vi_get_info_t		vga_get_info;
263static vi_query_mode_t		vga_query_mode;
264static vi_set_mode_t		vga_set_mode;
265static vi_save_font_t		vga_save_font;
266static vi_load_font_t		vga_load_font;
267static vi_show_font_t		vga_show_font;
268static vi_save_palette_t	vga_save_palette;
269static vi_load_palette_t	vga_load_palette;
270static vi_set_border_t		vga_set_border;
271static vi_save_state_t		vga_save_state;
272static vi_load_state_t		vga_load_state;
273static vi_set_win_org_t		vga_set_origin;
274static vi_read_hw_cursor_t	vga_read_hw_cursor;
275static vi_set_hw_cursor_t	vga_set_hw_cursor;
276static vi_set_hw_cursor_shape_t	vga_set_hw_cursor_shape;
277static vi_mmap_t		vga_mmap;
278static vi_diag_t		vga_diag;
279
280static video_switch_t vgavidsw = {
281	vga_probe,
282	vga_init,
283	vga_get_info,
284	vga_query_mode,
285	vga_set_mode,
286	vga_save_font,
287	vga_load_font,
288	vga_show_font,
289	vga_save_palette,
290	vga_load_palette,
291	vga_set_border,
292	vga_save_state,
293	vga_load_state,
294	vga_set_origin,
295	vga_read_hw_cursor,
296	vga_set_hw_cursor,
297	vga_set_hw_cursor_shape,
298	(vi_blank_display_t *)vga_nop,
299	vga_mmap,
300	vga_diag,
301};
302
303VIDEO_DRIVER(mda, vgavidsw, NULL);
304VIDEO_DRIVER(cga, vgavidsw, NULL);
305VIDEO_DRIVER(ega, vgavidsw, NULL);
306VIDEO_DRIVER(vga, vgavidsw, vga_configure);
307
308/* VGA BIOS standard video modes */
309#define EOT		(-1)
310#define NA		(-2)
311
312static video_info_t bios_vmode[] = {
313    /* CGA */
314    { M_B40x25,     V_INFO_COLOR, 40, 25, 8,  8, 2, 1,
315      CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0 },
316    { M_C40x25,     V_INFO_COLOR, 40, 25, 8,  8, 4, 1,
317      CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0 },
318    { M_B80x25,     V_INFO_COLOR, 80, 25, 8,  8, 2, 1,
319      CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0 },
320    { M_C80x25,     V_INFO_COLOR, 80, 25, 8,  8, 4, 1,
321      CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0 },
322    /* EGA */
323    { M_ENH_B40x25, V_INFO_COLOR, 40, 25, 8, 14, 2, 1,
324      CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0 },
325    { M_ENH_C40x25, V_INFO_COLOR, 40, 25, 8, 14, 4, 1,
326      CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0 },
327    { M_ENH_B80x25, V_INFO_COLOR, 80, 25, 8, 14, 2, 1,
328      CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0 },
329    { M_ENH_C80x25, V_INFO_COLOR, 80, 25, 8, 14, 4, 1,
330      CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0 },
331    /* VGA */
332    { M_VGA_C40x25, V_INFO_COLOR, 40, 25, 8, 16, 4, 1,
333      CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0 },
334    { M_VGA_M80x25, 0,            80, 25, 8, 16, 2, 1,
335      MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0 },
336    { M_VGA_C80x25, V_INFO_COLOR, 80, 25, 8, 16, 4, 1,
337      CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0 },
338    /* MDA */
339    { M_EGAMONO80x25, 0,          80, 25, 8, 14, 2, 1,
340      MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0 },
341    /* EGA */
342    { M_ENH_B80x43, V_INFO_COLOR, 80, 43, 8,  8, 2, 1,
343      CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0 },
344    { M_ENH_C80x43, V_INFO_COLOR, 80, 43, 8,  8, 4, 1,
345      CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0 },
346    /* VGA */
347    { M_VGA_M80x30, 0,            80, 30, 8, 16, 2, 1,
348      MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0 },
349    { M_VGA_C80x30, V_INFO_COLOR, 80, 30, 8, 16, 4, 1,
350      CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0 },
351    { M_VGA_M80x50, 0,            80, 50, 8,  8, 2, 1,
352      MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0 },
353    { M_VGA_C80x50, V_INFO_COLOR, 80, 50, 8,  8, 4, 1,
354      CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0 },
355    { M_VGA_M80x60, 0,            80, 60, 8,  8, 2, 1,
356      MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0 },
357    { M_VGA_C80x60, V_INFO_COLOR, 80, 60, 8,  8, 4, 1,
358      CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0 },
359#ifndef VGA_NO_MODE_CHANGE
360    /* CGA */
361    { M_BG320,      V_INFO_COLOR | V_INFO_GRAPHICS, 320, 200, 8,  8, 2, 1,
362      CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0 },
363    { M_CG320,      V_INFO_COLOR | V_INFO_GRAPHICS, 320, 200, 8,  8, 2, 1,
364      CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0 },
365    { M_BG640,      V_INFO_COLOR | V_INFO_GRAPHICS, 640, 200, 8,  8, 1, 1,
366      CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0 },
367    /* EGA */
368    { M_CG320_D,    V_INFO_COLOR | V_INFO_GRAPHICS, 320, 200, 8,  8, 4, 4,
369      GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 },
370    { M_CG640_E,    V_INFO_COLOR | V_INFO_GRAPHICS, 640, 200, 8,  8, 4, 4,
371      GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 },
372    { M_EGAMONOAPA, V_INFO_GRAPHICS,                640, 350, 8, 14, 4, 4,
373      GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, 64*1024, 0, 0 },
374    { M_ENHMONOAPA2,V_INFO_GRAPHICS,                640, 350, 8, 14, 4, 4,
375      GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 },
376    { M_CG640x350,  V_INFO_COLOR | V_INFO_GRAPHICS, 640, 350, 8, 14, 2, 2,
377      GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 },
378    { M_ENH_CG640,  V_INFO_COLOR | V_INFO_GRAPHICS, 640, 350, 8, 14, 4, 4,
379      GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 },
380    /* VGA */
381    { M_BG640x480,  V_INFO_COLOR | V_INFO_GRAPHICS, 640, 480, 8, 16, 4, 4,
382      GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 },
383    { M_CG640x480,  V_INFO_COLOR | V_INFO_GRAPHICS, 640, 480, 8, 16, 4, 4,
384      GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 },
385    { M_VGA_CG320,  V_INFO_COLOR | V_INFO_GRAPHICS, 320, 200, 8,  8, 8, 1,
386      GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 },
387    { M_VGA_MODEX,  V_INFO_COLOR | V_INFO_GRAPHICS, 320, 240, 8,  8, 8, 1,
388      GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 },
389#endif /* VGA_NO_MODE_CHANGE */
390
391    { EOT },
392};
393
394static int		init_done = FALSE;
395#if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
396static u_char		*video_mode_ptr = NULL;		/* EGA/VGA */
397static u_char		*video_mode_ptr2 = NULL;	/* CGA/MDA */
398#endif
399static u_char		*mode_map[V_MODE_MAP_SIZE];
400static adp_state_t	adpstate;
401static adp_state_t	adpstate2;
402static int		rows_offset = 1;
403
404/* local macros and functions */
405#define BIOS_SADDRTOLADDR(p) ((((p) & 0xffff0000) >> 12) + ((p) & 0x0000ffff))
406
407#if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
408static void map_mode_table(u_char *map[], u_char *table, int max);
409#endif
410static void clear_mode_map(video_adapter_t *adp, u_char *map[], int max,
411			   int color);
412#if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
413static int map_mode_num(int mode);
414#endif
415static int map_gen_mode_num(int type, int color, int mode);
416static int map_bios_mode_num(int type, int color, int bios_mode);
417static u_char *get_mode_param(int mode);
418#ifndef VGA_NO_BIOS
419static void fill_adapter_param(int code, video_adapter_t *adp);
420#endif
421static int verify_adapter(video_adapter_t *adp);
422static void update_adapter_info(video_adapter_t *adp, video_info_t *info);
423#if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
424#define COMP_IDENTICAL	0
425#define COMP_SIMILAR	1
426#define COMP_DIFFERENT	2
427static int comp_adpregs(u_char *buf1, u_char *buf2);
428#endif
429static int probe_adapters(void);
430
431#ifndef VGA_NO_FONT_LOADING
432#define PARAM_BUFSIZE	6
433static void set_font_mode(video_adapter_t *adp, u_char *buf);
434static void set_normal_mode(video_adapter_t *adp, u_char *buf);
435#endif
436
437static void dump_buffer(u_char *buf, size_t len);
438
439#define	ISMAPPED(pa, width)				\
440	(((pa) <= (u_long)0x1000 - (width)) 		\
441	 || ((pa) >= ISA_HOLE_START && (pa) <= 0x100000 - (width)))
442
443#define	prologue(adp, flag, err)			\
444	if (!init_done || !((adp)->va_flags & (flag)))	\
445	    return (err)
446
447/* a backdoor for the console driver */
448static int
449vga_configure(int flags)
450{
451    int i;
452
453    probe_adapters();
454    for (i = 0; i < biosadapters; ++i) {
455	if (!probe_done(&biosadapter[i]))
456	    continue;
457	biosadapter[i].va_flags |= V_ADP_INITIALIZED;
458	if (!config_done(&biosadapter[i])) {
459	    if (vid_register(&biosadapter[i]) < 0)
460		continue;
461	    biosadapter[i].va_flags |= V_ADP_REGISTERED;
462	}
463    }
464    if (vga_sub_configure != NULL)
465	(*vga_sub_configure)(flags);
466
467    return biosadapters;
468}
469
470/* local subroutines */
471
472#if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
473/* construct the mode parameter map */
474static void
475map_mode_table(u_char *map[], u_char *table, int max)
476{
477    int i;
478
479    for(i = 0; i < max; ++i)
480	map[i] = table + i*V_MODE_PARAM_SIZE;
481    for(; i < V_MODE_MAP_SIZE; ++i)
482	map[i] = NULL;
483}
484#endif /* !VGA_NO_BIOS && !VGA_NO_MODE_CHANGE */
485
486static void
487clear_mode_map(video_adapter_t *adp, u_char *map[], int max, int color)
488{
489    video_info_t info;
490    int i;
491
492    /*
493     * NOTE: we don't touch `bios_vmode[]' because it is shared
494     * by all adapters.
495     */
496    for(i = 0; i < max; ++i) {
497	if (vga_get_info(adp, i, &info))
498	    continue;
499	if ((info.vi_flags & V_INFO_COLOR) != color)
500	    map[i] = NULL;
501    }
502}
503
504#if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
505/* map the non-standard video mode to a known mode number */
506static int
507map_mode_num(int mode)
508{
509    static struct {
510        int from;
511        int to;
512    } mode_map[] = {
513        { M_ENH_B80x43, M_ENH_B80x25 },
514        { M_ENH_C80x43, M_ENH_C80x25 },
515        { M_VGA_M80x30, M_VGA_M80x25 },
516        { M_VGA_C80x30, M_VGA_C80x25 },
517        { M_VGA_M80x50, M_VGA_M80x25 },
518        { M_VGA_C80x50, M_VGA_C80x25 },
519        { M_VGA_M80x60, M_VGA_M80x25 },
520        { M_VGA_C80x60, M_VGA_C80x25 },
521        { M_VGA_MODEX,  M_VGA_CG320 },
522    };
523    int i;
524
525    for (i = 0; i < sizeof(mode_map)/sizeof(mode_map[0]); ++i) {
526        if (mode_map[i].from == mode)
527            return mode_map[i].to;
528    }
529    return mode;
530}
531#endif /* !VGA_NO_BIOS && !VGA_NO_MODE_CHANGE */
532
533/* map a generic video mode to a known mode number */
534static int
535map_gen_mode_num(int type, int color, int mode)
536{
537    static struct {
538	int from;
539	int to_color;
540	int to_mono;
541    } mode_map[] = {
542	{ M_TEXT_80x30,	M_VGA_C80x30, M_VGA_M80x30, },
543	{ M_TEXT_80x43,	M_ENH_C80x43, M_ENH_B80x43, },
544	{ M_TEXT_80x50,	M_VGA_C80x50, M_VGA_M80x50, },
545	{ M_TEXT_80x60,	M_VGA_C80x60, M_VGA_M80x60, },
546    };
547    int i;
548
549    if (mode == M_TEXT_80x25) {
550	switch (type) {
551
552	case KD_VGA:
553	    if (color)
554		return M_VGA_C80x25;
555	    else
556		return M_VGA_M80x25;
557	    break;
558
559	case KD_EGA:
560	    if (color)
561		return M_ENH_C80x25;
562	    else
563		return M_EGAMONO80x25;
564	    break;
565
566	case KD_CGA:
567	    return M_C80x25;
568
569	case KD_MONO:
570	case KD_HERCULES:
571	    return M_EGAMONO80x25;	/* XXX: this name is confusing */
572
573 	default:
574	    return -1;
575	}
576    }
577
578    for (i = 0; i < sizeof(mode_map)/sizeof(mode_map[0]); ++i) {
579        if (mode_map[i].from == mode)
580            return ((color) ? mode_map[i].to_color : mode_map[i].to_mono);
581    }
582    return mode;
583}
584
585/* turn the BIOS video number into our video mode number */
586static int
587map_bios_mode_num(int type, int color, int bios_mode)
588{
589    static int cga_modes[7] = {
590	M_B40x25, M_C40x25,		/* 0, 1 */
591	M_B80x25, M_C80x25,		/* 2, 3 */
592	M_BG320, M_CG320,
593	M_BG640,
594    };
595    static int ega_modes[17] = {
596	M_ENH_B40x25, M_ENH_C40x25,	/* 0, 1 */
597	M_ENH_B80x25, M_ENH_C80x25,	/* 2, 3 */
598	M_BG320, M_CG320,
599	M_BG640,
600	M_EGAMONO80x25,			/* 7 */
601	8, 9, 10, 11, 12,
602	M_CG320_D,
603	M_CG640_E,
604	M_ENHMONOAPA2,			/* XXX: video momery > 64K */
605	M_ENH_CG640,			/* XXX: video momery > 64K */
606    };
607    static int vga_modes[20] = {
608	M_VGA_C40x25, M_VGA_C40x25,	/* 0, 1 */
609	M_VGA_C80x25, M_VGA_C80x25,	/* 2, 3 */
610	M_BG320, M_CG320,
611	M_BG640,
612	M_VGA_M80x25,			/* 7 */
613	8, 9, 10, 11, 12,
614	M_CG320_D,
615	M_CG640_E,
616	M_ENHMONOAPA2,
617	M_ENH_CG640,
618	M_BG640x480, M_CG640x480,
619	M_VGA_CG320,
620    };
621
622    switch (type) {
623
624    case KD_VGA:
625	if (bios_mode < sizeof(vga_modes)/sizeof(vga_modes[0]))
626	    return vga_modes[bios_mode];
627	else if (color)
628	    return M_VGA_C80x25;
629	else
630	    return M_VGA_M80x25;
631	break;
632
633    case KD_EGA:
634	if (bios_mode < sizeof(ega_modes)/sizeof(ega_modes[0]))
635	    return ega_modes[bios_mode];
636	else if (color)
637	    return M_ENH_C80x25;
638	else
639	    return M_EGAMONO80x25;
640	break;
641
642    case KD_CGA:
643	if (bios_mode < sizeof(cga_modes)/sizeof(cga_modes[0]))
644	    return cga_modes[bios_mode];
645	else
646	    return M_C80x25;
647	break;
648
649    case KD_MONO:
650    case KD_HERCULES:
651	return M_EGAMONO80x25;		/* XXX: this name is confusing */
652
653    default:
654	break;
655    }
656    return -1;
657}
658
659/* look up a parameter table entry */
660static u_char
661*get_mode_param(int mode)
662{
663#if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
664    if (mode >= V_MODE_MAP_SIZE)
665	mode = map_mode_num(mode);
666#endif
667    if ((mode >= 0) && (mode < V_MODE_MAP_SIZE))
668	return mode_map[mode];
669    else
670	return NULL;
671}
672
673#ifndef VGA_NO_BIOS
674static void
675fill_adapter_param(int code, video_adapter_t *adp)
676{
677    static struct {
678	int primary;
679	int secondary;
680    } dcc[] = {
681	{ DCC_MONO, 			DCC_EGA40 /* CGA monitor */ },
682	{ DCC_MONO, 			DCC_EGA80 /* CGA monitor */ },
683	{ DCC_MONO, 			DCC_EGA80 /* CGA emulation */ },
684	{ DCC_MONO, 			DCC_EGA80 },
685	{ DCC_CGA40, 			DCC_EGAMONO },
686	{ DCC_CGA80, 			DCC_EGAMONO },
687	{ DCC_EGA40 /* CGA monitor */, 	DCC_MONO},
688	{ DCC_EGA80 /* CGA monitor */, 	DCC_MONO},
689	{ DCC_EGA80 /* CGA emulation */,DCC_MONO },
690	{ DCC_EGA80, 			DCC_MONO },
691	{ DCC_EGAMONO, 			DCC_CGA40 },
692	{ DCC_EGAMONO, 			DCC_CGA40 },
693    };
694
695    if ((code < 0) || (code >= sizeof(dcc)/sizeof(dcc[0]))) {
696	adp[V_ADP_PRIMARY] = adapter_init_value[DCC_MONO];
697	adp[V_ADP_SECONDARY] = adapter_init_value[DCC_CGA80];
698    } else {
699	adp[V_ADP_PRIMARY] = adapter_init_value[dcc[code].primary];
700	adp[V_ADP_SECONDARY] = adapter_init_value[dcc[code].secondary];
701    }
702}
703#endif /* VGA_NO_BIOS */
704
705static int
706verify_adapter(video_adapter_t *adp)
707{
708    vm_offset_t buf;
709    u_int16_t v;
710#if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
711    u_int32_t p;
712#endif
713
714    buf = BIOS_PADDRTOVADDR(adp->va_window);
715    v = readw(buf);
716    writew(buf, 0xA55A);
717    if (readw(buf) != 0xA55A)
718	return 1;
719    writew(buf, v);
720
721    switch (adp->va_type) {
722
723    case KD_EGA:
724	outb(adp->va_crtc_addr, 7);
725	if (inb(adp->va_crtc_addr) == 7) {
726	    adp->va_type = KD_VGA;
727	    adp->va_name = "vga";
728	    adp->va_flags |= V_ADP_STATESAVE | V_ADP_PALETTE;
729	}
730	adp->va_flags |= V_ADP_STATELOAD | V_ADP_BORDER;
731	/* the color adapter may be in the 40x25 mode... XXX */
732
733#if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
734	/* get the BIOS video mode pointer */
735	p = *(u_int32_t *)BIOS_PADDRTOVADDR(0x4a8);
736	p = BIOS_SADDRTOLADDR(p);
737	if (ISMAPPED(p, sizeof(u_int32_t))) {
738	    p = *(u_int32_t *)BIOS_PADDRTOVADDR(p);
739	    p = BIOS_SADDRTOLADDR(p);
740	    if (ISMAPPED(p, V_MODE_PARAM_SIZE))
741		video_mode_ptr = (u_char *)BIOS_PADDRTOVADDR(p);
742	}
743#endif
744	break;
745
746    case KD_CGA:
747	adp->va_flags |= V_ADP_COLOR | V_ADP_BORDER;
748	/* may be in the 40x25 mode... XXX */
749#if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
750	/* get the BIOS video mode pointer */
751	p = *(u_int32_t *)BIOS_PADDRTOVADDR(0x1d*4);
752	p = BIOS_SADDRTOLADDR(p);
753	video_mode_ptr2 = (u_char *)BIOS_PADDRTOVADDR(p);
754#endif
755	break;
756
757    case KD_MONO:
758#if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
759	/* get the BIOS video mode pointer */
760	p = *(u_int32_t *)BIOS_PADDRTOVADDR(0x1d*4);
761	p = BIOS_SADDRTOLADDR(p);
762	video_mode_ptr2 = (u_char *)BIOS_PADDRTOVADDR(p);
763#endif
764	break;
765    }
766
767    return 0;
768}
769
770static void
771update_adapter_info(video_adapter_t *adp, video_info_t *info)
772{
773    adp->va_flags &= ~V_ADP_COLOR;
774    adp->va_flags |=
775	(info->vi_flags & V_INFO_COLOR) ? V_ADP_COLOR : 0;
776    adp->va_crtc_addr =
777	(adp->va_flags & V_ADP_COLOR) ? COLOR_CRTC : MONO_CRTC;
778    adp->va_window = BIOS_PADDRTOVADDR(info->vi_window);
779    adp->va_window_size = info->vi_window_size;
780    adp->va_window_gran = info->vi_window_gran;
781    if (info->vi_buffer_size == 0) {
782    	adp->va_buffer = 0;
783    	adp->va_buffer_size = 0;
784    } else {
785    	adp->va_buffer = BIOS_PADDRTOVADDR(info->vi_buffer);
786    	adp->va_buffer_size = info->vi_buffer_size;
787    }
788    if (info->vi_flags & V_INFO_GRAPHICS) {
789	switch (info->vi_depth/info->vi_planes) {
790	case 1:
791	    adp->va_line_width = info->vi_width/8;
792	    break;
793	case 2:
794	    adp->va_line_width = info->vi_width/4;
795	    break;
796	case 4:
797	    adp->va_line_width = info->vi_width/2;
798	    break;
799	case 8:
800	default: /* shouldn't happen */
801	    adp->va_line_width = info->vi_width;
802	    break;
803	}
804    } else {
805	adp->va_line_width = info->vi_width;
806    }
807    bcopy(info, &adp->va_info, sizeof(adp->va_info));
808}
809
810#if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
811/* compare two parameter table entries */
812static int
813comp_adpregs(u_char *buf1, u_char *buf2)
814{
815    static struct {
816        u_char mask;
817    } params[V_MODE_PARAM_SIZE] = {
818	{0xff}, {0x00}, {0xff}, 		/* COLS, ROWS, POINTS */
819	{0x00}, {0x00}, 			/* page length */
820	{0xfe}, {0xff}, {0xff}, {0xff},		/* sequencer registers */
821	{0xf3},					/* misc register */
822	{0xff}, {0xff}, {0xff}, {0x7f}, {0xff},	/* CRTC */
823	{0xff}, {0xff}, {0xff}, {0x7f}, {0xff},
824	{0x00}, {0x00}, {0x00}, {0x00}, {0x00},
825	{0x00}, {0xff}, {0x7f}, {0xff}, {0xff},
826	{0x7f}, {0xff}, {0xff}, {0xef}, {0xff},
827	{0xff}, {0xff}, {0xff}, {0xff}, {0xff},	/* attribute controller regs */
828	{0xff}, {0xff}, {0xff}, {0xff}, {0xff},
829	{0xff}, {0xff}, {0xff}, {0xff}, {0xff},
830	{0xff}, {0xff}, {0xff}, {0xff}, {0xf0},
831	{0xff}, {0xff}, {0xff}, {0xff}, {0xff},	/* GDC register */
832	{0xff}, {0xff}, {0xff}, {0xff},
833    };
834    int identical = TRUE;
835    int i;
836
837    if ((buf1 == NULL) || (buf2 == NULL))
838	return COMP_DIFFERENT;
839
840    for (i = 0; i < sizeof(params)/sizeof(params[0]); ++i) {
841	if (params[i].mask == 0)	/* don't care */
842	    continue;
843	if ((buf1[i] & params[i].mask) != (buf2[i] & params[i].mask))
844	    return COMP_DIFFERENT;
845	if (buf1[i] != buf2[i])
846	    identical = FALSE;
847    }
848    return (identical) ? COMP_IDENTICAL : COMP_SIMILAR;
849}
850#endif /* !VGA_NO_BIOS && !VGA_NO_MODE_CHANGE */
851
852/* probe video adapters and return the number of detected adapters */
853static int
854probe_adapters(void)
855{
856    video_adapter_t *adp;
857    video_info_t info;
858    int i;
859
860    /* do this test only once */
861    if (init_done)
862	return biosadapters;
863    init_done = TRUE;
864
865    /*
866     * Locate display adapters.
867     * The AT architecture supports upto two adapters. `syscons' allows
868     * the following combinations of adapters:
869     *     1) MDA + CGA
870     *     2) MDA + EGA/VGA color
871     *     3) CGA + EGA/VGA mono
872     * Note that `syscons' doesn't bother with MCGA as it is only
873     * avaiable for low end PS/2 models which has 80286 or earlier CPUs,
874     * thus, they are not running FreeBSD!
875     * When there are two adapaters in the system, one becomes `primary'
876     * and the other `secondary'. The EGA adapter has a set of DIP
877     * switches on board for this information and the EGA BIOS copies
878     * it in the BIOS data area BIOSDATA_VIDEOSWITCH (40:88).
879     * The VGA BIOS has more sophisticated mechanism and has this
880     * information in BIOSDATA_DCCINDEX (40:8a), but it also maintains
881     * compatibility with the EGA BIOS by updating BIOSDATA_VIDEOSWITCH.
882     */
883
884    /*
885     * Check rtc and BIOS data area.
886     * XXX: we don't use BIOSDATA_EQUIPMENT, since it is not a dead
887     * copy of RTC_EQUIPMENT.  Bits 4 and 5 of ETC_EQUIPMENT are
888     * zeros for EGA and VGA.  However, the EGA/VGA BIOS sets
889     * these bits in BIOSDATA_EQUIPMENT according to the monitor
890     * type detected.
891     */
892#ifndef VGA_NO_BIOS
893    switch ((rtcin(RTC_EQUIPMENT) >> 4) & 3) {	/* bit 4 and 5 */
894    case 0:
895	/* EGA/VGA */
896	fill_adapter_param(readb(BIOS_PADDRTOVADDR(0x488)) & 0x0f,
897			   biosadapter);
898	break;
899    case 1:
900	/* CGA 40x25 */
901	/* FIXME: switch to the 80x25 mode? XXX */
902	biosadapter[V_ADP_PRIMARY] = adapter_init_value[DCC_CGA40];
903	biosadapter[V_ADP_SECONDARY] = adapter_init_value[DCC_MONO];
904	break;
905    case 2:
906	/* CGA 80x25 */
907	biosadapter[V_ADP_PRIMARY] = adapter_init_value[DCC_CGA80];
908	biosadapter[V_ADP_SECONDARY] = adapter_init_value[DCC_MONO];
909	break;
910    case 3:
911	/* MDA */
912	biosadapter[V_ADP_PRIMARY] = adapter_init_value[DCC_MONO];
913	biosadapter[V_ADP_SECONDARY] = adapter_init_value[DCC_CGA80];
914	break;
915    }
916#else
917    /* assume EGA/VGA? XXX */
918    biosadapter[V_ADP_PRIMARY] = adapter_init_value[DCC_EGA80];
919    biosadapter[V_ADP_SECONDARY] = adapter_init_value[DCC_MONO];
920#endif /* VGA_NO_BIOS */
921
922    biosadapters = 0;
923    if (verify_adapter(&biosadapter[V_ADP_SECONDARY]) == 0) {
924	++biosadapters;
925	biosadapter[V_ADP_SECONDARY].va_flags |= V_ADP_PROBED;
926	biosadapter[V_ADP_SECONDARY].va_mode =
927	    biosadapter[V_ADP_SECONDARY].va_initial_mode =
928	    map_bios_mode_num(biosadapter[V_ADP_SECONDARY].va_type,
929			      biosadapter[V_ADP_SECONDARY].va_flags
930				  & V_ADP_COLOR,
931			      biosadapter[V_ADP_SECONDARY].va_initial_bios_mode);
932    } else {
933	biosadapter[V_ADP_SECONDARY].va_type = -1;
934    }
935    if (verify_adapter(&biosadapter[V_ADP_PRIMARY]) == 0) {
936	++biosadapters;
937	biosadapter[V_ADP_PRIMARY].va_flags |= V_ADP_PROBED;
938#ifndef VGA_NO_BIOS
939	biosadapter[V_ADP_PRIMARY].va_initial_bios_mode =
940	    readb(BIOS_PADDRTOVADDR(0x449));
941#else
942	biosadapter[V_ADP_PRIMARY].va_initial_bios_mode = 3;	/* XXX */
943#endif
944	biosadapter[V_ADP_PRIMARY].va_mode =
945	    biosadapter[V_ADP_PRIMARY].va_initial_mode =
946	    map_bios_mode_num(biosadapter[V_ADP_PRIMARY].va_type,
947			      biosadapter[V_ADP_PRIMARY].va_flags & V_ADP_COLOR,
948			      biosadapter[V_ADP_PRIMARY].va_initial_bios_mode);
949    } else {
950	biosadapter[V_ADP_PRIMARY] = biosadapter[V_ADP_SECONDARY];
951	biosadapter[V_ADP_SECONDARY].va_type = -1;
952    }
953    if (biosadapters == 0)
954	return biosadapters;
955    biosadapter[V_ADP_PRIMARY].va_unit = V_ADP_PRIMARY;
956    biosadapter[V_ADP_SECONDARY].va_unit = V_ADP_SECONDARY;
957
958#if 0 /* we don't need these... */
959    fb_init_struct(&biosadapter[V_ADP_PRIMARY], ...);
960    fb_init_struct(&biosadapter[V_ADP_SECONDARY], ...);
961#endif
962
963#if 0
964    /*
965     * We cannot have two video adapter of the same type; there must be
966     * only one of color or mono adapter, or one each of them.
967     */
968    if (biosadapters > 1) {
969	if (!((biosadapter[0].va_flags ^ biosadapter[1].va_flags)
970	      & V_ADP_COLOR))
971	    /* we have two mono or color adapters!! */
972	    return (biosadapters = 0);
973    }
974#endif
975
976    /*
977     * Ensure a zero start address.  This is mainly to recover after
978     * switching from pcvt using userconfig().  The registers are w/o
979     * for old hardware so it's too hard to relocate the active screen
980     * memory.
981     * This must be done before vga_save_state() for VGA.
982     */
983    outb(biosadapter[V_ADP_PRIMARY].va_crtc_addr, 12);
984    outb(biosadapter[V_ADP_PRIMARY].va_crtc_addr + 1, 0);
985    outb(biosadapter[V_ADP_PRIMARY].va_crtc_addr, 13);
986    outb(biosadapter[V_ADP_PRIMARY].va_crtc_addr + 1, 0);
987
988    /* the video mode parameter table in EGA/VGA BIOS */
989    /* NOTE: there can be only one EGA/VGA, wheather color or mono,
990     * recognized by the video BIOS.
991     */
992    if ((biosadapter[V_ADP_PRIMARY].va_type == KD_EGA) ||
993	(biosadapter[V_ADP_PRIMARY].va_type == KD_VGA)) {
994	adp = &biosadapter[V_ADP_PRIMARY];
995    } else if ((biosadapter[V_ADP_SECONDARY].va_type == KD_EGA) ||
996	       (biosadapter[V_ADP_SECONDARY].va_type == KD_VGA)) {
997	adp = &biosadapter[V_ADP_SECONDARY];
998    } else {
999	adp = NULL;
1000    }
1001    bzero(mode_map, sizeof(mode_map));
1002    if (adp != NULL) {
1003	if (adp->va_type == KD_VGA) {
1004	    vga_save_state(adp, &adpstate, sizeof(adpstate));
1005#if defined(VGA_NO_BIOS) || defined(VGA_NO_MODE_CHANGE)
1006	    mode_map[adp->va_initial_mode] = adpstate.regs;
1007	    rows_offset = 1;
1008#else /* VGA_NO_BIOS || VGA_NO_MODE_CHANGE */
1009	    if (video_mode_ptr == NULL) {
1010		mode_map[adp->va_initial_mode] = adpstate.regs;
1011		rows_offset = 1;
1012	    } else {
1013		/* discard the table if we are not familiar with it... */
1014		u_char *mp;
1015		map_mode_table(mode_map, video_mode_ptr, M_VGA_CG320 + 1);
1016		mp = get_mode_param(adp->va_initial_mode);
1017		if (mp != NULL)
1018		    bcopy(mp, adpstate2.regs, sizeof(adpstate2.regs));
1019		switch (comp_adpregs(adpstate.regs, mp)) {
1020		case COMP_IDENTICAL:
1021		    /*
1022		     * OK, this parameter table looks reasonably familiar
1023		     * to us...
1024		     */
1025		    /*
1026		     * This is a kludge for Toshiba DynaBook SS433
1027		     * whose BIOS video mode table entry has the actual #
1028		     * of rows at the offset 1; BIOSes from other
1029		     * manufacturers store the # of rows - 1 there. XXX
1030		     */
1031		    rows_offset = adpstate.regs[1] + 1 - mp[1];
1032		    break;
1033
1034		case COMP_SIMILAR:
1035		    /*
1036		     * Not exactly the same, but similar enough to be
1037		     * trusted. However, use the saved register values
1038		     * for the initial mode and other modes which are
1039		     * based on the initial mode.
1040		     */
1041		    mode_map[adp->va_initial_mode] = adpstate.regs;
1042		    rows_offset = adpstate.regs[1] + 1 - mp[1];
1043		    adpstate.regs[1] -= rows_offset - 1;
1044		    break;
1045
1046		case COMP_DIFFERENT:
1047		default:
1048		    /*
1049		     * Don't use the paramter table in BIOS. It doesn't
1050		     * look familiar to us. Video mode switching is allowed
1051		     * only if the new mode is the same as or based on
1052		     * the initial mode.
1053		     */
1054		    video_mode_ptr = NULL;
1055		    bzero(mode_map, sizeof(mode_map));
1056		    mode_map[adp->va_initial_mode] = adpstate.regs;
1057		    rows_offset = 1;
1058		    break;
1059		}
1060	    }
1061#endif /* VGA_NO_BIOS || VGA_NO_MODE_CHANGE */
1062
1063#ifndef VGA_NO_MODE_CHANGE
1064	    adp->va_flags |= V_ADP_MODECHANGE;
1065#endif
1066#ifndef VGA_NO_FONT_LOADING
1067	    adp->va_flags |= V_ADP_FONT;
1068#endif
1069	} else if (adp->va_type == KD_EGA) {
1070#if defined(VGA_NO_BIOS) || defined(VGA_NO_MODE_CHANGE)
1071	    rows_offset = 1;
1072#else /* VGA_NO_BIOS || VGA_NO_MODE_CHANGE */
1073	    if (video_mode_ptr == NULL) {
1074		rows_offset = 1;
1075	    } else {
1076		u_char *mp;
1077		map_mode_table(mode_map, video_mode_ptr, M_ENH_C80x25 + 1);
1078		/* XXX how can one validate the EGA table... */
1079		mp = get_mode_param(adp->va_initial_mode);
1080		if (mp != NULL) {
1081		    adp->va_flags |= V_ADP_MODECHANGE;
1082#ifndef VGA_NO_FONT_LOADING
1083		    adp->va_flags |= V_ADP_FONT;
1084#endif
1085		    rows_offset = 1;
1086		} else {
1087		    /*
1088		     * This is serious. We will not be able to switch video
1089		     * modes at all...
1090		     */
1091		    video_mode_ptr = NULL;
1092		    bzero(mode_map, sizeof(mode_map));
1093		    rows_offset = 1;
1094                }
1095	    }
1096#endif /* VGA_NO_BIOS || VGA_NO_MODE_CHANGE */
1097	}
1098    }
1099
1100    /* remove conflicting modes if we have more than one adapter */
1101    if (biosadapters > 1) {
1102	for (i = 0; i < biosadapters; ++i) {
1103	    if (!(biosadapter[i].va_flags & V_ADP_MODECHANGE))
1104		continue;
1105	    clear_mode_map(&biosadapter[i], mode_map, M_VGA_CG320 + 1,
1106			   (biosadapter[i].va_flags & V_ADP_COLOR) ?
1107			       V_INFO_COLOR : 0);
1108	    if ((biosadapter[i].va_type == KD_VGA)
1109		|| (biosadapter[i].va_type == KD_EGA)) {
1110		biosadapter[i].va_io_base =
1111		    (biosadapter[i].va_flags & V_ADP_COLOR) ?
1112			IO_VGA : IO_MDA;
1113		biosadapter[i].va_io_size = 32;
1114	    }
1115	}
1116    }
1117
1118    /* buffer address */
1119    vga_get_info(&biosadapter[V_ADP_PRIMARY],
1120		 biosadapter[V_ADP_PRIMARY].va_initial_mode, &info);
1121    update_adapter_info(&biosadapter[V_ADP_PRIMARY], &info);
1122
1123    if (biosadapters > 1) {
1124	vga_get_info(&biosadapter[V_ADP_SECONDARY],
1125		     biosadapter[V_ADP_SECONDARY].va_initial_mode, &info);
1126	update_adapter_info(&biosadapter[V_ADP_SECONDARY], &info);
1127    }
1128
1129    /*
1130     * XXX: we should verify the following values for the primary adapter...
1131     * crtc I/O port address: *(u_int16_t *)BIOS_PADDRTOVADDR(0x463);
1132     * color/mono display: (*(u_int8_t *)BIOS_PADDRTOVADDR(0x487) & 0x02)
1133     *                     ? 0 : V_ADP_COLOR;
1134     * columns: *(u_int8_t *)BIOS_PADDRTOVADDR(0x44a);
1135     * rows: *(u_int8_t *)BIOS_PADDRTOVADDR(0x484);
1136     * font size: *(u_int8_t *)BIOS_PADDRTOVADDR(0x485);
1137     * buffer size: *(u_int16_t *)BIOS_PADDRTOVADDR(0x44c);
1138     */
1139
1140    return biosadapters;
1141}
1142
1143/* entry points */
1144
1145static int
1146vga_nop(void)
1147{
1148    return 0;
1149}
1150
1151static int
1152vga_probe(int unit, video_adapter_t **adpp, void *arg, int flags)
1153{
1154    probe_adapters();
1155    if (unit >= biosadapters)
1156	return ENXIO;
1157
1158    *adpp = &biosadapter[unit];
1159
1160    return 0;
1161}
1162
1163static int
1164vga_init(int unit, video_adapter_t *adp, int flags)
1165{
1166    if ((unit >= biosadapters) || (adp == NULL) || !probe_done(adp))
1167	return ENXIO;
1168
1169    if (!init_done(adp)) {
1170	/* nothing to do really... */
1171	adp->va_flags |= V_ADP_INITIALIZED;
1172    }
1173
1174    if (!config_done(adp)) {
1175	if (vid_register(adp) < 0)
1176		return ENXIO;
1177	adp->va_flags |= V_ADP_REGISTERED;
1178    }
1179    if (vga_sub_configure != NULL)
1180	(*vga_sub_configure)(0);
1181
1182    return 0;
1183}
1184
1185/*
1186 * get_info():
1187 * Return the video_info structure of the requested video mode.
1188 *
1189 * all adapters
1190 */
1191static int
1192vga_get_info(video_adapter_t *adp, int mode, video_info_t *info)
1193{
1194    int i;
1195
1196    if (!init_done)
1197	return 1;
1198
1199    mode = map_gen_mode_num(adp->va_type, adp->va_flags & V_ADP_COLOR, mode);
1200#ifndef VGA_NO_MODE_CHANGE
1201    if (adp->va_flags & V_ADP_MODECHANGE) {
1202	/*
1203	 * If the parameter table entry for this mode is not found,
1204	 * the mode is not supported...
1205	 */
1206	if (get_mode_param(mode) == NULL)
1207	    return 1;
1208    } else
1209#endif /* VGA_NO_MODE_CHANGE */
1210    {
1211	/*
1212	 * Even if we don't support video mode switching on this adapter,
1213	 * the information on the initial (thus current) video mode
1214	 * should be made available.
1215	 */
1216	if (mode != adp->va_initial_mode)
1217	    return 1;
1218    }
1219
1220    for (i = 0; bios_vmode[i].vi_mode != EOT; ++i) {
1221	if (bios_vmode[i].vi_mode == NA)
1222	    continue;
1223	if (mode == bios_vmode[i].vi_mode) {
1224	    *info = bios_vmode[i];
1225	    return 0;
1226	}
1227    }
1228    return 1;
1229}
1230
1231/*
1232 * query_mode():
1233 * Find a video mode matching the requested parameters.
1234 * Fields filled with 0 are considered "don't care" fields and
1235 * match any modes.
1236 *
1237 * all adapters
1238 */
1239static int
1240vga_query_mode(video_adapter_t *adp, video_info_t *info)
1241{
1242    video_info_t buf;
1243    int i;
1244
1245    if (!init_done)
1246	return -1;
1247
1248    for (i = 0; bios_vmode[i].vi_mode != EOT; ++i) {
1249	if (bios_vmode[i].vi_mode == NA)
1250	    continue;
1251
1252	if ((info->vi_width != 0)
1253	    && (info->vi_width != bios_vmode[i].vi_width))
1254		continue;
1255	if ((info->vi_height != 0)
1256	    && (info->vi_height != bios_vmode[i].vi_height))
1257		continue;
1258	if ((info->vi_cwidth != 0)
1259	    && (info->vi_cwidth != bios_vmode[i].vi_cwidth))
1260		continue;
1261	if ((info->vi_cheight != 0)
1262	    && (info->vi_cheight != bios_vmode[i].vi_cheight))
1263		continue;
1264	if ((info->vi_depth != 0)
1265	    && (info->vi_depth != bios_vmode[i].vi_depth))
1266		continue;
1267	if ((info->vi_planes != 0)
1268	    && (info->vi_planes != bios_vmode[i].vi_planes))
1269		continue;
1270	/* XXX: should check pixel format, memory model */
1271	if ((info->vi_flags != 0)
1272	    && (info->vi_flags != bios_vmode[i].vi_flags))
1273		continue;
1274
1275	/* verify if this mode is supported on this adapter */
1276	if (vga_get_info(adp, bios_vmode[i].vi_mode, &buf))
1277		continue;
1278	return bios_vmode[i].vi_mode;
1279    }
1280    return -1;
1281}
1282
1283/*
1284 * set_mode():
1285 * Change the video mode.
1286 *
1287 * EGA/VGA
1288 */
1289static int
1290vga_set_mode(video_adapter_t *adp, int mode)
1291{
1292#ifndef VGA_NO_MODE_CHANGE
1293    video_info_t info;
1294    adp_state_t params;
1295
1296    prologue(adp, V_ADP_MODECHANGE, 1);
1297
1298    mode = map_gen_mode_num(adp->va_type,
1299			    adp->va_flags & V_ADP_COLOR, mode);
1300    if (vga_get_info(adp, mode, &info))
1301	return 1;
1302    params.sig = V_STATE_SIG;
1303    bcopy(get_mode_param(mode), params.regs, sizeof(params.regs));
1304
1305    switch (mode) {
1306    case M_VGA_C80x60: case M_VGA_M80x60:
1307	params.regs[2]  = 0x08;
1308	params.regs[19] = 0x47;
1309	goto special_480l;
1310
1311    case M_VGA_C80x30: case M_VGA_M80x30:
1312	params.regs[19] = 0x4f;
1313special_480l:
1314	params.regs[9] |= 0xc0;
1315	params.regs[16] = 0x08;
1316	params.regs[17] = 0x3e;
1317	params.regs[26] = 0xea;
1318	params.regs[28] = 0xdf;
1319	params.regs[31] = 0xe7;
1320	params.regs[32] = 0x04;
1321	goto setup_mode;
1322
1323    case M_ENH_C80x43: case M_ENH_B80x43:
1324	params.regs[28] = 87;
1325	goto special_80x50;
1326
1327    case M_VGA_C80x50: case M_VGA_M80x50:
1328special_80x50:
1329	params.regs[2] = 8;
1330	params.regs[19] = 7;
1331	goto setup_mode;
1332
1333    case M_VGA_C40x25: case M_VGA_C80x25:
1334    case M_VGA_M80x25:
1335    case M_B40x25:     case M_C40x25:
1336    case M_B80x25:     case M_C80x25:
1337    case M_ENH_B40x25: case M_ENH_C40x25:
1338    case M_ENH_B80x25: case M_ENH_C80x25:
1339    case M_EGAMONO80x25:
1340
1341setup_mode:
1342	vga_load_state(adp, &params);
1343	break;
1344
1345    case M_VGA_MODEX:
1346	/* "unchain" the VGA mode */
1347	params.regs[5-1+0x04] &= 0xf7;
1348	params.regs[5-1+0x04] |= 0x04;
1349	/* turn off doubleword mode */
1350	params.regs[10+0x14] &= 0xbf;
1351	/* turn off word adressing */
1352	params.regs[10+0x17] |= 0x40;
1353	/* set logical screen width */
1354	params.regs[10+0x13] = 80;
1355	/* set 240 lines */
1356	params.regs[10+0x11] = 0x2c;
1357	params.regs[10+0x06] = 0x0d;
1358	params.regs[10+0x07] = 0x3e;
1359	params.regs[10+0x10] = 0xea;
1360	params.regs[10+0x11] = 0xac;
1361	params.regs[10+0x12] = 0xdf;
1362	params.regs[10+0x15] = 0xe7;
1363	params.regs[10+0x16] = 0x06;
1364	/* set vertical sync polarity to reflect aspect ratio */
1365	params.regs[9] = 0xe3;
1366	goto setup_grmode;
1367
1368    case M_BG320:     case M_CG320:     case M_BG640:
1369    case M_CG320_D:   case M_CG640_E:
1370    case M_CG640x350: case M_ENH_CG640:
1371    case M_BG640x480: case M_CG640x480: case M_VGA_CG320:
1372
1373setup_grmode:
1374	vga_load_state(adp, &params);
1375	break;
1376
1377    default:
1378	return 1;
1379    }
1380
1381    adp->va_mode = mode;
1382    update_adapter_info(adp, &info);
1383
1384    /* move hardware cursor out of the way */
1385    (*vidsw[adp->va_index]->set_hw_cursor)(adp, -1, -1);
1386
1387    return 0;
1388#else /* VGA_NO_MODE_CHANGE */
1389    return 1;
1390#endif /* VGA_NO_MODE_CHANGE */
1391}
1392
1393#ifndef VGA_NO_FONT_LOADING
1394
1395static void
1396set_font_mode(video_adapter_t *adp, u_char *buf)
1397{
1398    u_char *mp;
1399    int s;
1400
1401    s = splhigh();
1402
1403    /* save register values */
1404    if (adp->va_type == KD_VGA) {
1405	outb(TSIDX, 0x02); buf[0] = inb(TSREG);
1406	outb(TSIDX, 0x04); buf[1] = inb(TSREG);
1407	outb(GDCIDX, 0x04); buf[2] = inb(GDCREG);
1408	outb(GDCIDX, 0x05); buf[3] = inb(GDCREG);
1409	outb(GDCIDX, 0x06); buf[4] = inb(GDCREG);
1410	inb(adp->va_crtc_addr + 6);
1411	outb(ATC, 0x10); buf[5] = inb(ATC + 1);
1412    } else /* if (adp->va_type == KD_EGA) */ {
1413	/*
1414	 * EGA cannot be read; copy parameters from the mode parameter
1415	 * table.
1416	 */
1417	mp = get_mode_param(adp->va_mode);
1418	buf[0] = mp[5 + 0x02 - 1];
1419	buf[1] = mp[5 + 0x04 - 1];
1420	buf[2] = mp[55 + 0x04];
1421	buf[3] = mp[55 + 0x05];
1422	buf[4] = mp[55 + 0x06];
1423	buf[5] = mp[35 + 0x10];
1424    }
1425
1426    /* setup vga for loading fonts */
1427    inb(adp->va_crtc_addr + 6);			/* reset flip-flop */
1428    outb(ATC, 0x10); outb(ATC, buf[5] & ~0x01);
1429    inb(adp->va_crtc_addr + 6);			/* reset flip-flop */
1430    outb(ATC, 0x20);				/* enable palette */
1431
1432#if VGA_SLOW_IOACCESS
1433#ifdef VGA_ALT_SEQACCESS
1434    outb(TSIDX, 0x00); outb(TSREG, 0x01);
1435#endif
1436    outb(TSIDX, 0x02); outb(TSREG, 0x04);
1437    outb(TSIDX, 0x04); outb(TSREG, 0x07);
1438#ifdef VGA_ALT_SEQACCESS
1439    outb(TSIDX, 0x00); outb(TSREG, 0x03);
1440#endif
1441    outb(GDCIDX, 0x04); outb(GDCREG, 0x02);
1442    outb(GDCIDX, 0x05); outb(GDCREG, 0x00);
1443    outb(GDCIDX, 0x06); outb(GDCREG, 0x04);
1444#else /* VGA_SLOW_IOACCESS */
1445#ifdef VGA_ALT_SEQACCESS
1446    outw(TSIDX, 0x0100);
1447#endif
1448    outw(TSIDX, 0x0402);
1449    outw(TSIDX, 0x0704);
1450#ifdef VGA_ALT_SEQACCESS
1451    outw(TSIDX, 0x0300);
1452#endif
1453    outw(GDCIDX, 0x0204);
1454    outw(GDCIDX, 0x0005);
1455    outw(GDCIDX, 0x0406);               /* addr = a0000, 64kb */
1456#endif /* VGA_SLOW_IOACCESS */
1457
1458    splx(s);
1459}
1460
1461static void
1462set_normal_mode(video_adapter_t *adp, u_char *buf)
1463{
1464    int s;
1465
1466    s = splhigh();
1467
1468    /* setup vga for normal operation mode again */
1469    inb(adp->va_crtc_addr + 6);			/* reset flip-flop */
1470    outb(ATC, 0x10); outb(ATC, buf[5]);
1471    inb(adp->va_crtc_addr + 6);			/* reset flip-flop */
1472    outb(ATC, 0x20);				/* enable palette */
1473
1474#if VGA_SLOW_IOACCESS
1475#ifdef VGA_ALT_SEQACCESS
1476    outb(TSIDX, 0x00); outb(TSREG, 0x01);
1477#endif
1478    outb(TSIDX, 0x02); outb(TSREG, buf[0]);
1479    outb(TSIDX, 0x04); outb(TSREG, buf[1]);
1480#ifdef VGA_ALT_SEQACCESS
1481    outb(TSIDX, 0x00); outb(TSREG, 0x03);
1482#endif
1483    outb(GDCIDX, 0x04); outb(GDCREG, buf[2]);
1484    outb(GDCIDX, 0x05); outb(GDCREG, buf[3]);
1485    if (adp->va_crtc_addr == MONO_CRTC) {
1486	outb(GDCIDX, 0x06); outb(GDCREG,(buf[4] & 0x03) | 0x08);
1487    } else {
1488	outb(GDCIDX, 0x06); outb(GDCREG,(buf[4] & 0x03) | 0x0c);
1489    }
1490#else /* VGA_SLOW_IOACCESS */
1491#ifdef VGA_ALT_SEQACCESS
1492    outw(TSIDX, 0x0100);
1493#endif
1494    outw(TSIDX, 0x0002 | (buf[0] << 8));
1495    outw(TSIDX, 0x0004 | (buf[1] << 8));
1496#ifdef VGA_ALT_SEQACCESS
1497    outw(TSIDX, 0x0300);
1498#endif
1499    outw(GDCIDX, 0x0004 | (buf[2] << 8));
1500    outw(GDCIDX, 0x0005 | (buf[3] << 8));
1501    if (adp->va_crtc_addr == MONO_CRTC)
1502        outw(GDCIDX, 0x0006 | (((buf[4] & 0x03) | 0x08)<<8));
1503    else
1504        outw(GDCIDX, 0x0006 | (((buf[4] & 0x03) | 0x0c)<<8));
1505#endif /* VGA_SLOW_IOACCESS */
1506
1507    splx(s);
1508}
1509
1510#endif /* VGA_NO_FONT_LOADING */
1511
1512/*
1513 * save_font():
1514 * Read the font data in the requested font page from the video adapter.
1515 *
1516 * EGA/VGA
1517 */
1518static int
1519vga_save_font(video_adapter_t *adp, int page, int fontsize, u_char *data,
1520	      int ch, int count)
1521{
1522#ifndef VGA_NO_FONT_LOADING
1523    u_char buf[PARAM_BUFSIZE];
1524    u_int32_t segment;
1525    int c;
1526#ifdef VGA_ALT_SEQACCESS
1527    int s;
1528    u_char val = 0;
1529#endif
1530
1531    prologue(adp, V_ADP_FONT, 1);
1532
1533    if (fontsize < 14) {
1534	/* FONT_8 */
1535	fontsize = 8;
1536    } else if (fontsize >= 32) {
1537	fontsize = 32;
1538    } else if (fontsize >= 16) {
1539	/* FONT_16 */
1540	fontsize = 16;
1541    } else {
1542	/* FONT_14 */
1543	fontsize = 14;
1544    }
1545
1546    if (page < 0 || page >= 8)
1547	return 1;
1548    segment = FONT_BUF + 0x4000*page;
1549    if (page > 3)
1550	segment -= 0xe000;
1551
1552#ifdef VGA_ALT_SEQACCESS
1553    if (adp->va_type == KD_VGA) {	/* what about EGA? XXX */
1554	s = splhigh();
1555	outb(TSIDX, 0x00); outb(TSREG, 0x01);
1556	outb(TSIDX, 0x01); val = inb(TSREG);	/* disable screen */
1557	outb(TSIDX, 0x01); outb(TSREG, val | 0x20);
1558	outb(TSIDX, 0x00); outb(TSREG, 0x03);
1559	splx(s);
1560    }
1561#endif
1562
1563    set_font_mode(adp, buf);
1564    if (fontsize == 32) {
1565	bcopy_fromio(segment + ch*32, data, fontsize*count);
1566    } else {
1567	for (c = ch; count > 0; ++c, --count) {
1568	    bcopy_fromio(segment + c*32, data, fontsize);
1569	    data += fontsize;
1570	}
1571    }
1572    set_normal_mode(adp, buf);
1573
1574#ifdef VGA_ALT_SEQACCESS
1575    if (adp->va_type == KD_VGA) {
1576	s = splhigh();
1577	outb(TSIDX, 0x00); outb(TSREG, 0x01);
1578	outb(TSIDX, 0x01); outb(TSREG, val & 0xdf);	/* enable screen */
1579	outb(TSIDX, 0x00); outb(TSREG, 0x03);
1580	splx(s);
1581    }
1582#endif
1583
1584    return 0;
1585#else /* VGA_NO_FONT_LOADING */
1586    return 1;
1587#endif /* VGA_NO_FONT_LOADING */
1588}
1589
1590/*
1591 * load_font():
1592 * Set the font data in the requested font page.
1593 * NOTE: it appears that some recent video adapters do not support
1594 * the font page other than 0... XXX
1595 *
1596 * EGA/VGA
1597 */
1598static int
1599vga_load_font(video_adapter_t *adp, int page, int fontsize, u_char *data,
1600	      int ch, int count)
1601{
1602#ifndef VGA_NO_FONT_LOADING
1603    u_char buf[PARAM_BUFSIZE];
1604    u_int32_t segment;
1605    int c;
1606#ifdef VGA_ALT_SEQACCESS
1607    int s;
1608    u_char val = 0;
1609#endif
1610
1611    prologue(adp, V_ADP_FONT, 1);
1612
1613    if (fontsize < 14) {
1614	/* FONT_8 */
1615	fontsize = 8;
1616    } else if (fontsize >= 32) {
1617	fontsize = 32;
1618    } else if (fontsize >= 16) {
1619	/* FONT_16 */
1620	fontsize = 16;
1621    } else {
1622	/* FONT_14 */
1623	fontsize = 14;
1624    }
1625
1626    if (page < 0 || page >= 8)
1627	return 1;
1628    segment = FONT_BUF + 0x4000*page;
1629    if (page > 3)
1630	segment -= 0xe000;
1631
1632#ifdef VGA_ALT_SEQACCESS
1633    if (adp->va_type == KD_VGA) {	/* what about EGA? XXX */
1634	s = splhigh();
1635	outb(TSIDX, 0x00); outb(TSREG, 0x01);
1636	outb(TSIDX, 0x01); val = inb(TSREG);	/* disable screen */
1637	outb(TSIDX, 0x01); outb(TSREG, val | 0x20);
1638	outb(TSIDX, 0x00); outb(TSREG, 0x03);
1639	splx(s);
1640    }
1641#endif
1642
1643    set_font_mode(adp, buf);
1644    if (fontsize == 32) {
1645	bcopy_toio(data, segment + ch*32, fontsize*count);
1646    } else {
1647	for (c = ch; count > 0; ++c, --count) {
1648	    bcopy_toio(data, segment + c*32, fontsize);
1649	    data += fontsize;
1650	}
1651    }
1652    set_normal_mode(adp, buf);
1653
1654#ifdef VGA_ALT_SEQACCESS
1655    if (adp->va_type == KD_VGA) {
1656	s = splhigh();
1657	outb(TSIDX, 0x00); outb(TSREG, 0x01);
1658	outb(TSIDX, 0x01); outb(TSREG, val & 0xdf);	/* enable screen */
1659	outb(TSIDX, 0x00); outb(TSREG, 0x03);
1660	splx(s);
1661    }
1662#endif
1663
1664    return 0;
1665#else /* VGA_NO_FONT_LOADING */
1666    return 1;
1667#endif /* VGA_NO_FONT_LOADING */
1668}
1669
1670/*
1671 * show_font():
1672 * Activate the requested font page.
1673 * NOTE: it appears that some recent video adapters do not support
1674 * the font page other than 0... XXX
1675 *
1676 * EGA/VGA
1677 */
1678static int
1679vga_show_font(video_adapter_t *adp, int page)
1680{
1681#ifndef VGA_NO_FONT_LOADING
1682    static u_char cg[] = { 0x00, 0x05, 0x0a, 0x0f, 0x30, 0x35, 0x3a, 0x3f };
1683    int s;
1684
1685    prologue(adp, V_ADP_FONT, 1);
1686    if (page < 0 || page >= 8)
1687	return 1;
1688
1689    s = splhigh();
1690    outb(TSIDX, 0x03); outb(TSREG, cg[page]);
1691    splx(s);
1692
1693    return 0;
1694#else /* VGA_NO_FONT_LOADING */
1695    return 1;
1696#endif /* VGA_NO_FONT_LOADING */
1697}
1698
1699/*
1700 * save_palette():
1701 * Read DAC values. The values have expressed in 8 bits.
1702 *
1703 * VGA
1704 */
1705static int
1706vga_save_palette(video_adapter_t *adp, u_char *palette)
1707{
1708    int i;
1709
1710    prologue(adp, V_ADP_PALETTE, 1);
1711
1712    /*
1713     * We store 8 bit values in the palette buffer, while the standard
1714     * VGA has 6 bit DAC .
1715     */
1716    outb(PALRADR, 0x00);
1717    for (i = 0; i < 256*3; ++i)
1718	palette[i] = inb(PALDATA) << 2;
1719    inb(adp->va_crtc_addr + 6);	/* reset flip/flop */
1720    return 0;
1721}
1722
1723/*
1724 * load_palette():
1725 * Set DAC values.
1726 *
1727 * VGA
1728 */
1729static int
1730vga_load_palette(video_adapter_t *adp, u_char *palette)
1731{
1732    int i;
1733
1734    prologue(adp, V_ADP_PALETTE, 1);
1735
1736    outb(PIXMASK, 0xff);		/* no pixelmask */
1737    outb(PALWADR, 0x00);
1738    for (i = 0; i < 256*3; ++i)
1739	outb(PALDATA, palette[i] >> 2);
1740    inb(adp->va_crtc_addr + 6);	/* reset flip/flop */
1741    outb(ATC, 0x20);			/* enable palette */
1742    return 0;
1743}
1744
1745/*
1746 * set_border():
1747 * Change the border color.
1748 *
1749 * CGA/EGA/VGA
1750 */
1751static int
1752vga_set_border(video_adapter_t *adp, int color)
1753{
1754    prologue(adp, V_ADP_BORDER, 1);
1755
1756    switch (adp->va_type) {
1757    case KD_EGA:
1758    case KD_VGA:
1759	inb(adp->va_crtc_addr + 6);	/* reset flip-flop */
1760	outb(ATC, 0x31); outb(ATC, color & 0xff);
1761	break;
1762    case KD_CGA:
1763	outb(adp->va_crtc_addr + 5, color & 0x0f); /* color select register */
1764	break;
1765    case KD_MONO:
1766    case KD_HERCULES:
1767    default:
1768	break;
1769    }
1770    return 0;
1771}
1772
1773/*
1774 * save_state():
1775 * Read video register values.
1776 * NOTE: this function only reads the standard EGA/VGA registers.
1777 * any extra/extended registers of SVGA adapters are not saved.
1778 *
1779 * VGA
1780 */
1781static int
1782vga_save_state(video_adapter_t *adp, void *p, size_t size)
1783{
1784    video_info_t info;
1785    u_char *buf;
1786    int crtc_addr;
1787    int i, j;
1788    int s;
1789
1790    if (size == 0) {
1791	/* return the required buffer size */
1792	prologue(adp, V_ADP_STATESAVE, 0);
1793	return sizeof(adp_state_t);
1794    } else {
1795	prologue(adp, V_ADP_STATESAVE, 1);
1796	if (size < sizeof(adp_state_t))
1797	    return 1;
1798    }
1799
1800    ((adp_state_t *)p)->sig = V_STATE_SIG;
1801    buf = ((adp_state_t *)p)->regs;
1802    bzero(buf, V_MODE_PARAM_SIZE);
1803    crtc_addr = adp->va_crtc_addr;
1804
1805    s = splhigh();
1806
1807    outb(TSIDX, 0x00); outb(TSREG, 0x01);	/* stop sequencer */
1808    for (i = 0, j = 5; i < 4; i++) {
1809	outb(TSIDX, i + 1);
1810	buf[j++]  =  inb(TSREG);
1811    }
1812    buf[9]  =  inb(MISC + 10);			/* dot-clock */
1813    outb(TSIDX, 0x00); outb(TSREG, 0x03);	/* start sequencer */
1814
1815    for (i = 0, j = 10; i < 25; i++) {		/* crtc */
1816	outb(crtc_addr, i);
1817	buf[j++]  =  inb(crtc_addr + 1);
1818    }
1819    for (i = 0, j = 35; i < 20; i++) {		/* attribute ctrl */
1820        inb(crtc_addr + 6);			/* reset flip-flop */
1821	outb(ATC, i);
1822	buf[j++]  =  inb(ATC + 1);
1823    }
1824    for (i = 0, j = 55; i < 9; i++) {		/* graph data ctrl */
1825	outb(GDCIDX, i);
1826	buf[j++]  =  inb(GDCREG);
1827    }
1828    inb(crtc_addr + 6);				/* reset flip-flop */
1829    outb(ATC, 0x20);				/* enable palette */
1830
1831    splx(s);
1832
1833#if 1
1834    if (vga_get_info(adp, adp->va_mode, &info) == 0) {
1835	if (info.vi_flags & V_INFO_GRAPHICS) {
1836	    buf[0] = info.vi_width/info.vi_cwidth; /* COLS */
1837	    buf[1] = info.vi_height/info.vi_cheight - 1; /* ROWS */
1838	} else {
1839	    buf[0] = info.vi_width;		/* COLS */
1840	    buf[1] = info.vi_height - 1;	/* ROWS */
1841	}
1842	buf[2] = info.vi_cheight;		/* POINTS */
1843    } else {
1844	/* XXX: shouldn't be happening... */
1845	printf("vga%d: %s: failed to obtain mode info. (vga_save_state())\n",
1846	       adp->va_unit, adp->va_name);
1847    }
1848#else
1849    buf[0] = readb(BIOS_PADDRTOVADDR(0x44a));	/* COLS */
1850    buf[1] = readb(BIOS_PADDRTOVADDR(0x484));	/* ROWS */
1851    buf[2] = readb(BIOS_PADDRTOVADDR(0x485));	/* POINTS */
1852    buf[3] = readb(BIOS_PADDRTOVADDR(0x44c));
1853    buf[4] = readb(BIOS_PADDRTOVADDR(0x44d));
1854#endif
1855
1856    return 0;
1857}
1858
1859/*
1860 * load_state():
1861 * Set video registers at once.
1862 * NOTE: this function only updates the standard EGA/VGA registers.
1863 * any extra/extended registers of SVGA adapters are not changed.
1864 *
1865 * EGA/VGA
1866 */
1867static int
1868vga_load_state(video_adapter_t *adp, void *p)
1869{
1870    u_char *buf;
1871    int crtc_addr;
1872    int s;
1873    int i;
1874
1875    prologue(adp, V_ADP_STATELOAD, 1);
1876    if (((adp_state_t *)p)->sig != V_STATE_SIG)
1877	return 1;
1878
1879    buf = ((adp_state_t *)p)->regs;
1880    crtc_addr = adp->va_crtc_addr;
1881
1882    s = splhigh();
1883
1884    outb(TSIDX, 0x00); outb(TSREG, 0x01);	/* stop sequencer */
1885    for (i = 0; i < 4; ++i) {			/* program sequencer */
1886	outb(TSIDX, i + 1);
1887	outb(TSREG, buf[i + 5]);
1888    }
1889    outb(MISC, buf[9]);				/* set dot-clock */
1890    outb(TSIDX, 0x00); outb(TSREG, 0x03);	/* start sequencer */
1891    outb(crtc_addr, 0x11);
1892    outb(crtc_addr + 1, inb(crtc_addr + 1) & 0x7F);
1893    for (i = 0; i < 25; ++i) {			/* program crtc */
1894	outb(crtc_addr, i);
1895	outb(crtc_addr + 1, buf[i + 10]);
1896    }
1897    inb(crtc_addr+6);				/* reset flip-flop */
1898    for (i = 0; i < 20; ++i) {			/* program attribute ctrl */
1899	outb(ATC, i);
1900	outb(ATC, buf[i + 35]);
1901    }
1902    for (i = 0; i < 9; ++i) {			/* program graph data ctrl */
1903	outb(GDCIDX, i);
1904	outb(GDCREG, buf[i + 55]);
1905    }
1906    inb(crtc_addr + 6);				/* reset flip-flop */
1907    outb(ATC, 0x20);				/* enable palette */
1908
1909#if notyet /* a temporary workaround for kernel panic, XXX */
1910#ifndef VGA_NO_BIOS
1911    if (adp->va_unit == V_ADP_PRIMARY) {
1912	writeb(BIOS_PADDRTOVADDR(0x44a), buf[0]);	/* COLS */
1913	writeb(BIOS_PADDRTOVADDR(0x484), buf[1] + rows_offset - 1); /* ROWS */
1914	writeb(BIOS_PADDRTOVADDR(0x485), buf[2]);	/* POINTS */
1915#if 0
1916	writeb(BIOS_PADDRTOVADDR(0x44c), buf[3]);
1917	writeb(BIOS_PADDRTOVADDR(0x44d), buf[4]);
1918#endif
1919    }
1920#endif /* VGA_NO_BIOS */
1921#endif /* notyet */
1922
1923    splx(s);
1924    return 0;
1925}
1926
1927/*
1928 * set_origin():
1929 * Change the origin (window mapping) of the banked frame buffer.
1930 */
1931static int
1932vga_set_origin(video_adapter_t *adp, off_t offset)
1933{
1934    /*
1935     * The standard video modes do not require window mapping;
1936     * always return error.
1937     */
1938    return 1;
1939}
1940
1941/*
1942 * read_hw_cursor():
1943 * Read the position of the hardware text cursor.
1944 *
1945 * all adapters
1946 */
1947static int
1948vga_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
1949{
1950    u_int16_t off;
1951    int s;
1952
1953    if (!init_done)
1954	return 1;
1955
1956    if (adp->va_info.vi_flags & V_INFO_GRAPHICS)
1957	return 1;
1958
1959    s = spltty();
1960    outb(adp->va_crtc_addr, 14);
1961    off = inb(adp->va_crtc_addr + 1);
1962    outb(adp->va_crtc_addr, 15);
1963    off = (off << 8) | inb(adp->va_crtc_addr + 1);
1964    splx(s);
1965
1966    *row = off / adp->va_info.vi_width;
1967    *col = off % adp->va_info.vi_width;
1968
1969    return 0;
1970}
1971
1972/*
1973 * set_hw_cursor():
1974 * Move the hardware text cursor.  If col and row are both -1,
1975 * the cursor won't be shown.
1976 *
1977 * all adapters
1978 */
1979static int
1980vga_set_hw_cursor(video_adapter_t *adp, int col, int row)
1981{
1982    u_int16_t off;
1983    int s;
1984
1985    if (!init_done)
1986	return 1;
1987
1988    if ((col == -1) && (row == -1)) {
1989	off = -1;
1990    } else {
1991	if (adp->va_info.vi_flags & V_INFO_GRAPHICS)
1992	    return 1;
1993	off = row*adp->va_info.vi_width + col;
1994    }
1995
1996    s = spltty();
1997    outb(adp->va_crtc_addr, 14);
1998    outb(adp->va_crtc_addr + 1, off >> 8);
1999    outb(adp->va_crtc_addr, 15);
2000    outb(adp->va_crtc_addr + 1, off & 0x00ff);
2001    splx(s);
2002
2003    return 0;
2004}
2005
2006/*
2007 * set_hw_cursor_shape():
2008 * Change the shape of the hardware text cursor. If the height is
2009 * zero or negative, the cursor won't be shown.
2010 *
2011 * all adapters
2012 */
2013static int
2014vga_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
2015			int celsize, int blink)
2016{
2017    int s;
2018
2019    if (!init_done)
2020	return 1;
2021
2022    s = spltty();
2023    switch (adp->va_type) {
2024    case KD_VGA:
2025    case KD_CGA:
2026    case KD_MONO:
2027    case KD_HERCULES:
2028    default:
2029	if (height <= 0) {
2030	    /* make the cursor invisible */
2031	    outb(adp->va_crtc_addr, 10);
2032	    outb(adp->va_crtc_addr + 1, 32);
2033	    outb(adp->va_crtc_addr, 11);
2034	    outb(adp->va_crtc_addr + 1, 0);
2035	} else {
2036	    outb(adp->va_crtc_addr, 10);
2037	    outb(adp->va_crtc_addr + 1, celsize - base - height);
2038	    outb(adp->va_crtc_addr, 11);
2039	    outb(adp->va_crtc_addr + 1, celsize - base - 1);
2040	}
2041	break;
2042    case KD_EGA:
2043	if (height <= 0) {
2044	    /* make the cursor invisible */
2045	    outb(adp->va_crtc_addr, 10);
2046	    outb(adp->va_crtc_addr + 1, celsize);
2047	    outb(adp->va_crtc_addr, 11);
2048	    outb(adp->va_crtc_addr + 1, 0);
2049	} else {
2050	    outb(adp->va_crtc_addr, 10);
2051	    outb(adp->va_crtc_addr + 1, celsize - base - height);
2052	    outb(adp->va_crtc_addr, 11);
2053	    outb(adp->va_crtc_addr + 1, celsize - base);
2054	}
2055	break;
2056    }
2057    splx(s);
2058
2059    return 0;
2060}
2061
2062/*
2063 * mmap():
2064 * Mmap frame buffer.
2065 *
2066 * all adapters
2067 */
2068static int
2069vga_mmap(video_adapter_t *adp, vm_offset_t offset)
2070{
2071    if (offset > 0x20000 - PAGE_SIZE)
2072	return -1;
2073#ifdef __i386__
2074    return i386_btop((VIDEO_BUF_BASE + offset));
2075#endif
2076#ifdef __alpha__
2077    return alpha_btop((VIDEO_BUF_BASE + offset));
2078#endif
2079}
2080
2081static void
2082dump_buffer(u_char *buf, size_t len)
2083{
2084    int i;
2085
2086    for(i = 0; i < len;) {
2087	printf("%02x ", buf[i]);
2088	if ((++i % 16) == 0)
2089	    printf("\n");
2090    }
2091}
2092
2093/*
2094 * diag():
2095 * Print some information about the video adapter and video modes,
2096 * with requested level of details.
2097 *
2098 * all adapters
2099 */
2100static int
2101vga_diag(video_adapter_t *adp, int level)
2102{
2103#if FB_DEBUG > 1
2104    video_info_t info;
2105#endif
2106    u_char *mp;
2107
2108    if (!init_done)
2109	return 1;
2110
2111#if FB_DEBUG > 1
2112#ifndef VGA_NO_BIOS
2113    printf("vga: RTC equip. code:0x%02x, DCC code:0x%02x\n",
2114	   rtcin(RTC_EQUIPMENT), readb(BIOS_PADDRTOVADDR(0x488)));
2115    printf("vga: CRTC:0x%x, video option:0x%02x, ",
2116	   readw(BIOS_PADDRTOVADDR(0x463)),
2117	   readb(BIOS_PADDRTOVADDR(0x487)));
2118    printf("rows:%d, cols:%d, font height:%d\n",
2119	   readb(BIOS_PADDRTOVADDR(0x44a)),
2120	   readb(BIOS_PADDRTOVADDR(0x484)) + 1,
2121	   readb(BIOS_PADDRTOVADDR(0x485)));
2122#endif /* VGA_NO_BIOS */
2123#if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
2124    printf("vga: param table EGA/VGA:%p", video_mode_ptr);
2125    printf(", CGA/MDA:%p\n", video_mode_ptr2);
2126#endif
2127    printf("vga: rows_offset:%d\n", rows_offset);
2128#endif /* FB_DEBUG > 1 */
2129
2130    fb_dump_adp_info(DRIVER_NAME, adp, level);
2131
2132#if FB_DEBUG > 1
2133    if (adp->va_flags & V_ADP_MODECHANGE) {
2134	for (i = 0; bios_vmode[i].vi_mode != EOT; ++i) {
2135	    if (bios_vmode[i].vi_mode == NA)
2136		continue;
2137	    if (get_mode_param(bios_vmode[i].vi_mode) == NULL)
2138		continue;
2139	    fb_dump_mode_info(DRIVER_NAME, adp, &bios_vmode[i], level);
2140	}
2141    } else {
2142	vga_get_info(adp, adp->va_initial_mode, &info);	/* shouldn't fail */
2143	fb_dump_mode_info(DRIVER_NAME, adp, &info, level);
2144    }
2145#endif /* FB_DEBUG > 1 */
2146
2147    if ((adp->va_type != KD_EGA) && (adp->va_type != KD_VGA))
2148	return 0;
2149#if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
2150    if (video_mode_ptr == NULL)
2151	printf("vga%d: %s: WARNING: video mode switching is not "
2152	       "fully supported on this adapter\n",
2153	       adp->va_unit, adp->va_name);
2154#endif
2155    if (level <= 0)
2156	return 0;
2157
2158    if (adp->va_type == KD_VGA) {
2159	printf("VGA parameters upon power-up\n");
2160	dump_buffer(adpstate.regs, sizeof(adpstate.regs));
2161	printf("VGA parameters in BIOS for mode %d\n", adp->va_initial_mode);
2162	dump_buffer(adpstate2.regs, sizeof(adpstate2.regs));
2163    }
2164
2165    mp = get_mode_param(adp->va_initial_mode);
2166    if (mp == NULL)	/* this shouldn't be happening */
2167	return 0;
2168    printf("EGA/VGA parameters to be used for mode %d\n", adp->va_initial_mode);
2169    dump_buffer(mp, V_MODE_PARAM_SIZE);
2170
2171    return 0;
2172}
2173
2174#endif /* NVGA > 0 */
2175