vga_raster.c revision 1.4 1 /* $NetBSD: vga_raster.c,v 1.4 2002/11/04 08:05:53 junyoung 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 char *typestr;
316
317 if (check && !vga_common_probe(iot, memt))
318 return (ENXIO);
319
320 /* set up bus-independent VGA configuration */
321 vga_raster_init(&vga_console_vc, iot, memt);
322 #ifdef VGA_CONSOLE_SCREENTYPE
323 scr = wsdisplay_screentype_pick(vga_console_vc.hdl.vh_mono ?
324 &vga_screenlist_mono : &vga_screenlist, VGA_CONSOLE_SCREENTYPE);
325 if (!scr)
326 /* Invalid screen type, continue with the default mode. */
327 typestr = "80x25";
328 else if (scr->nrows > 30)
329 /* Unsupported screen type, try 80x30. */
330 typestr = "80x30";
331 scr = wsdisplay_screentype_pick(vga_console_vc.hdl.vh_mono ?
332 &vga_screenlist_mono : &vga_screenlist, typestr);
333 if (scr != vga_console_vc.currenttype)
334 vga_console_vc.currenttype = scr;
335 #else
336 scr = vga_console_vc.currenttype;
337 #endif
338 vga_raster_init_screen(&vga_console_vc, &vga_console_screen, scr, 1,
339 &defattr);
340
341 vga_console_screen.active = 1;
342 vga_console_vc.active = &vga_console_screen;
343
344 wsdisplay_cnattach(scr, &vga_console_screen,
345 vga_console_screen.cursorcol, vga_console_screen.cursorrow,
346 defattr);
347
348 vgaconsole = 1;
349 vga_console_type = type;
350 return (0);
351 }
352
353 void
354 vga_raster_init(struct vga_config *vc, bus_space_tag_t iot,
355 bus_space_tag_t memt)
356 {
357 struct vga_handle *vh = &vc->hdl;
358 u_int8_t mor;
359 struct vga_raster_font *vf;
360
361 vh->vh_iot = iot;
362 vh->vh_memt = memt;
363
364 if (bus_space_map(vh->vh_iot, 0x3c0, 0x10, 0, &vh->vh_ioh_vga))
365 panic("vga_raster_init: couldn't map vga io");
366
367 /* read "misc output register" */
368 mor = bus_space_read_1(vh->vh_iot, vh->vh_ioh_vga, 0xc);
369 vh->vh_mono = !(mor & 1);
370
371 if (bus_space_map(vh->vh_iot, (vh->vh_mono ? 0x3b0 : 0x3d0), 0x10, 0,
372 &vh->vh_ioh_6845))
373 panic("vga_raster_init: couldn't map 6845 io");
374
375 if (bus_space_map(vh->vh_memt, 0xa0000, 0x20000, 0, &vh->vh_allmemh))
376 panic("vga_raster_init: couldn't map memory");
377
378 if (bus_space_subregion(vh->vh_memt, vh->vh_allmemh, 0, 0x10000,
379 &vh->vh_memh))
380 panic("vga_raster_init: mem subrange failed");
381
382 /* should only reserve the space (no need to map - save KVM) */
383 vc->vc_biostag = memt;
384 if (bus_space_map(vc->vc_biostag, 0xc0000, 0x8000, 0, &vc->vc_bioshdl))
385 vc->vc_biosmapped = 0;
386 else
387 vc->vc_biosmapped = 1;
388
389 vc->nscreens = 0;
390 LIST_INIT(&vc->screens);
391 vc->active = NULL;
392 vc->currenttype = vh->vh_mono ? &vga_25lscreen_mono : &vga_25lscreen;
393 callout_init(&vc->vc_switch_callout);
394
395 vc->nfonts = 1;
396 LIST_INIT(&vc->vc_fontlist);
397 vf = &vga_console_fontset_ascii;
398 vga_load_builtinfont(vh, builtinfont_data, 0, 256);
399 vf->font = &builtinfont;
400 LIST_INSERT_HEAD(&vc->vc_fontlist, vf, next);
401 }
402
403 void
404 vga_raster_init_screen(struct vga_config *vc, struct vgascreen *scr,
405 const struct wsscreen_descr *type, int existing, long *attrp)
406 {
407 int cpos;
408 int res;
409 struct vga_handle *vh;
410
411 scr->cfg = vc;
412 scr->hdl = &vc->hdl;
413 scr->type = type;
414 scr->mindispoffset = 0;
415 scr->maxdispoffset = 0x10000;
416 scr->encoding = WSDISPLAY_FONTENC_IBM;
417 vh = &vc->hdl;
418
419 wsfont_init();
420 LIST_INIT(&scr->fontset);
421 vga_raster_setup_font(vc, scr);
422
423 if (existing) {
424 int i;
425
426 cpos = vga_6845_read(vh, cursorh) << 8;
427 cpos |= vga_6845_read(vh, cursorl);
428
429 /* make sure we have a valid cursor position */
430 if (cpos < 0 || cpos >= type->nrows * type->ncols)
431 cpos = 0;
432
433 scr->dispoffset = vga_6845_read(vh, startadrh) << 9;
434 scr->dispoffset |= vga_6845_read(vh, startadrl) << 1;
435
436 /* make sure we have a valid memory offset */
437 if (scr->dispoffset < scr->mindispoffset ||
438 scr->dispoffset > scr->maxdispoffset)
439 scr->dispoffset = scr->mindispoffset;
440
441 scr->mem = boot_scrmem;
442 scr->active = 1;
443
444 /* Save the current screen to memory. XXXBJY assume 80x25 */
445 for (i = 0; i < 80 * 25; i++) {
446 scr->mem[i].ch = bus_space_read_1(vh->vh_memt,
447 vh->vh_allmemh, 0x18000 + i * 2);
448 scr->mem[i].attr = bus_space_read_1(vh->vh_memt,
449 vh->vh_allmemh, 0x18000 + i * 2 + 1);
450 scr->mem[i].enc = WSDISPLAY_FONTENC_IBM;
451 }
452
453 vga_raster_setscreentype(vc, type);
454
455 /* Clear the entire screen. */
456 vga_gdc_write(vh, mode, 0x02);
457 bus_space_set_region_4(vh->vh_memt, vh->vh_allmemh, 0, 0,
458 0x4000);
459
460 vga_restore_screen(scr, type, scr->mem);
461
462 /* Delay to prevent the boot screen from being too
463 fast scrolled up. */
464 delay(1000000);
465 } else {
466 cpos = 0;
467 scr->dispoffset = scr->mindispoffset;
468 scr->mem = NULL;
469 scr->active = 0;
470 }
471
472 scr->cursorrow = cpos / type->ncols;
473 scr->cursorcol = cpos % type->ncols;
474 vga_raster_cursor_init(scr, existing);
475
476 #ifdef __alpha__
477 if (!vc->hdl.vh_mono)
478 /*
479 * DEC firmware uses a blue background.
480 */
481 res = vga_raster_allocattr(scr, WSCOL_WHITE, WSCOL_BLUE,
482 WSATTR_WSCOLORS, attrp);
483 else
484 #endif
485 res = vga_raster_allocattr(scr, 0, 0, 0, attrp);
486 #ifdef DIAGNOSTIC
487 if (res)
488 panic("vga_raster_init_screen: attribute botch");
489 #endif
490
491 vc->nscreens++;
492 LIST_INSERT_HEAD(&vc->screens, scr, next);
493 }
494
495 void
496 vga_common_attach(struct vga_softc *sc, bus_space_tag_t iot,
497 bus_space_tag_t memt, int type, int quirks, const struct vga_funcs *vf)
498 {
499 int console;
500 struct vga_config *vc;
501 struct wsemuldisplaydev_attach_args aa;
502
503 console = vga_is_console(iot, type);
504
505 if (console) {
506 vc = &vga_console_vc;
507 vga_console_attached = 1;
508 } else {
509 vc = malloc(sizeof(struct vga_config), M_DEVBUF,
510 M_WAITOK);
511 vga_raster_init(vc, iot, memt);
512 }
513
514 vc->vc_type = type;
515 vc->vc_funcs = vf;
516
517 sc->sc_vc = vc;
518 vc->softc = sc;
519
520 aa.console = console;
521 aa.scrdata = (vc->hdl.vh_mono ? &vga_screenlist_mono : &vga_screenlist);
522 aa.accessops = &vga_raster_accessops;
523 aa.accesscookie = vc;
524
525 config_found(&sc->sc_dev, &aa, wsemuldisplaydevprint);
526 }
527
528 int
529 vga_is_console(bus_space_tag_t iot, int type)
530 {
531 if (vgaconsole &&
532 !vga_console_attached &&
533 iot == vga_console_vc.hdl.vh_iot &&
534 (vga_console_type == -1 || (type == vga_console_type)))
535 return (1);
536 return (0);
537 }
538
539 #define VGA_TS_BLANK 0x20
540
541 static int
542 vga_get_video(struct vga_config *vc)
543 {
544 return (vga_ts_read(&vc->hdl, mode) & VGA_TS_BLANK) == 0;
545 }
546
547 static void
548 vga_set_video(struct vga_config *vc, int state)
549 {
550 int val;
551
552 vga_ts_write(&vc->hdl, syncreset, 0x01);
553 if (state) { /* unblank screen */
554 val = vga_ts_read(&vc->hdl, mode);
555 vga_ts_write(&vc->hdl, mode, val & ~VGA_TS_BLANK);
556 #ifndef VGA_NO_VBLANK
557 val = vga_6845_read(&vc->hdl, mode);
558 vga_6845_write(&vc->hdl, mode, val | 0x80);
559 #endif
560 } else { /* blank screen */
561 val = vga_ts_read(&vc->hdl, mode);
562 vga_ts_write(&vc->hdl, mode, val | VGA_TS_BLANK);
563 #ifndef VGA_NO_VBLANK
564 val = vga_6845_read(&vc->hdl, mode);
565 vga_6845_write(&vc->hdl, mode, val & ~0x80);
566 #endif
567 }
568 vga_ts_write(&vc->hdl, syncreset, 0x03);
569 }
570
571 int
572 vga_raster_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
573 {
574 struct vga_config *vc = v;
575 const struct vga_funcs *vf = vc->vc_funcs;
576
577 switch (cmd) {
578 case WSDISPLAYIO_GTYPE:
579 *(int *)data = vc->vc_type;
580 return 0;
581
582 case WSDISPLAYIO_GINFO:
583 /* XXX should get detailed hardware information here */
584 return EPASSTHROUGH;
585
586 case WSDISPLAYIO_GVIDEO:
587 #if 1
588 *(int *)data = (vga_get_video(vc) ? WSDISPLAYIO_VIDEO_ON :
589 WSDISPLAYIO_VIDEO_OFF);
590 return 0;
591 #endif
592
593 case WSDISPLAYIO_SVIDEO:
594 #if 1
595 vga_set_video(vc, *(int *)data == WSDISPLAYIO_VIDEO_ON);
596 return 0;
597 #endif
598
599 case WSDISPLAYIO_GETCMAP:
600 case WSDISPLAYIO_PUTCMAP:
601 case WSDISPLAYIO_GCURPOS:
602 case WSDISPLAYIO_SCURPOS:
603 case WSDISPLAYIO_GCURMAX:
604 case WSDISPLAYIO_GCURSOR:
605 case WSDISPLAYIO_SCURSOR:
606 /* NONE of these operations are by the generic VGA driver. */
607 return EPASSTHROUGH;
608 }
609
610 if (vc->vc_funcs == NULL)
611 return (EPASSTHROUGH);
612
613 if (vf->vf_ioctl == NULL)
614 return (EPASSTHROUGH);
615
616 return ((*vf->vf_ioctl)(v, cmd, data, flag, p));
617
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(0, 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 /* Put a single width space character no matter what the
1105 actual width of the character is. */
1106 _vga_raster_putchar(scr, row, col, 0x20, attr,
1107 &vga_console_fontset_ascii);
1108 scr->mem[off].ch = c;
1109 scr->mem[off].attr = attr;
1110 scr->mem[off].second = 0;
1111 scr->mem[off].enc = WSDISPLAY_FONTENC_IBM;
1112 }
1113
1114 static void
1115 _vga_raster_putchar(void *id, int row, int col, u_int c, long attr,
1116 struct vga_raster_font *fs)
1117 {
1118 struct vgascreen *scr = id;
1119 struct vga_handle *vh = scr->hdl;
1120 bus_space_tag_t memt = vh->vh_memt;
1121 bus_space_handle_t memh = vh->vh_memh;
1122 int i;
1123 int rasoff, rasoff2;
1124 int fheight = scr->type->fontheight;
1125 volatile u_int8_t dummy, pattern;
1126 u_int8_t fgcolor, bgcolor;
1127
1128 rasoff = scr->dispoffset + row * scr->type->ncols * fheight + col;
1129 rasoff2 = rasoff;
1130
1131 #if 0
1132 bgcolor = bgansitopc[attr >> 4];
1133 fgcolor = fgansitopc[attr & 0x0f];
1134 #else
1135 bgcolor = ((attr >> 4) & 0x0f);
1136 fgcolor = attr & 0x0f;
1137 #endif
1138
1139 if (fs->font->stride == 1) {
1140 /* Paint background. */
1141 vga_gdc_write(vh, mode, 0x02);
1142 for (i = 0; i < fheight; i++) {
1143 bus_space_write_1(memt, memh, rasoff, bgcolor);
1144 rasoff += scr->type->ncols;
1145 }
1146
1147 /* Draw a single width character. */
1148 vga_gdc_write(vh, mode, 0x03);
1149 vga_gdc_write(vh, setres, fgcolor);
1150 for (i = 0; i < fheight; i++) {
1151 pattern = ((u_int8_t *)fs->font->data)[c * fheight + i];
1152 /* When pattern is 0, skip output for speed-up. */
1153 if (pattern != 0) {
1154 dummy = bus_space_read_1(memt, memh, rasoff2);
1155 bus_space_write_1(memt, memh, rasoff2, pattern);
1156 }
1157 rasoff2 += scr->type->ncols;
1158 }
1159 } else if (fs->font->stride == 2) {
1160 /* Paint background. */
1161 vga_gdc_write(vh, mode, 0x02);
1162 for (i = 0; i < fheight; i++) {
1163 bus_space_write_1(memt, memh, rasoff, bgcolor);
1164 bus_space_write_1(memt, memh, rasoff + 1, bgcolor);
1165 rasoff += scr->type->ncols;
1166 }
1167
1168 /* Draw a double width character. */
1169 vga_gdc_write(vh, mode, 0x03);
1170 vga_gdc_write(vh, setres, fgcolor);
1171 for (i = 0; i < fheight; i++) {
1172 pattern = ((u_int8_t *)fs->font->data)
1173 [(c * fheight + i) * 2];
1174 if (pattern != 0) {
1175 dummy = bus_space_read_1(memt, memh, rasoff2);
1176 bus_space_write_1(memt, memh, rasoff2, pattern);
1177 }
1178 pattern = ((u_int8_t *)fs->font->data)
1179 [(c * fheight + i) * 2 + 1];
1180 if (pattern != 0) {
1181 rasoff2++;
1182 dummy = bus_space_read_1(memt, memh, rasoff2);
1183 bus_space_write_1(memt, memh, rasoff2, pattern);
1184 rasoff2--;
1185 }
1186 rasoff2 += scr->type->ncols;
1187 }
1188 }
1189 }
1190
1191 void
1192 vga_raster_copycols(void *id, int row, int srccol, int dstcol, int ncols)
1193 {
1194 struct vgascreen *scr = id;
1195 struct vga_handle *vh = scr->hdl;
1196 bus_space_tag_t memt = vh->vh_memt;
1197 bus_space_handle_t memh = vh->vh_memh;
1198 bus_size_t srcoff, dstoff;
1199 bus_size_t rassrcoff, rasdstoff;
1200 int i;
1201 int fheight = scr->type->fontheight;
1202
1203 srcoff = row * scr->type->ncols + srccol;
1204 dstoff = row * scr->type->ncols + dstcol;
1205 rassrcoff = scr->dispoffset + row * scr->type->ncols * fheight + srccol;
1206 rasdstoff = scr->dispoffset + row * scr->type->ncols * fheight + dstcol;
1207
1208 memcpy(&scr->mem[dstoff], &scr->mem[srcoff],
1209 ncols * sizeof(struct vga_scrmem));
1210
1211 vga_gdc_write(vh, mode, 0x01);
1212 if (scr->active) {
1213 for (i = 0; i < fheight; i++) {
1214 bus_space_copy_region_1(memt, memh,
1215 rassrcoff + i * scr->type->ncols, memh,
1216 rasdstoff + i * scr->type->ncols, ncols);
1217 }
1218 }
1219 }
1220
1221 void
1222 vga_raster_erasecols(void *id, int row, int startcol, int ncols, long fillattr)
1223 {
1224 struct vgascreen *scr = id;
1225 int i;
1226
1227 if (scr->active == 0)
1228 return;
1229
1230 for (i = startcol; i < startcol + ncols; i++)
1231 vga_raster_putchar(id, row, i, ' ', fillattr);
1232 }
1233
1234 void
1235 vga_raster_copyrows(void *id, int srcrow, int dstrow, int nrows)
1236 {
1237 struct vgascreen *scr = id;
1238 struct vga_handle *vh = scr->hdl;
1239 bus_space_tag_t memt = vh->vh_memt;
1240 bus_space_handle_t memh = vh->vh_memh;
1241 int ncols;
1242 bus_size_t srcoff, dstoff;
1243 bus_size_t rassrcoff, rasdstoff;
1244 int fheight;
1245
1246 ncols = scr->type->ncols;
1247 fheight = scr->type->fontheight;
1248
1249 srcoff = srcrow * ncols;
1250 dstoff = dstrow * ncols;
1251 rassrcoff = srcoff * fheight;
1252 rasdstoff = dstoff * fheight;
1253
1254 if (scr->active) {
1255 vga_gdc_write(vh, mode, 0x01);
1256 if (dstrow == 0 && (srcrow + nrows == scr->type->nrows)) {
1257 int cursoron = scr->cursoron;
1258
1259 if (cursoron)
1260 /* Disable cursor. */
1261 vga_raster_cursor(scr, 0,
1262 scr->cursorrow, scr->cursorcol);
1263
1264 /* scroll up whole screen */
1265 if ((scr->dispoffset + srcrow * ncols * fheight)
1266 <= scr->maxdispoffset)
1267 scr->dispoffset += srcrow * ncols * fheight;
1268 else {
1269 bus_space_copy_region_1(memt, memh,
1270 scr->dispoffset + rassrcoff,
1271 memh, scr->mindispoffset,
1272 nrows * ncols * fheight);
1273 scr->dispoffset = scr->mindispoffset;
1274 }
1275 vga_6845_write(vh, startadrh, scr->dispoffset >> 8);
1276 vga_6845_write(vh, startadrl, scr->dispoffset);
1277
1278 if (cursoron)
1279 /* Enable cursor. */
1280 vga_raster_cursor(scr, 1, scr->cursorrow,
1281 scr->cursorcol);
1282 } else
1283 bus_space_copy_region_1(memt, memh,
1284 scr->dispoffset + rassrcoff, memh,
1285 scr->dispoffset + rasdstoff,
1286 nrows * ncols * fheight);
1287 }
1288 memcpy(&scr->mem[dstoff], &scr->mem[srcoff],
1289 nrows * ncols * sizeof(struct vga_scrmem));
1290 }
1291
1292 void
1293 vga_raster_eraserows(void *id, int startrow, int nrows, long fillattr)
1294 {
1295 struct vgascreen *scr = id;
1296 struct vga_handle *vh = scr->hdl;
1297 bus_space_tag_t memt = vh->vh_memt;
1298 bus_space_handle_t memh = vh->vh_memh;
1299 bus_size_t off, count;
1300 bus_size_t rasoff, rascount;
1301 int i;
1302
1303 off = startrow * scr->type->ncols;
1304 count = nrows * scr->type->ncols;
1305 rasoff = off * scr->type->fontheight;
1306 rascount = count * scr->type->fontheight;
1307
1308 if (scr->active) {
1309 /* Paint background. */
1310 vga_gdc_write(vh, mode, 0x02);
1311 if (scr->type->ncols % 4 == 0)
1312 /* We can speed up I/O */
1313 for (i = rasoff; i < rasoff + rascount; i += 4)
1314 bus_space_write_4(memt, memh,
1315 scr->dispoffset + i, fillattr >> 4);
1316 else
1317 for (i = rasoff; i < rasoff + rascount; i += 2)
1318 bus_space_write_2(memt, memh,
1319 scr->dispoffset + i, fillattr >> 4);
1320 }
1321 for (i = 0; i < count; i++) {
1322 scr->mem[off + i].ch = ' ';
1323 scr->mem[off + i].attr = fillattr;
1324 scr->mem[off + i].second = 0;
1325 scr->mem[off + i].enc = WSDISPLAY_FONTENC_IBM;
1326 }
1327 }
1328
1329 static int
1330 vga_raster_allocattr(void *id, int fg, int bg, int flags, long *attrp)
1331 {
1332 struct vgascreen *scr = id;
1333 struct vga_config *vc = scr->cfg;
1334
1335 if (vc->hdl.vh_mono) {
1336 if (flags & WSATTR_WSCOLORS)
1337 return (EINVAL);
1338 if (flags & WSATTR_REVERSE)
1339 *attrp = 0x70;
1340 else
1341 *attrp = 0x07;
1342 if (flags & WSATTR_UNDERLINE)
1343 *attrp |= FG_UNDERLINE;
1344 if (flags & WSATTR_HILIT)
1345 *attrp |= FG_INTENSE;
1346 } else {
1347 if (flags & (WSATTR_UNDERLINE | WSATTR_REVERSE))
1348 return (EINVAL);
1349 if (flags & WSATTR_WSCOLORS)
1350 *attrp = fgansitopc[fg] | bgansitopc[bg];
1351 else
1352 *attrp = 7;
1353 if (flags & WSATTR_HILIT)
1354 *attrp += 8;
1355 }
1356 if (flags & WSATTR_BLINK)
1357 *attrp |= FG_BLINK;
1358 return (0);
1359 }
1360
1361 void
1362 vga_restore_screen(struct vgascreen *scr,
1363 const struct wsscreen_descr *type, struct vga_scrmem *mem)
1364 {
1365 int i, j, off, tmp;
1366
1367 tmp = scr->encoding;
1368 for (i = 0; i < type->nrows; i++) {
1369 for (j = 0; j < type->ncols; j++) {
1370 off = i * type->ncols + j;
1371 if (mem[off].second != 1) {
1372 scr->encoding = mem[off].enc;
1373 vga_raster_putchar(scr, i, j, mem[off].ch,
1374 mem[off].attr);
1375 }
1376 }
1377 }
1378 scr->encoding = tmp;
1379 }
1380
1381 void
1382 vga_raster_setscreentype(struct vga_config *vc,
1383 const struct wsscreen_descr *type)
1384 {
1385 struct vga_handle *vh = &vc->hdl;
1386 struct vga_moderegs moderegs;
1387
1388 vga_setup_regs((struct videomode *)type->modecookie, &moderegs);
1389 vga_set_mode(vh, &moderegs);
1390 }
1391