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