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