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