xref: /netbsd-src/sys/dev/ic/vga_raster.c (revision e6c7e151de239c49d2e38720a061ed9d1fa99309)
1 /*	$NetBSD: vga_raster.c,v 1.46 2019/12/01 14:18:51 ad Exp $	*/
2 
3 /*
4  * Copyright (c) 2001, 2002 Bang Jun-Young
5  * Copyright (c) 2004 Julio M. Merino Vidal
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /*
32  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
33  * All rights reserved.
34  *
35  * Author: Chris G. Demetriou
36  *
37  * Permission to use, copy, modify and distribute this software and
38  * its documentation is hereby granted, provided that both the copyright
39  * notice and this permission notice appear in all copies of the
40  * software, derivative works or modified versions, and any portions
41  * thereof, and that both notices appear in supporting documentation.
42  *
43  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
44  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
45  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46  *
47  * Carnegie Mellon requests users of this software to return to
48  *
49  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
50  *  School of Computer Science
51  *  Carnegie Mellon University
52  *  Pittsburgh PA 15213-3890
53  *
54  * any improvements or extensions that they make and grant Carnegie the
55  * rights to redistribute these changes.
56  */
57 
58 #include <sys/cdefs.h>
59 __KERNEL_RCSID(0, "$NetBSD: vga_raster.c,v 1.46 2019/12/01 14:18:51 ad Exp $");
60 
61 #include "opt_vga.h"
62 #include "opt_wsmsgattrs.h" /* for WSDISPLAY_CUSTOM_OUTPUT */
63 
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/callout.h>
67 #include <sys/kernel.h>
68 #include <sys/device.h>
69 #include <sys/malloc.h>
70 #include <sys/queue.h>
71 #include <sys/bus.h>
72 
73 #include <dev/ic/mc6845reg.h>
74 #include <dev/ic/pcdisplayvar.h>
75 #include <dev/ic/vgareg.h>
76 #include <dev/ic/vgavar.h>
77 #include <dev/videomode/videomode.h>
78 
79 #include <dev/wscons/wsdisplayvar.h>
80 #include <dev/wscons/wsconsio.h>
81 #include <dev/wsfont/wsfont.h>
82 
83 #include <dev/ic/pcdisplay.h>
84 
85 int vga_no_builtinfont = 0;
86 
87 u_int8_t builtinfont_data[256 * 16];
88 
89 struct wsdisplay_font builtinfont = {
90 	"builtin",			/* typeface name */
91 	0,				/* firstchar */
92 	256,				/* numchars */
93 	WSDISPLAY_FONTENC_IBM,		/* encoding */
94 	8,				/* width */
95 	16,				/* height */
96 	1,				/* stride */
97 	WSDISPLAY_FONTORDER_L2R,	/* bit order */
98 	WSDISPLAY_FONTORDER_L2R,	/* byte order */
99 	builtinfont_data		/* data */
100 };
101 
102 struct vga_scrmem {
103 	u_int16_t ch;
104 	u_int8_t attr;
105 	u_int8_t second;	/* XXXBJY should be u_int8_t len; */
106 	u_int8_t enc;
107 };
108 
109 #ifdef VGA_CONSOLE_SCREENTYPE
110 #define VGA_SCRMEM_SIZE		(80 * 30)
111 #else
112 #define VGA_SCRMEM_SIZE		(80 * 25)
113 #endif
114 
115 struct vga_scrmem boot_scrmem[VGA_SCRMEM_SIZE];
116 
117 struct vga_raster_font {
118 	LIST_ENTRY(vga_raster_font) next;
119 	struct wsdisplay_font *font;
120 };
121 
122 struct vgascreen {
123 	LIST_ENTRY(vgascreen) next;
124 	struct vga_config *cfg;
125 	struct vga_handle *hdl;
126 	const struct wsscreen_descr *type;
127 
128 	int active;
129 	struct vga_scrmem *mem;
130 	int encoding;
131 
132 	int dispoffset;
133 	int mindispoffset;
134 	int maxdispoffset;
135 
136 	int cursoron;			/* Is cursor displayed? */
137 	int cursorcol;			/* Current cursor column */
138 	int cursorrow;			/* Current cursor row */
139 	struct vga_scrmem cursortmp;
140 	int cursorstride;
141 
142 	LIST_HEAD(, vga_raster_font) fontset;
143 };
144 
145 struct vga_moderegs {
146 	u_int8_t miscout;		/* Misc. output */
147 	u_int8_t crtc[MC6845_NREGS];	/* CRTC controller */
148 	u_int8_t atc[VGA_ATC_NREGS];	/* Attribute controller */
149 	u_int8_t ts[VGA_TS_NREGS];	/* Time sequencer */
150 	u_int8_t gdc[VGA_GDC_NREGS];	/* Graphics display controller */
151 };
152 
153 static int vgaconsole, vga_console_type, vga_console_attached;
154 static struct vgascreen vga_console_screen;
155 static struct vga_config vga_console_vc;
156 static struct vga_raster_font vga_console_fontset_ascii;
157 static struct videomode vga_console_modes[2] = {
158 	/* 640x400 for 80x25, 80x40 and 80x50 modes */
159 	{
160 		25175, 640, 664, 760, 800, 400, 409, 411, 450, 0, NULL,
161 	},
162 	/* 640x480 for 80x30 mode */
163 	{
164 		25175, 640, 664, 760, 800, 480, 491, 493, 525, 0, NULL,
165 	}
166 };
167 
168 static void vga_raster_init(struct vga_config *, bus_space_tag_t,
169 		bus_space_tag_t);
170 static void vga_raster_init_screen(struct vga_config *, struct vgascreen *,
171 		const struct wsscreen_descr *, int, long *);
172 static void vga_raster_setup_font(struct vga_config *, struct vgascreen *);
173 static void vga_setup_regs(struct videomode *, struct vga_moderegs *);
174 static void vga_set_mode(struct vga_handle *, struct vga_moderegs *);
175 static void vga_restore_screen(struct vgascreen *,
176 		const struct wsscreen_descr *, struct vga_scrmem *);
177 static void vga_raster_cursor_init(struct vgascreen *, int);
178 static void _vga_raster_putchar(void *, int, int, u_int, long,
179 		struct vga_raster_font *);
180 
181 static void vga_raster_cursor(void *, int, int, int);
182 static int  vga_raster_mapchar(void *, int, u_int *);
183 static void vga_raster_putchar(void *, int, int, u_int, long);
184 static void vga_raster_copycols(void *, int, int, int, int);
185 static void vga_raster_erasecols(void *, int, int, int, long);
186 static void vga_raster_copyrows(void *, int, int, int);
187 static void vga_raster_eraserows(void *, int, int, long);
188 static int  vga_raster_allocattr(void *, int, int, int, long *);
189 #ifdef WSDISPLAY_CUSTOM_OUTPUT
190 static void vga_raster_replaceattr(void *, long, long);
191 #endif /* WSDISPLAY_CUSTOM_OUTPUT */
192 
193 const struct wsdisplay_emulops vga_raster_emulops = {
194 	vga_raster_cursor,
195 	vga_raster_mapchar,
196 	vga_raster_putchar,
197 	vga_raster_copycols,
198 	vga_raster_erasecols,
199 	vga_raster_copyrows,
200 	vga_raster_eraserows,
201 	vga_raster_allocattr,
202 #ifdef WSDISPLAY_CUSTOM_OUTPUT
203 	vga_raster_replaceattr,
204 #else /* WSDISPLAY_CUSTOM_OUTPUT */
205 	NULL,
206 #endif /* WSDISPLAY_CUSTOM_OUTPUT */
207 };
208 
209 /*
210  * translate WS(=ANSI) color codes to standard pc ones
211  */
212 static const unsigned char fgansitopc[] = {
213 #ifdef __alpha__
214 	/*
215 	 * XXX DEC HAS SWITCHED THE CODES FOR BLUE AND RED!!!
216 	 * XXX We should probably not bother with this
217 	 * XXX (reinitialize the palette registers).
218 	 */
219 	FG_BLACK, FG_BLUE, FG_GREEN, FG_CYAN, FG_RED,
220 	FG_MAGENTA, FG_BROWN, FG_LIGHTGREY
221 #else
222 	FG_BLACK, FG_RED, FG_GREEN, FG_BROWN, FG_BLUE,
223 	FG_MAGENTA, FG_CYAN, FG_LIGHTGREY
224 #endif
225 }, bgansitopc[] = {
226 #ifdef __alpha__
227 	BG_BLACK, BG_BLUE, BG_GREEN, BG_CYAN, BG_RED,
228 	BG_MAGENTA, BG_BROWN, BG_LIGHTGREY
229 #else
230 	BG_BLACK, BG_RED, BG_GREEN, BG_BROWN, BG_BLUE,
231 	BG_MAGENTA, BG_CYAN, BG_LIGHTGREY
232 #endif
233 };
234 
235 const struct wsscreen_descr vga_25lscreen = {
236 	"80x25", 80, 25,
237 	&vga_raster_emulops,
238 	8, 16,
239 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK,
240 	&vga_console_modes[0]
241 }, vga_25lscreen_mono = {
242 	"80x25", 80, 25,
243 	&vga_raster_emulops,
244 	8, 16,
245 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE,
246 	&vga_console_modes[0]
247 }, vga_30lscreen = {
248 	"80x30", 80, 30,
249 	&vga_raster_emulops,
250 	8, 16,
251 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK,
252 	&vga_console_modes[1]
253 }, vga_30lscreen_mono = {
254 	"80x30", 80, 30,
255 	&vga_raster_emulops,
256 	8, 16,
257 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE,
258 	&vga_console_modes[1]
259 }, vga_40lscreen = {
260 	"80x40", 80, 40,
261 	&vga_raster_emulops,
262 	8, 10,
263 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK,
264 	&vga_console_modes[0]
265 }, vga_40lscreen_mono = {
266 	"80x40", 80, 40,
267 	&vga_raster_emulops,
268 	8, 10,
269 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE,
270 	&vga_console_modes[0]
271 }, vga_50lscreen = {
272 	"80x50", 80, 50,
273 	&vga_raster_emulops,
274 	8, 8,
275 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK,
276 	&vga_console_modes[0]
277 }, vga_50lscreen_mono = {
278 	"80x50", 80, 50,
279 	&vga_raster_emulops,
280 	8, 8,
281 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE,
282 	&vga_console_modes[0]
283 };
284 
285 const struct wsscreen_descr *_vga_scrlist[] = {
286 	&vga_25lscreen,
287 	&vga_30lscreen,
288 	&vga_40lscreen,
289 	&vga_50lscreen,
290 }, *_vga_scrlist_mono[] = {
291 	&vga_25lscreen_mono,
292 	&vga_30lscreen_mono,
293 	&vga_40lscreen_mono,
294 	&vga_50lscreen_mono,
295 };
296 
297 const struct wsscreen_list vga_screenlist = {
298 	sizeof(_vga_scrlist) / sizeof(struct wsscreen_descr *),
299 	_vga_scrlist
300 }, vga_screenlist_mono = {
301 	sizeof(_vga_scrlist_mono) / sizeof(struct wsscreen_descr *),
302 	_vga_scrlist_mono
303 };
304 
305 static int	vga_raster_ioctl(void *, void *, u_long, void *, int,
306 		    struct lwp *);
307 static paddr_t	vga_raster_mmap(void *, void *, off_t, int);
308 static int	vga_raster_alloc_screen(void *, const struct wsscreen_descr *,
309 		    void **, int *, int *, long *);
310 static void	vga_raster_free_screen(void *, void *);
311 static int	vga_raster_show_screen(void *, void *, int,
312 		    void (*)(void *, int, int), void *);
313 static int	vga_raster_load_font(void *, void *, struct wsdisplay_font *);
314 
315 static void 	vga_switch_screen(struct vga_config *);
316 static void 	vga_raster_setscreentype(struct vga_config *,
317 		    const struct wsscreen_descr *);
318 
319 const struct wsdisplay_accessops vga_raster_accessops = {
320 	vga_raster_ioctl,
321 	vga_raster_mmap,
322 	vga_raster_alloc_screen,
323 	vga_raster_free_screen,
324 	vga_raster_show_screen,
325 	vga_raster_load_font,
326 	NULL,	/* pollc */
327 	NULL,	/* scroll */
328 };
329 
330 int
331 vga_cnattach(bus_space_tag_t iot, bus_space_tag_t memt, int type, int check)
332 {
333 	long defattr;
334 	const struct wsscreen_descr *scr;
335 #ifdef VGA_CONSOLE_SCREENTYPE
336 	const char *typestr = NULL;
337 #endif
338 
339 	if (check && !vga_common_probe(iot, memt))
340 		return (ENXIO);
341 
342 	/* set up bus-independent VGA configuration */
343 	vga_raster_init(&vga_console_vc, iot, memt);
344 #ifdef VGA_CONSOLE_SCREENTYPE
345 	scr = wsdisplay_screentype_pick(vga_console_vc.hdl.vh_mono ?
346 	    &vga_screenlist_mono : &vga_screenlist, VGA_CONSOLE_SCREENTYPE);
347 	if (!scr)
348 		/* Invalid screen type, continue with the default mode. */
349 		typestr = "80x25";
350 	else if (scr->nrows > 30)
351 		/* Unsupported screen type, try 80x30. */
352 		typestr = "80x30";
353 	if (typestr)
354 		scr = wsdisplay_screentype_pick(vga_console_vc.hdl.vh_mono ?
355 		    &vga_screenlist_mono : &vga_screenlist, typestr);
356 	if (scr != vga_console_vc.currenttype)
357 		vga_console_vc.currenttype = scr;
358 #else
359 	scr = vga_console_vc.currenttype;
360 #endif
361 	vga_raster_init_screen(&vga_console_vc, &vga_console_screen, scr, 1,
362 	    &defattr);
363 
364 	vga_console_screen.active = 1;
365 	vga_console_vc.active = &vga_console_screen;
366 
367 	wsdisplay_cnattach(scr, &vga_console_screen,
368 	    vga_console_screen.cursorcol, vga_console_screen.cursorrow,
369 	    defattr);
370 
371 	vgaconsole = 1;
372 	vga_console_type = type;
373 	return (0);
374 }
375 
376 static void
377 vga_raster_init(struct vga_config *vc, bus_space_tag_t iot,
378     bus_space_tag_t memt)
379 {
380 	struct vga_handle *vh = &vc->hdl;
381 	u_int8_t mor;
382 	struct vga_raster_font *vf;
383 
384 	vh->vh_iot = iot;
385 	vh->vh_memt = memt;
386 
387 	if (bus_space_map(vh->vh_iot, 0x3c0, 0x10, 0, &vh->vh_ioh_vga))
388 		panic("vga_raster_init: couldn't map vga io");
389 
390 	/* read "misc output register" */
391 	mor = bus_space_read_1(vh->vh_iot, vh->vh_ioh_vga, VGA_MISC_DATAR);
392 	vh->vh_mono = !(mor & 1);
393 
394 	if (bus_space_map(vh->vh_iot, (vh->vh_mono ? 0x3b0 : 0x3d0), 0x10, 0,
395 	    &vh->vh_ioh_6845))
396 		panic("vga_raster_init: couldn't map 6845 io");
397 
398 	if (bus_space_map(vh->vh_memt, 0xa0000, 0x20000,
399 	    BUS_SPACE_MAP_CACHEABLE | BUS_SPACE_MAP_PREFETCHABLE,
400 	    &vh->vh_allmemh))
401 		panic("vga_init: couldn't map memory");
402 
403 	if (bus_space_subregion(vh->vh_memt, vh->vh_allmemh, 0, 0x10000,
404 	    &vh->vh_memh))
405 		panic("vga_raster_init: mem subrange failed");
406 
407 	vc->nscreens = 0;
408 	LIST_INIT(&vc->screens);
409 	vc->active = NULL;
410 	vc->currenttype = vh->vh_mono ? &vga_25lscreen_mono : &vga_25lscreen;
411 	callout_init(&vc->vc_switch_callout, 0);
412 
413 	wsfont_init();
414 	vc->nfonts = 1;
415 	LIST_INIT(&vc->vc_fontlist);
416 	vf = &vga_console_fontset_ascii;
417 	if (vga_no_builtinfont) {
418 		struct wsdisplay_font *wf;
419 		int cookie;
420 
421 		/* prefer 8x16 pixel font */
422 		cookie = wsfont_find(NULL, 8, 16, 0, WSDISPLAY_FONTORDER_L2R,
423 		    0, WSFONT_FIND_BITMAP);
424 		if (cookie == -1)
425 			cookie = wsfont_find(NULL, 0, 0, 0,
426 			    WSDISPLAY_FONTORDER_L2R, WSDISPLAY_FONTORDER_L2R,
427 			    WSFONT_FIND_BITMAP);
428 		if (cookie == -1 || wsfont_lock(cookie, &wf))
429 			panic("vga_raster_init: can't load console font");
430 		vf->font = wf;
431 	} else {
432 		vga_load_builtinfont(vh, builtinfont_data, 0, 256);
433 		vf->font = &builtinfont;
434 	}
435 	LIST_INSERT_HEAD(&vc->vc_fontlist, vf, next);
436 }
437 
438 static void
439 vga_raster_init_screen(struct vga_config *vc, struct vgascreen *scr,
440     const struct wsscreen_descr *type, int existing, long *attrp)
441 {
442 	int cpos;
443 	int res;
444 	struct vga_handle *vh;
445 
446 	scr->cfg = vc;
447 	scr->hdl = &vc->hdl;
448 	scr->type = type;
449 	scr->mindispoffset = 0;
450 	scr->maxdispoffset = scr->dispoffset +
451 	    type->nrows * type->ncols * type->fontheight;
452 	vh = &vc->hdl;
453 
454 	LIST_INIT(&scr->fontset);
455 	vga_raster_setup_font(vc, scr);
456 
457 	if (existing) {
458 		int i;
459 
460 		cpos = vga_6845_read(vh, cursorh) << 8;
461 		cpos |= vga_6845_read(vh, cursorl);
462 
463 		/* make sure we have a valid cursor position */
464 		if (cpos < 0 || cpos >= type->nrows * type->ncols)
465 			cpos = 0;
466 
467 		scr->dispoffset = vga_6845_read(vh, startadrh) << 9;
468 		scr->dispoffset |= vga_6845_read(vh, startadrl) << 1;
469 
470 		/* make sure we have a valid memory offset */
471 		if (scr->dispoffset < scr->mindispoffset ||
472 		    scr->dispoffset > scr->maxdispoffset)
473 			scr->dispoffset = scr->mindispoffset;
474 
475 		scr->mem = boot_scrmem;
476 		scr->active = 1;
477 
478 		/* Save the current screen to memory. XXXBJY assume 80x25 */
479 		for (i = 0; i < 80 * 25; i++) {
480 			scr->mem[i].ch = bus_space_read_1(vh->vh_memt,
481 			    vh->vh_allmemh, 0x18000 + i * 2);
482 			scr->mem[i].attr = bus_space_read_1(vh->vh_memt,
483 			    vh->vh_allmemh, 0x18000 + i * 2 + 1);
484 			scr->mem[i].enc = scr->encoding;
485 		}
486 
487 		vga_raster_setscreentype(vc, type);
488 
489 		/* Clear the entire screen. */
490 		vga_gdc_write(vh, mode, 0x02);
491 		bus_space_set_region_4(vh->vh_memt, vh->vh_allmemh, 0, 0,
492 		    0x4000);
493 
494 		vga_restore_screen(scr, type, scr->mem);
495 	} else {
496 		cpos = 0;
497 		scr->dispoffset = scr->mindispoffset;
498 		scr->mem = NULL;
499 		scr->active = 0;
500 	}
501 
502 	scr->cursorrow = cpos / type->ncols;
503 	scr->cursorcol = cpos % type->ncols;
504 	vga_raster_cursor_init(scr, existing);
505 
506 #ifdef __alpha__
507 	if (!vc->hdl.vh_mono)
508 		/*
509 		 * DEC firmware uses a blue background.
510 		 */
511 		res = vga_raster_allocattr(scr, WSCOL_WHITE, WSCOL_BLUE,
512 		    WSATTR_WSCOLORS, attrp);
513 	else
514 #endif
515 	res = vga_raster_allocattr(scr, 0, 0, 0, attrp);
516 #ifdef DIAGNOSTIC
517 	if (res)
518 		panic("vga_raster_init_screen: attribute botch");
519 #endif
520 
521 	vc->nscreens++;
522 	LIST_INSERT_HEAD(&vc->screens, scr, next);
523 }
524 
525 void
526 vga_common_attach(struct vga_softc *sc, bus_space_tag_t iot,
527     bus_space_tag_t memt, int type, int quirks,
528     const struct vga_funcs *vf)
529 {
530 	int console;
531 	struct vga_config *vc;
532 	struct wsemuldisplaydev_attach_args aa;
533 
534 	console = vga_is_console(iot, type);
535 
536 	if (console) {
537 		vc = &vga_console_vc;
538 		vga_console_attached = 1;
539 	} else {
540 		vc = malloc(sizeof(struct vga_config), M_DEVBUF, M_WAITOK);
541 		vga_raster_init(vc, iot, memt);
542 	}
543 
544 	vc->vc_type = type;
545 	vc->vc_funcs = vf;
546 
547 	sc->sc_vc = vc;
548 	vc->softc = sc;
549 
550 	aa.console = console;
551 	aa.scrdata = (vc->hdl.vh_mono ? &vga_screenlist_mono : &vga_screenlist);
552 	aa.accessops = &vga_raster_accessops;
553 	aa.accesscookie = vc;
554 
555 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
556 }
557 
558 int
559 vga_cndetach(void)
560 {
561 	struct vga_config *vc;
562 	struct vga_handle *vh;
563 
564 	vc = &vga_console_vc;
565 	vh = &vc->hdl;
566 
567 	if (vgaconsole) {
568 		wsdisplay_cndetach();
569 
570 		bus_space_unmap(vh->vh_iot, vh->vh_ioh_vga, 0x10);
571 		bus_space_unmap(vh->vh_iot, vh->vh_ioh_6845, 0x10);
572 		bus_space_unmap(vh->vh_memt, vh->vh_allmemh, 0x20000);
573 
574 		vga_console_attached = 0;
575 		vgaconsole = 0;
576 
577 		return 1;
578 	}
579 
580 	return 0;
581 }
582 
583 int
584 vga_is_console(bus_space_tag_t iot, int type)
585 {
586 	if (vgaconsole &&
587 	    !vga_console_attached &&
588 	    iot == vga_console_vc.hdl.vh_iot &&
589 	    (vga_console_type == -1 || (type == vga_console_type)))
590 		return (1);
591 	return (0);
592 }
593 
594 static int
595 vga_get_video(struct vga_config *vc)
596 {
597 
598 	return (vga_ts_read(&vc->hdl, mode) & VGA_TS_MODE_BLANK) == 0;
599 }
600 
601 static void
602 vga_set_video(struct vga_config *vc, int state)
603 {
604 	int val;
605 
606 	vga_ts_write(&vc->hdl, syncreset, 0x01);
607 	if (state) {					/* unblank screen */
608 		val = vga_ts_read(&vc->hdl, mode);
609 		vga_ts_write(&vc->hdl, mode, val & ~VGA_TS_MODE_BLANK);
610 #ifndef VGA_NO_VBLANK
611 		val = vga_6845_read(&vc->hdl, mode);
612 		vga_6845_write(&vc->hdl, mode, val | 0x80);
613 #endif
614 	} else {					/* blank screen */
615 		val = vga_ts_read(&vc->hdl, mode);
616 		vga_ts_write(&vc->hdl, mode, val | VGA_TS_MODE_BLANK);
617 #ifndef VGA_NO_VBLANK
618 		val = vga_6845_read(&vc->hdl, mode);
619 		vga_6845_write(&vc->hdl, mode, val & ~0x80);
620 #endif
621 	}
622 	vga_ts_write(&vc->hdl, syncreset, 0x03);
623 }
624 
625 int
626 vga_raster_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
627 	struct lwp *l)
628 {
629 	struct vga_config *vc = v;
630 	const struct vga_funcs *vf = vc->vc_funcs;
631 
632 	switch (cmd) {
633 	case WSDISPLAYIO_GTYPE:
634 		*(int *)data = vc->vc_type;
635 		return 0;
636 
637 	case WSDISPLAYIO_GINFO: {
638 		struct wsdisplay_fbinfo *fbi = data;
639 		const struct wsscreen_descr *wd = vc->currenttype;
640 		const struct videomode *vm = wd->modecookie;
641 		fbi->width = vm->hdisplay;
642 		fbi->height = vm->vdisplay;
643 		fbi->depth = 24;	/* xxx: ? */
644 		fbi->cmsize = 256;	/* xxx: from palette */
645 		return 0;
646 	}
647 
648 	case WSDISPLAYIO_GVIDEO:
649 		*(int *)data = (vga_get_video(vc) ?
650 		    WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF);
651 		return 0;
652 
653 	case WSDISPLAYIO_SVIDEO:
654 		vga_set_video(vc, *(int *)data == WSDISPLAYIO_VIDEO_ON);
655 		return 0;
656 
657 	case WSDISPLAYIO_GETCMAP:
658 	case WSDISPLAYIO_PUTCMAP:
659 	case WSDISPLAYIO_GCURPOS:
660 	case WSDISPLAYIO_SCURPOS:
661 	case WSDISPLAYIO_GCURMAX:
662 	case WSDISPLAYIO_GCURSOR:
663 	case WSDISPLAYIO_SCURSOR:
664 #ifdef DIAGNOSTIC
665 		printf("%s: 0x%lx unsupported\n", __func__, cmd);
666 #endif
667 		/* NONE of these operations are by the generic VGA driver. */
668 		return EPASSTHROUGH;
669 	}
670 
671 	if (vc->vc_funcs == NULL) {
672 #ifdef DIAGNOSTIC
673 		printf("%s: no vc_funcs\n", __func__);
674 #endif
675 		return EPASSTHROUGH;
676 	}
677 
678 	if (vf->vf_ioctl == NULL) {
679 #ifdef DIAGNOSTIC
680 		printf("%s: no vf_ioctl\n", __func__);
681 #endif
682 		return EPASSTHROUGH;
683 	}
684 
685 	return ((*vf->vf_ioctl)(v, cmd, data, flag, l));
686 }
687 
688 static paddr_t
689 vga_raster_mmap(void *v, void *vs, off_t offset, int prot)
690 {
691 	struct vga_config *vc = v;
692 	const struct vga_funcs *vf = vc->vc_funcs;
693 
694 	if (vc->vc_funcs == NULL)
695 		return (-1);
696 
697 	if (vf->vf_mmap == NULL)
698 		return (-1);
699 
700 	return ((*vf->vf_mmap)(v, offset, prot));
701 }
702 
703 static int
704 vga_raster_alloc_screen(void *v, const struct wsscreen_descr *type,
705     void **cookiep, int *curxp, int *curyp, long *defattrp)
706 {
707 	struct vga_config *vc = v;
708 	struct vgascreen *scr;
709 
710 	if (vc->nscreens == 1) {
711 		vc->screens.lh_first->mem = boot_scrmem;
712 	}
713 
714 	scr = malloc(sizeof(struct vgascreen), M_DEVBUF, M_WAITOK);
715 	vga_raster_init_screen(vc, scr, type, vc->nscreens == 0, defattrp);
716 
717 	if (vc->nscreens == 1) {
718 		scr->active = 1;
719 		vc->active = scr;
720 		vc->currenttype = type;
721 	} else {
722 		scr->mem = malloc(sizeof(struct vga_scrmem) *
723 		    type->ncols * type->nrows, M_DEVBUF, M_WAITOK);
724 		vga_raster_eraserows(scr, 0, type->nrows, *defattrp);
725 	}
726 
727 	*cookiep = scr;
728 	*curxp = scr->cursorcol;
729 	*curyp = scr->cursorrow;
730 
731 	return (0);
732 }
733 
734 static void
735 vga_raster_free_screen(void *v, void *cookie)
736 {
737 	struct vgascreen *vs = cookie;
738 	struct vga_config *vc = vs->cfg;
739 
740 	LIST_REMOVE(vs, next);
741 	vc->nscreens--;
742 	if (vs != &vga_console_screen)
743 		free(vs, M_DEVBUF);
744 	else
745 		panic("vga_raster_free_screen: console");
746 
747 	if (vc->active == vs)
748 		vc->active = 0;
749 }
750 
751 static int
752 vga_raster_show_screen(void *v, void *cookie, int waitok,
753     void (*cb)(void *, int, int), void *cbarg)
754 {
755 	struct vgascreen *scr = cookie, *oldscr;
756 	struct vga_config *vc = scr->cfg;
757 
758 	oldscr = vc->active; /* can be NULL! */
759 	if (scr == oldscr) {
760 		return (0);
761 	}
762 
763 	vc->wantedscreen = cookie;
764 	vc->switchcb = cb;
765 	vc->switchcbarg = cbarg;
766 	if (cb) {
767 		callout_reset(&vc->vc_switch_callout, 0,
768 		    (void(*)(void *))vga_switch_screen, vc);
769 		return (EAGAIN);
770 	}
771 
772 	vga_switch_screen(vc);
773 	return (0);
774 }
775 
776 static void
777 vga_switch_screen(struct vga_config *vc)
778 {
779 	struct vgascreen *scr, *oldscr;
780 	struct vga_handle *vh = &vc->hdl;
781 	const struct wsscreen_descr *type;
782 
783 	scr = vc->wantedscreen;
784 	if (!scr) {
785 		printf("vga_switch_screen: disappeared\n");
786 		(*vc->switchcb)(vc->switchcbarg, EIO, 0);
787 		return;
788 	}
789 	type = scr->type;
790 	oldscr = vc->active; /* can be NULL! */
791 #ifdef DIAGNOSTIC
792 	if (oldscr) {
793 		if (!oldscr->active)
794 			panic("vga_raster_show_screen: not active");
795 		if (oldscr->type != vc->currenttype)
796 			panic("vga_raster_show_screen: bad type");
797 	}
798 #endif
799 	if (scr == oldscr) {
800 		return;
801 	}
802 #ifdef DIAGNOSTIC
803 	if (scr->active)
804 		panic("vga_raster_show_screen: active");
805 #endif
806 
807 	if (oldscr)
808 		oldscr->active = 0;
809 
810 	if (vc->currenttype != type) {
811 		vga_raster_setscreentype(vc, type);
812 		vc->currenttype = type;
813 	}
814 
815 	scr->dispoffset = scr->mindispoffset;
816 
817 	if (!oldscr || (scr->dispoffset != oldscr->dispoffset)) {
818 		vga_6845_write(vh, startadrh, scr->dispoffset >> 8);
819 		vga_6845_write(vh, startadrl, scr->dispoffset);
820 	}
821 
822 	/* Clear the entire screen. */
823 	vga_gdc_write(vh, mode, 0x02);
824 	bus_space_set_region_4(vh->vh_memt, vh->vh_allmemh, 0, 0, 0x2000);
825 
826 	scr->active = 1;
827 	vga_restore_screen(scr, type, scr->mem);
828 
829 	vc->active = scr;
830 
831 	vga_raster_cursor(scr, scr->cursoron, scr->cursorrow, scr->cursorcol);
832 
833 	vc->wantedscreen = 0;
834 	if (vc->switchcb)
835 		(*vc->switchcb)(vc->switchcbarg, 0, 0);
836 }
837 
838 static int
839 vga_raster_load_font(void *v, void *id,
840     struct wsdisplay_font *data)
841 {
842 	/* XXX */
843 	printf("vga_raster_load_font: called\n");
844 
845 	return (0);
846 }
847 
848 static void
849 vga_raster_setup_font(struct vga_config *vc, struct vgascreen *scr)
850 {
851 	struct vga_raster_font *vf;
852 	struct wsdisplay_font *wf;
853 	int cookie;
854 
855 	LIST_FOREACH(vf, &vc->vc_fontlist, next) {
856 		if (wsfont_matches(vf->font, 0, 0, scr->type->fontheight, 0,
857 		    WSFONT_FIND_BITMAP)) {
858 			scr->encoding = vf->font->encoding;
859 			LIST_INSERT_HEAD(&scr->fontset, vf, next);
860 			return;
861 		}
862 	}
863 
864 	cookie = wsfont_find(NULL, 0, scr->type->fontheight, 0,
865 	    WSDISPLAY_FONTORDER_L2R, 0, WSFONT_FIND_BITMAP);
866 	if (cookie == -1)
867 		return;
868 
869 	if (wsfont_lock(cookie, &wf))
870 		return;
871 
872 	vf = malloc(sizeof(struct vga_raster_font), M_DEVBUF, M_WAITOK);
873 	vf->font = wf;
874 	scr->encoding = vf->font->encoding;
875 	LIST_INSERT_HEAD(&scr->fontset, vf, next);
876 }
877 
878 static void
879 vga_setup_regs(struct videomode *mode, struct vga_moderegs *regs)
880 {
881 	int i;
882 	int depth = 4;			/* XXXBJY hardcoded for now */
883 	const u_int8_t palette[] = {
884 		0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x14, 0x07,
885 		0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f
886 	};
887 
888 	/*
889 	 * Compute hsync and vsync polarity.
890 	 */
891 	if ((mode->flags & (VID_PHSYNC | VID_NHSYNC))
892 	    && (mode->flags & (VID_PVSYNC | VID_NVSYNC))) {
893 	    	regs->miscout = 0x23;
894 		if (mode->flags & VID_NHSYNC)
895 			regs->miscout |= 0x40;
896 		if (mode->flags & VID_NVSYNC)
897 			regs->miscout |= 0x80;
898 	} else {
899 		if (mode->flags & VID_DBLSCAN)
900 			mode->vdisplay *= 2;
901 		if (mode->vdisplay < 400)
902 			regs->miscout = 0xa3;
903 		else if (mode->vdisplay < 480)
904 			regs->miscout = 0x63;
905 		else if (mode->vdisplay < 768)
906 			regs->miscout = 0xe3;
907 		else
908 			regs->miscout = 0x23;
909 	}
910 
911 	/*
912 	 * Time sequencer
913 	 */
914 	if (depth == 4)
915 		regs->ts[0] = 0x02;
916 	else
917 		regs->ts[0] = 0x00;
918 	if (mode->flags & VID_CLKDIV2)
919 		regs->ts[1] = 0x09;
920 	else
921 		regs->ts[1] = 0x01;
922 	regs->ts[2] = 0x0f;
923 	regs->ts[3] = 0x00;
924 	if (depth < 8)
925 		regs->ts[4] = 0x06;
926 	else
927 		regs->ts[4] = 0x0e;
928 
929 	/*
930 	 * CRTC controller
931 	 */
932 	regs->crtc[0] = (mode->htotal >> 3) - 5;
933 	regs->crtc[1] = (mode->hdisplay >> 3) - 1;
934 	regs->crtc[2] = (mode->hsync_start >> 3) - 1;
935 	regs->crtc[3] = (((mode->hsync_end >> 3) - 1) & 0x1f) | 0x80;
936 	regs->crtc[4] = mode->hsync_start >> 3;
937 	regs->crtc[5] = ((((mode->hsync_end >> 3) - 1) & 0x20) << 2)
938 	    | (((mode->hsync_end >> 3)) & 0x1f);
939 	regs->crtc[6] = (mode->vtotal - 2) & 0xff;
940 	regs->crtc[7] = (((mode->vtotal - 2) & 0x100) >> 8)
941 	    | (((mode->vdisplay - 1) & 0x100) >> 7)
942 	    | ((mode->vsync_start & 0x100) >> 6)
943 	    | (((mode->vsync_start - 1) & 0x100) >> 5)
944 	    | 0x10
945 	    | (((mode->vtotal - 2) & 0x200) >> 4)
946 	    | (((mode->vdisplay - 1) & 0x200) >> 3)
947 	    | ((mode->vsync_start & 0x200) >> 2);
948 	regs->crtc[8] = 0x00;
949 	regs->crtc[9] = (((mode->vsync_start - 1) & 0x200) >> 4) | 0x40;
950 	if (mode->flags & VID_DBLSCAN)
951 		regs->crtc[9] |= 0x80;
952 	regs->crtc[10] = 0x00;
953 	regs->crtc[11] = 0x00;
954 	regs->crtc[12] = 0x00;
955 	regs->crtc[13] = 0x00;
956 	regs->crtc[14] = 0x00;
957 	regs->crtc[15] = 0x00;
958 	regs->crtc[16] = mode->vsync_start & 0xff;
959 	regs->crtc[17] = (mode->vsync_end & 0x0f) | 0x20;
960 	regs->crtc[18] = (mode->vdisplay - 1) & 0xff;
961 	regs->crtc[19] = mode->hdisplay >> 4;	/* XXXBJY */
962 	regs->crtc[20] = 0x00;
963 	regs->crtc[21] = (mode->vsync_start - 1) & 0xff;
964 	regs->crtc[22] = (mode->vsync_end - 1) & 0xff;
965 	if (depth < 8)
966 		regs->crtc[23] = 0xe3;
967 	else
968 		regs->crtc[23] = 0xc3;
969 	regs->crtc[24] = 0xff;
970 
971 	/*
972 	 * Graphics display controller
973 	 */
974 	regs->gdc[0] = 0x00;
975 	regs->gdc[1] = 0x00;
976 	regs->gdc[2] = 0x00;
977 	regs->gdc[3] = 0x00;
978 	regs->gdc[4] = 0x00;
979 	if (depth == 4)
980 		regs->gdc[5] = 0x02;
981 	else
982 		regs->gdc[5] = 0x40;
983 	regs->gdc[6] = 0x01;
984 	regs->gdc[7] = 0x0f;
985 	regs->gdc[8] = 0xff;
986 
987 	/*
988 	 * Attribute controller
989 	 */
990 	/* Set palette registers. */
991 	for (i = 0; i < 16; i++)
992 		regs->atc[i] = palette[i];
993 	if (depth == 4)
994 		regs->atc[16] = 0x01;	/* XXXBJY was 0x81 in XFree86 */
995 	else
996 		regs->atc[16] = 0x41;
997 	regs->atc[17] = 0x00;		/* XXXBJY just a guess */
998 	regs->atc[18] = 0x0f;
999 	regs->atc[19] = 0x00;
1000 	regs->atc[20] = 0x00;
1001 }
1002 
1003 static void
1004 vga_set_mode(struct vga_handle *vh, struct vga_moderegs *regs)
1005 {
1006 	int i;
1007 
1008 	/* Disable display. */
1009 	vga_ts_write(vh, mode, vga_ts_read(vh, mode) | VGA_TS_MODE_BLANK);
1010 
1011 	/* Write misc output register. */
1012 	bus_space_write_1(vh->vh_iot, vh->vh_ioh_vga, VGA_MISC_DATAW,
1013 	    regs->miscout);
1014 
1015 	/* Set synchronous reset. */
1016 	vga_ts_write(vh, syncreset, 0x01);
1017 	vga_ts_write(vh, mode, regs->ts[1] | VGA_TS_MODE_BLANK);
1018 	for (i = 2; i < VGA_TS_NREGS; i++)
1019 		_vga_ts_write(vh, i, regs->ts[i]);
1020 	/* Clear synchronous reset. */
1021 	vga_ts_write(vh, syncreset, 0x03);
1022 
1023 	/* Unprotect CRTC registers 0-7. */
1024 	vga_6845_write(vh, vsynce, vga_6845_read(vh, vsynce) & ~0x80);
1025 	/* Write CRTC registers. */
1026 	for (i = 0; i < MC6845_NREGS; i++)
1027 		_vga_6845_write(vh, i, regs->crtc[i]);
1028 
1029 	/* Write graphics display registers. */
1030 	for (i = 0; i < VGA_GDC_NREGS; i++)
1031 		_vga_gdc_write(vh, i, regs->gdc[i]);
1032 
1033 	/* Write attribute controller registers. */
1034 	for (i = 0; i < VGA_ATC_NREGS; i++)
1035 		_vga_attr_write(vh, i, regs->atc[i]);
1036 
1037 	/* Enable display. */
1038 	vga_ts_write(vh, mode, vga_ts_read(vh, mode) & ~VGA_TS_MODE_BLANK);
1039 }
1040 
1041 static void
1042 vga_raster_cursor_init(struct vgascreen *scr, int existing)
1043 {
1044 	int off;
1045 
1046 	if (existing) {
1047 		/*
1048 		 * This is the first screen. At this point, scr->active is
1049 		 * false, so we can't use vga_raster_cursor() to do this.
1050 		 */
1051 		off = (scr->cursorrow * scr->type->ncols + scr->cursorcol) +
1052 		    scr->dispoffset / 8;
1053 
1054 		scr->cursortmp = scr->mem[off];
1055 		vga_raster_putchar(scr, scr->cursorrow, scr->cursorcol,
1056 		    scr->cursortmp.ch, scr->cursortmp.attr ^ 0x77);
1057 	} else {
1058 		scr->cursortmp.ch = 0;
1059 		scr->cursortmp.attr = 0;
1060 		scr->cursortmp.second = 0;
1061 		scr->cursortmp.enc = scr->encoding;
1062 	}
1063 
1064 	scr->cursoron = 1;
1065 }
1066 
1067 static void
1068 vga_raster_cursor(void *id, int on, int row, int col)
1069 {
1070 	struct vgascreen *scr = id;
1071 	int off, tmp;
1072 
1073 	/* Remove old cursor image */
1074 	if (scr->cursoron) {
1075 		off = scr->cursorrow * scr->type->ncols + scr->cursorcol;
1076 		if (scr->active) {
1077 			tmp = scr->encoding;
1078 			scr->encoding = scr->cursortmp.enc;
1079 			if (scr->cursortmp.second)
1080 				vga_raster_putchar(id, scr->cursorrow,
1081 				    scr->cursorcol - 1, scr->cursortmp.ch,
1082 				    scr->cursortmp.attr);
1083 			else
1084 				vga_raster_putchar(id, scr->cursorrow,
1085 				    scr->cursorcol, scr->cursortmp.ch,
1086 				    scr->cursortmp.attr);
1087 			scr->encoding = tmp;
1088 		}
1089 	}
1090 
1091 	scr->cursorrow = row;
1092 	scr->cursorcol = col;
1093 
1094 	if ((scr->cursoron = on) == 0)
1095 		return;
1096 
1097 	off = scr->cursorrow * scr->type->ncols + scr->cursorcol;
1098 	scr->cursortmp = scr->mem[off];
1099 	if (scr->active) {
1100 		tmp = scr->encoding;
1101 		scr->encoding = scr->cursortmp.enc;
1102 		if (scr->cursortmp.second)
1103 			vga_raster_putchar(id, scr->cursorrow,
1104 			    scr->cursorcol - 1, scr->cursortmp.ch,
1105 			    scr->cursortmp.attr ^ 0x77);
1106 		else
1107 			vga_raster_putchar(id, scr->cursorrow,
1108 			    scr->cursorcol, scr->cursortmp.ch,
1109 			    scr->cursortmp.attr ^ 0x77);
1110 		scr->encoding = tmp;
1111 	}
1112 }
1113 
1114 static int
1115 vga_raster_mapchar(void *id, int uni, u_int *index)
1116 {
1117 	struct vgascreen *scr = id;
1118 
1119 	if (scr->encoding == WSDISPLAY_FONTENC_IBM)
1120 		return pcdisplay_mapchar(id, uni, index);
1121 	else {
1122 		*index = uni;
1123 		return 5;
1124 	}
1125 }
1126 
1127 static void
1128 vga_raster_putchar(void *id, int row, int col, u_int c, long attr)
1129 {
1130 	struct vgascreen *scr = id;
1131 	size_t off;
1132 	struct vga_raster_font *fs;
1133 	u_int tmp_ch;
1134 
1135 	off = row * scr->type->ncols + col;
1136 
1137 	if (__predict_false(off >= (scr->type->ncols * scr->type->nrows)))
1138 		return;
1139 
1140 	LIST_FOREACH(fs, &scr->fontset, next) {
1141 		if ((scr->encoding == fs->font->encoding) &&
1142 		    (c >= fs->font->firstchar) &&
1143 		    (c < fs->font->firstchar + fs->font->numchars) &&
1144 		    (scr->type->fontheight == fs->font->fontheight)) {
1145 			if (scr->active) {
1146 				tmp_ch = c - fs->font->firstchar;
1147 				_vga_raster_putchar(scr, row, col, tmp_ch,
1148 				    attr, fs);
1149 			}
1150 
1151 			scr->mem[off].ch = c;
1152 			scr->mem[off].attr = attr;
1153 			scr->mem[off].second = 0;
1154 			scr->mem[off].enc = fs->font->encoding;
1155 
1156 			if (fs->font->stride == 2) {
1157 				scr->mem[off + 1].ch = c;
1158 				scr->mem[off + 1].attr = attr;
1159 				scr->mem[off + 1].second = 1;
1160 				scr->mem[off + 1].enc = fs->font->encoding;
1161 			}
1162 
1163 			return;
1164 		}
1165 	}
1166 
1167 	/*
1168 	 * No match found.
1169 	 */
1170 	if (scr->active)
1171 		/*
1172 		 * Put a single width space character no matter what the
1173 		 * actual width of the character is.
1174 		 */
1175 		_vga_raster_putchar(scr, row, col, ' ', attr,
1176 		    &vga_console_fontset_ascii);
1177 	scr->mem[off].ch = c;
1178 	scr->mem[off].attr = attr;
1179 	scr->mem[off].second = 0;
1180 	scr->mem[off].enc = scr->encoding;
1181 }
1182 
1183 static void
1184 _vga_raster_putchar(void *id, int row, int col, u_int c, long attr,
1185     struct vga_raster_font *fs)
1186 {
1187 	struct vgascreen *scr = id;
1188 	struct vga_handle *vh = scr->hdl;
1189 	bus_space_tag_t memt = vh->vh_memt;
1190 	bus_space_handle_t memh = vh->vh_memh;
1191 	int i;
1192 	int rasoff, rasoff2;
1193 	int fheight = scr->type->fontheight;
1194 	volatile u_int8_t pattern;
1195 	u_int8_t fgcolor, bgcolor;
1196 
1197 	rasoff = scr->dispoffset + row * scr->type->ncols * fheight + col;
1198 	rasoff2 = rasoff;
1199 
1200 #if 0
1201 	bgcolor = bgansitopc[attr >> 4];
1202 	fgcolor = fgansitopc[attr & 0x0f];
1203 #else
1204 	bgcolor = ((attr >> 4) & 0x0f);
1205 	fgcolor = attr & 0x0f;
1206 #endif
1207 
1208 	if (fs->font->stride == 1) {
1209 		/* Paint background. */
1210 		vga_gdc_write(vh, mode, 0x02);
1211 		for (i = 0; i < fheight; i++) {
1212 			bus_space_write_1(memt, memh, rasoff, bgcolor);
1213 			rasoff += scr->type->ncols;
1214 		}
1215 
1216 		/* Draw a single width character. */
1217 		vga_gdc_write(vh, mode, 0x03);
1218 		vga_gdc_write(vh, setres, fgcolor);
1219 		for (i = 0; i < fheight; i++) {
1220 			pattern = ((u_int8_t *)fs->font->data)[c * fheight + i];
1221 			/* When pattern is 0, skip output for speed-up. */
1222 			if (pattern != 0) {
1223 				bus_space_read_1(memt, memh, rasoff2);
1224 				bus_space_write_1(memt, memh, rasoff2, pattern);
1225 			}
1226 			rasoff2 += scr->type->ncols;
1227 		}
1228 	} else if (fs->font->stride == 2) {
1229 		/* Paint background. */
1230 		vga_gdc_write(vh, mode, 0x02);
1231 		for (i = 0; i < fheight; i++) {
1232 			bus_space_write_1(memt, memh, rasoff, bgcolor);
1233 			bus_space_write_1(memt, memh, rasoff + 1, bgcolor);
1234 			rasoff += scr->type->ncols;
1235 		}
1236 
1237 		/* Draw a double width character. */
1238 		vga_gdc_write(vh, mode, 0x03);
1239 		vga_gdc_write(vh, setres, fgcolor);
1240 		for (i = 0; i < fheight; i++) {
1241 			pattern = ((u_int8_t *)fs->font->data)
1242 			    [(c * fheight + i) * 2];
1243 			if (pattern != 0) {
1244 				bus_space_read_1(memt, memh, rasoff2);
1245 				bus_space_write_1(memt, memh, rasoff2, pattern);
1246 			}
1247 			pattern = ((u_int8_t *)fs->font->data)
1248 			    [(c * fheight + i) * 2 + 1];
1249 			if (pattern != 0) {
1250 				rasoff2++;
1251 				bus_space_read_1(memt, memh, rasoff2);
1252 				bus_space_write_1(memt, memh, rasoff2, pattern);
1253 				rasoff2--;
1254 			}
1255 			rasoff2 += scr->type->ncols;
1256 		}
1257 	}
1258 }
1259 
1260 static void
1261 vga_raster_copycols(void *id, int row, int srccol, int dstcol, int ncols)
1262 {
1263 	struct vgascreen *scr = id;
1264 	struct vga_handle *vh = scr->hdl;
1265 	bus_space_tag_t memt = vh->vh_memt;
1266 	bus_space_handle_t memh = vh->vh_memh;
1267 	bus_size_t srcoff, dstoff;
1268 	bus_size_t rassrcoff, rasdstoff;
1269 	int i;
1270 	int fheight = scr->type->fontheight;
1271 
1272 	srcoff = row * scr->type->ncols + srccol;
1273 	dstoff = row * scr->type->ncols + dstcol;
1274 	rassrcoff = scr->dispoffset + row * scr->type->ncols * fheight + srccol;
1275 	rasdstoff = scr->dispoffset + row * scr->type->ncols * fheight + dstcol;
1276 
1277 	memcpy(&scr->mem[dstoff], &scr->mem[srcoff],
1278 	    ncols * sizeof(struct vga_scrmem));
1279 
1280 	vga_gdc_write(vh, mode, 0x01);
1281 	if (scr->active) {
1282 		for (i = 0; i < fheight; i++) {
1283 			bus_space_copy_region_1(memt, memh,
1284 			    rassrcoff + i * scr->type->ncols, memh,
1285 			    rasdstoff + i * scr->type->ncols, ncols);
1286 		}
1287 	}
1288 }
1289 
1290 static void
1291 vga_raster_erasecols(void *id, int row, int startcol, int ncols, long fillattr)
1292 {
1293 	struct vgascreen *scr = id;
1294 	int i;
1295 
1296 	if (scr->active == 0)
1297 		return;
1298 
1299 	for (i = startcol; i < startcol + ncols; i++)
1300 		vga_raster_putchar(id, row, i, ' ', fillattr);
1301 }
1302 
1303 static void
1304 vga_raster_copyrows(void *id, int srcrow, int dstrow, int nrows)
1305 {
1306 	struct vgascreen *scr = id;
1307 	struct vga_handle *vh = scr->hdl;
1308 	bus_space_tag_t memt = vh->vh_memt;
1309 	bus_space_handle_t memh = vh->vh_memh;
1310 	int ncols;
1311 	bus_size_t srcoff, dstoff;
1312 	bus_size_t rassrcoff, rasdstoff;
1313 	int fheight;
1314 
1315 	ncols = scr->type->ncols;
1316 	fheight = scr->type->fontheight;
1317 
1318 	srcoff = srcrow * ncols;
1319 	dstoff = dstrow * ncols;
1320 	rassrcoff = srcoff * fheight;
1321 	rasdstoff = dstoff * fheight;
1322 
1323 	if (scr->active) {
1324 		vga_gdc_write(vh, mode, 0x01);
1325 		if (dstrow == 0 && (srcrow + nrows == scr->type->nrows)) {
1326 			int cursoron = scr->cursoron;
1327 
1328 			if (cursoron)
1329 				/* Disable cursor. */
1330 				vga_raster_cursor(scr, 0,
1331 				    scr->cursorrow, scr->cursorcol);
1332 
1333 			/* scroll up whole screen */
1334 			if ((scr->dispoffset + srcrow * ncols * fheight)
1335 			    <= scr->maxdispoffset)
1336 				scr->dispoffset += srcrow * ncols * fheight;
1337 			else {
1338 				bus_space_copy_region_1(memt, memh,
1339 				    scr->dispoffset + rassrcoff,
1340 				    memh, scr->mindispoffset,
1341 				    nrows * ncols * fheight);
1342 				scr->dispoffset = scr->mindispoffset;
1343 			}
1344 			vga_6845_write(vh, startadrh, scr->dispoffset >> 8);
1345 			vga_6845_write(vh, startadrl, scr->dispoffset);
1346 
1347 			if (cursoron)
1348 				/* Enable cursor. */
1349 				vga_raster_cursor(scr, 1, scr->cursorrow,
1350 				    scr->cursorcol);
1351 		} else
1352 			bus_space_copy_region_1(memt, memh,
1353 			    scr->dispoffset + rassrcoff, memh,
1354 			    scr->dispoffset + rasdstoff,
1355 			    nrows * ncols * fheight);
1356 	}
1357 	memcpy(&scr->mem[dstoff], &scr->mem[srcoff],
1358 	    nrows * ncols * sizeof(struct vga_scrmem));
1359 }
1360 
1361 static void
1362 vga_raster_eraserows(void *id, int startrow, int nrows, long fillattr)
1363 {
1364 	struct vgascreen *scr = id;
1365 	struct vga_handle *vh = scr->hdl;
1366 	bus_space_tag_t memt = vh->vh_memt;
1367 	bus_space_handle_t memh = vh->vh_memh;
1368 	bus_size_t off, count;
1369 	bus_size_t rasoff, rascount;
1370 	int i;
1371 
1372 	off = startrow * scr->type->ncols;
1373 	count = nrows * scr->type->ncols;
1374 	rasoff = off * scr->type->fontheight;
1375 	rascount = count * scr->type->fontheight;
1376 
1377 	if (scr->active) {
1378 		u_int8_t bgcolor = (fillattr >> 4) & 0x0F;
1379 
1380 		/* Paint background. */
1381 		vga_gdc_write(vh, mode, 0x02);
1382 		if (scr->type->ncols % 4 == 0) {
1383 			u_int32_t fill = bgcolor | (bgcolor << 8) |
1384 			    (bgcolor << 16) | (bgcolor << 24);
1385 			/* We can speed up I/O */
1386 			for (i = rasoff; i < rasoff + rascount; i += 4)
1387 				bus_space_write_4(memt, memh,
1388 				    scr->dispoffset + i, fill);
1389 		} else {
1390 			u_int16_t fill = bgcolor | (bgcolor << 8);
1391 			for (i = rasoff; i < rasoff + rascount; i += 2)
1392 				bus_space_write_2(memt, memh,
1393 				    scr->dispoffset + i, fill);
1394 		}
1395 	}
1396 	for (i = 0; i < count; i++) {
1397 		scr->mem[off + i].ch = ' ';
1398 		scr->mem[off + i].attr = fillattr;
1399 		scr->mem[off + i].second = 0;
1400 		scr->mem[off + i].enc = scr->encoding;
1401 	}
1402 }
1403 
1404 static int
1405 vga_raster_allocattr(void *id, int fg, int bg, int flags, long *attrp)
1406 {
1407 	struct vgascreen *scr = id;
1408 	struct vga_config *vc = scr->cfg;
1409 
1410 	if (__predict_false((unsigned int)fg >= sizeof(fgansitopc) ||
1411 	    (unsigned int)bg >= sizeof(bgansitopc)))
1412 	    	return (EINVAL);
1413 
1414 	if (vc->hdl.vh_mono) {
1415 		if (flags & WSATTR_WSCOLORS)
1416 			return (EINVAL);
1417 		if (flags & WSATTR_REVERSE)
1418 			*attrp = 0x70;
1419 		else
1420 			*attrp = 0x07;
1421 		if (flags & WSATTR_UNDERLINE)
1422 			*attrp |= FG_UNDERLINE;
1423 		if (flags & WSATTR_HILIT)
1424 			*attrp |= FG_INTENSE;
1425 	} else {
1426 		if (flags & (WSATTR_UNDERLINE | WSATTR_REVERSE))
1427 			return (EINVAL);
1428 		if (flags & WSATTR_WSCOLORS)
1429 			*attrp = fgansitopc[fg] | bgansitopc[bg];
1430 		else
1431 			*attrp = 7;
1432 		if (flags & WSATTR_HILIT)
1433 			*attrp += 8;
1434 	}
1435 	if (flags & WSATTR_BLINK)
1436 		*attrp |= FG_BLINK;
1437 	return (0);
1438 }
1439 
1440 static void
1441 vga_restore_screen(struct vgascreen *scr,
1442     const struct wsscreen_descr *type, struct vga_scrmem *mem)
1443 {
1444 	int i, j, off, tmp;
1445 
1446 	tmp = scr->encoding;
1447 	for (i = 0; i < type->nrows; i++) {
1448 		for (j = 0; j < type->ncols; j++) {
1449 			off = i * type->ncols + j;
1450 			if (mem[off].second != 1) {
1451 				scr->encoding = mem[off].enc;
1452 				vga_raster_putchar(scr, i, j, mem[off].ch,
1453 				    mem[off].attr);
1454 			}
1455 		}
1456 	}
1457 	scr->encoding = tmp;
1458 }
1459 
1460 static void
1461 vga_raster_setscreentype(struct vga_config *vc,
1462     const struct wsscreen_descr *type)
1463 {
1464 	struct vga_handle *vh = &vc->hdl;
1465 	struct vga_moderegs moderegs;
1466 
1467 	vga_setup_regs((struct videomode *)type->modecookie, &moderegs);
1468 	vga_set_mode(vh, &moderegs);
1469 }
1470 
1471 #ifdef WSDISPLAY_CUSTOM_OUTPUT
1472 static void
1473 vga_raster_replaceattr(void *id, long oldattr, long newattr)
1474 {
1475 	struct vgascreen *scr = id;
1476 	const struct wsscreen_descr *type = scr->type;
1477 	int off;
1478 
1479 	for (off = 0; off < type->nrows * type->ncols; off++) {
1480 		if (scr->mem[off].attr == oldattr)
1481 			scr->mem[off].attr = newattr;
1482 	}
1483 
1484 	/* Repaint the whole screen, if needed */
1485 	if (scr->active)
1486 		vga_restore_screen(scr, type, scr->mem);
1487 }
1488 #endif /* WSDISPLAY_CUSTOM_OUTPUT */
1489 
1490 void
1491 vga_resume(struct vga_softc *sc)
1492 {
1493 #ifdef VGA_RESET_ON_RESUME
1494 	vga_initregs(&sc->sc_vc->hdl);
1495 #endif
1496 }
1497