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