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