Home | History | Annotate | Line # | Download | only in sbus
mgx.c revision 1.2
      1 /*	$NetBSD: mgx.c,v 1.2 2015/01/04 18:18:20 macallan Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2014 Michael Lorenz
      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  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 /* a console driver for the SSB 4096V-MGX graphics card */
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: mgx.c,v 1.2 2015/01/04 18:18:20 macallan Exp $");
     33 
     34 #include <sys/param.h>
     35 #include <sys/systm.h>
     36 #include <sys/buf.h>
     37 #include <sys/device.h>
     38 #include <sys/ioctl.h>
     39 #include <sys/conf.h>
     40 #include <sys/kmem.h>
     41 
     42 #include <sys/bus.h>
     43 #include <machine/autoconf.h>
     44 
     45 #include <dev/sbus/sbusvar.h>
     46 #include <dev/sun/fbio.h>
     47 #include <dev/sun/fbvar.h>
     48 
     49 #include <dev/wscons/wsdisplayvar.h>
     50 #include <dev/wscons/wsconsio.h>
     51 #include <dev/wsfont/wsfont.h>
     52 #include <dev/rasops/rasops.h>
     53 
     54 #include <dev/wscons/wsdisplay_vconsvar.h>
     55 #include <dev/wscons/wsdisplay_glyphcachevar.h>
     56 
     57 #include <dev/ic/vgareg.h>
     58 #include <dev/sbus/mgxreg.h>
     59 
     60 #include "opt_wsemul.h"
     61 
     62 
     63 struct mgx_softc {
     64 	device_t	sc_dev;
     65 	bus_space_tag_t sc_tag;
     66 	bus_space_handle_t sc_blith;
     67 	bus_space_handle_t sc_vgah;
     68 	bus_addr_t	sc_paddr;
     69 	void		*sc_fbaddr;
     70 	int		sc_width;
     71 	int		sc_height;
     72 	int		sc_stride;
     73 	int		sc_fbsize;
     74 	int		sc_mode;
     75 	uint32_t	sc_dec;
     76 	u_char		sc_cmap_red[256];
     77 	u_char		sc_cmap_green[256];
     78 	u_char		sc_cmap_blue[256];
     79 	void (*sc_putchar)(void *, int, int, u_int, long);
     80 	struct vcons_screen sc_console_screen;
     81 	struct wsscreen_descr sc_defaultscreen_descr;
     82 	const struct wsscreen_descr *sc_screens[1];
     83 	struct wsscreen_list sc_screenlist;
     84 	struct vcons_data vd;
     85 	glyphcache 	sc_gc;
     86 };
     87 
     88 static int	mgx_match(device_t, cfdata_t, void *);
     89 static void	mgx_attach(device_t, device_t, void *);
     90 static int	mgx_ioctl(void *, void *, u_long, void *, int,
     91 				 struct lwp*);
     92 static paddr_t	mgx_mmap(void *, void *, off_t, int);
     93 static void	mgx_init_screen(void *, struct vcons_screen *, int,
     94 				 long *);
     95 static void	mgx_write_dac(struct mgx_softc *, int, int, int, int);
     96 static void	mgx_setup(struct mgx_softc *, int);
     97 static void	mgx_init_palette(struct mgx_softc *);
     98 static int	mgx_wait_engine(struct mgx_softc *);
     99 static int	mgx_wait_fifo(struct mgx_softc *, unsigned int);
    100 
    101 static void	mgx_bitblt(void *, int, int, int, int, int, int, int);
    102 static void 	mgx_rectfill(void *, int, int, int, int, long);
    103 
    104 static void	mgx_putchar(void *, int, int, u_int, long);
    105 static void	mgx_cursor(void *, int, int, int);
    106 static void	mgx_copycols(void *, int, int, int, int);
    107 static void	mgx_erasecols(void *, int, int, int, long);
    108 static void	mgx_copyrows(void *, int, int, int);
    109 static void	mgx_eraserows(void *, int, int, long);
    110 
    111 CFATTACH_DECL_NEW(mgx, sizeof(struct mgx_softc),
    112     mgx_match, mgx_attach, NULL, NULL);
    113 
    114 struct wsdisplay_accessops mgx_accessops = {
    115 	mgx_ioctl,
    116 	mgx_mmap,
    117 	NULL,	/* vcons_alloc_screen */
    118 	NULL,	/* vcons_free_screen */
    119 	NULL,	/* vcons_show_screen */
    120 	NULL,	/* load_font */
    121 	NULL,	/* polls */
    122 	NULL,	/* scroll */
    123 };
    124 
    125 static int
    126 mgx_match(device_t parent, cfdata_t cf, void *aux)
    127 {
    128 	struct sbus_attach_args *sa = aux;
    129 
    130 	if (strcmp("SMSI,mgx", sa->sa_name) == 0)
    131 		return 100;
    132 	return 0;
    133 }
    134 
    135 /*
    136  * Attach a display.  We need to notice if it is the console, too.
    137  */
    138 static void
    139 mgx_attach(device_t parent, device_t self, void *args)
    140 {
    141 	struct mgx_softc *sc = device_private(self);
    142 	struct sbus_attach_args *sa = args;
    143 	struct wsemuldisplaydev_attach_args aa;
    144 	struct rasops_info *ri;
    145 	unsigned long defattr;
    146 	bus_space_handle_t bh;
    147 	int node = sa->sa_node;
    148 	int isconsole;
    149 
    150 	aprint_normal("\n");
    151 	sc->sc_dev = self;
    152 	sc->sc_tag = sa->sa_bustag;
    153 
    154 	sc->sc_paddr = sbus_bus_addr(sa->sa_bustag, sa->sa_slot,
    155 	    sa->sa_reg[8].oa_base);
    156 
    157 	/* read geometry information from the device tree */
    158 	sc->sc_width = prom_getpropint(sa->sa_node, "width", 1152);
    159 	sc->sc_height = prom_getpropint(sa->sa_node, "height", 900);
    160 	sc->sc_stride = prom_getpropint(sa->sa_node, "linebytes", 900);
    161 	sc->sc_fbsize = sc->sc_height * sc->sc_stride;
    162 	sc->sc_fbaddr = NULL;
    163 	if (sc->sc_fbaddr == NULL) {
    164 		if (sbus_bus_map(sa->sa_bustag,
    165 			 sa->sa_slot,
    166 			 sa->sa_reg[8].oa_base,
    167 			 sc->sc_fbsize,
    168 			 BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_LARGE,
    169 			 &bh) != 0) {
    170 			aprint_error_dev(self, "cannot map framebuffer\n");
    171 			return;
    172 		}
    173 		sc->sc_fbaddr = bus_space_vaddr(sa->sa_bustag, bh);
    174 	}
    175 
    176 	aprint_normal_dev(self, "%d x %d\n", sc->sc_width, sc->sc_height);
    177 
    178 	if (sbus_bus_map(sa->sa_bustag,
    179 			 sa->sa_slot,
    180 			 sa->sa_reg[4].oa_base, 0x1000, 0,
    181 			 &sc->sc_vgah) != 0) {
    182 		aprint_error("%s: couldn't map VGA registers\n",
    183 		    device_xname(sc->sc_dev));
    184 		return;
    185 	}
    186 
    187 	if (sbus_bus_map(sa->sa_bustag,
    188 			 sa->sa_slot,
    189 			 sa->sa_reg[5].oa_base + MGX_REG_ATREG_OFFSET, 0x1000,
    190 			 0, &sc->sc_blith) != 0) {
    191 		aprint_error("%s: couldn't map blitter registers\n",
    192 		    device_xname(sc->sc_dev));
    193 		return;
    194 	}
    195 
    196 	mgx_setup(sc, 8);
    197 
    198 	sc->sc_defaultscreen_descr = (struct wsscreen_descr) {
    199 		"default",
    200 		0, 0,
    201 		NULL,
    202 		8, 16,
    203 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    204 		NULL
    205 	};
    206 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
    207 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
    208 
    209 	isconsole = fb_is_console(node);
    210 
    211 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    212 	wsfont_init();
    213 
    214 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr, &mgx_accessops);
    215 	sc->vd.init_screen = mgx_init_screen;
    216 
    217 	vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1, &defattr);
    218 	sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    219 
    220 	ri = &sc->sc_console_screen.scr_ri;
    221 
    222 	sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    223 	sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    224 	sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    225 	sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    226 
    227 	sc->sc_gc.gc_bitblt = mgx_bitblt;
    228 	sc->sc_gc.gc_rectfill = mgx_rectfill;
    229 	sc->sc_gc.gc_blitcookie = sc;
    230 	sc->sc_gc.gc_rop = ROP_SRC;
    231 
    232 	glyphcache_init(&sc->sc_gc,
    233 	    sc->sc_height + 5,
    234 	    (0x400000 / sc->sc_stride) - sc->sc_height - 5,
    235 	    sc->sc_width,
    236 	    ri->ri_font->fontwidth,
    237 	    ri->ri_font->fontheight,
    238 	    defattr);
    239 
    240 	mgx_init_palette(sc);
    241 
    242 	if(isconsole) {
    243 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
    244 		    defattr);
    245 		vcons_replay_msgbuf(&sc->sc_console_screen);
    246 	}
    247 
    248 	aa.console = isconsole;
    249 	aa.scrdata = &sc->sc_screenlist;
    250 	aa.accessops = &mgx_accessops;
    251 	aa.accesscookie = &sc->vd;
    252 
    253 	config_found(self, &aa, wsemuldisplaydevprint);
    254 }
    255 
    256 static inline void
    257 mgx_write_vga(struct mgx_softc *sc, uint32_t reg, uint8_t val)
    258 {
    259 	bus_space_write_1(sc->sc_tag, sc->sc_vgah, reg ^ 3, val);
    260 }
    261 
    262 static inline void
    263 mgx_write_1(struct mgx_softc *sc, uint32_t reg, uint8_t val)
    264 {
    265 	bus_space_write_1(sc->sc_tag, sc->sc_blith, reg ^ 3, val);
    266 }
    267 
    268 static inline uint8_t
    269 mgx_read_1(struct mgx_softc *sc, uint32_t reg)
    270 {
    271 	return bus_space_read_1(sc->sc_tag, sc->sc_blith, reg ^ 3);
    272 }
    273 
    274 static inline void
    275 mgx_write_4(struct mgx_softc *sc, uint32_t reg, uint32_t val)
    276 {
    277 	bus_space_write_4(sc->sc_tag, sc->sc_blith, reg, val);
    278 }
    279 
    280 
    281 static void
    282 mgx_write_dac(struct mgx_softc *sc, int idx, int r, int g, int b)
    283 {
    284 	mgx_write_vga(sc, VGA_BASE + VGA_DAC_ADDRW, idx);
    285 	mgx_write_vga(sc, VGA_BASE + VGA_DAC_PALETTE, r);
    286 	mgx_write_vga(sc, VGA_BASE + VGA_DAC_PALETTE, g);
    287 	mgx_write_vga(sc, VGA_BASE + VGA_DAC_PALETTE, b);
    288 }
    289 
    290 static void
    291 mgx_init_palette(struct mgx_softc *sc)
    292 {
    293 	struct rasops_info *ri = &sc->sc_console_screen.scr_ri;
    294 	int i, j = 0;
    295 	uint8_t cmap[768];
    296 
    297 	rasops_get_cmap(ri, cmap, sizeof(cmap));
    298 	for (i = 0; i < 256; i++) {
    299 		sc->sc_cmap_red[i] = cmap[j];
    300 		sc->sc_cmap_green[i] = cmap[j + 1];
    301 		sc->sc_cmap_blue[i] = cmap[j + 2];
    302 		mgx_write_dac(sc, i, cmap[j], cmap[j + 1], cmap[j + 2]);
    303 		j += 3;
    304 	}
    305 }
    306 
    307 static int
    308 mgx_wait_engine(struct mgx_softc *sc)
    309 {
    310 	unsigned int i;
    311 	uint8_t stat;
    312 
    313 	for (i = 100000; i != 0; i--) {
    314 		stat = mgx_read_1(sc, ATR_BLT_STATUS);
    315 		if ((stat & (BLT_HOST_BUSY | BLT_ENGINE_BUSY)) == 0)
    316 			break;
    317 	}
    318 
    319 	return i;
    320 }
    321 
    322 static int
    323 mgx_wait_fifo(struct mgx_softc *sc, unsigned int nfifo)
    324 {
    325 	unsigned int i;
    326 	uint8_t stat;
    327 
    328 	for (i = 100000; i != 0; i--) {
    329 		stat = mgx_read_1(sc, ATR_FIFO_STATUS);
    330 		stat = (stat & FIFO_MASK) >> FIFO_SHIFT;
    331 		if (stat >= nfifo)
    332 			break;
    333 		mgx_write_1(sc, ATR_FIFO_STATUS, 0);
    334 	}
    335 
    336 	return i;
    337 }
    338 
    339 static void
    340 mgx_setup(struct mgx_softc *sc, int depth)
    341 {
    342 	/* wait for everything to go idle */
    343 	if (mgx_wait_engine(sc) == 0)
    344 		return;
    345 	if (mgx_wait_fifo(sc, FIFO_AT24) == 0)
    346 		return;
    347 	/*
    348 	 * Compute the invariant bits of the DEC register.
    349 	 */
    350 
    351 	switch (depth) {
    352 		case 8:
    353 			sc->sc_dec = DEC_DEPTH_8 << DEC_DEPTH_SHIFT;
    354 			break;
    355 		case 15:
    356 		case 16:
    357 			sc->sc_dec = DEC_DEPTH_16 << DEC_DEPTH_SHIFT;
    358 			break;
    359 		case 32:
    360 			sc->sc_dec = DEC_DEPTH_32 << DEC_DEPTH_SHIFT;
    361 			break;
    362 		default:
    363 			return; /* not supported */
    364 	}
    365 
    366 	switch (sc->sc_stride) {
    367 		case 640:
    368 			sc->sc_dec |= DEC_WIDTH_640 << DEC_WIDTH_SHIFT;
    369 			break;
    370 		case 800:
    371 			sc->sc_dec |= DEC_WIDTH_800 << DEC_WIDTH_SHIFT;
    372 			break;
    373 		case 1024:
    374 			sc->sc_dec |= DEC_WIDTH_1024 << DEC_WIDTH_SHIFT;
    375 			break;
    376 		case 1152:
    377 			sc->sc_dec |= DEC_WIDTH_1152 << DEC_WIDTH_SHIFT;
    378 			break;
    379 		case 1280:
    380 			sc->sc_dec |= DEC_WIDTH_1280 << DEC_WIDTH_SHIFT;
    381 			break;
    382 		case 1600:
    383 			sc->sc_dec |= DEC_WIDTH_1600 << DEC_WIDTH_SHIFT;
    384 			break;
    385 		default:
    386 			return; /* not supported */
    387 	}
    388 	mgx_write_1(sc, ATR_CLIP_CONTROL, 0);
    389 	mgx_write_1(sc, ATR_BYTEMASK, 0xff);
    390 }
    391 
    392 static void
    393 mgx_bitblt(void *cookie, int xs, int ys, int xd, int yd, int wi, int he,
    394              int rop)
    395 {
    396 	struct mgx_softc *sc = cookie;
    397 	uint32_t dec = sc->sc_dec;
    398 
    399         dec |= (DEC_COMMAND_BLT << DEC_COMMAND_SHIFT) |
    400 	       (DEC_START_DIMX << DEC_START_SHIFT);
    401 	if (xs < xd) {
    402 		xs += wi - 1;
    403 		xd += wi - 1;
    404 		dec |= DEC_DIR_X_REVERSE;
    405 	}
    406 	if (ys < yd) {
    407 		ys += he - 1;
    408 		yd += he - 1;
    409 		dec |= DEC_DIR_Y_REVERSE;
    410 	}
    411 	mgx_wait_fifo(sc, 5);
    412 	mgx_write_1(sc, ATR_ROP, rop);
    413 	mgx_write_4(sc, ATR_DEC, dec);
    414 	mgx_write_4(sc, ATR_SRC_XY, (ys << 16) | xs);
    415 	mgx_write_4(sc, ATR_DST_XY, (yd << 16) | xd);
    416 	mgx_write_4(sc, ATR_WH, (he << 16) | wi);
    417 }
    418 
    419 static void
    420 mgx_rectfill(void *cookie, int x, int y, int wi, int he, long fg)
    421 {
    422 	struct mgx_softc *sc = cookie;
    423 	struct vcons_screen *scr = sc->vd.active;
    424 	uint32_t dec = sc->sc_dec;
    425 	uint32_t col;
    426 
    427 	if (scr == NULL)
    428 		return;
    429 	col = scr->scr_ri.ri_devcmap[fg];
    430 
    431 	dec = sc->sc_dec;
    432 	dec |= (DEC_COMMAND_RECT << DEC_COMMAND_SHIFT) |
    433 	       (DEC_START_DIMX << DEC_START_SHIFT);
    434 	mgx_wait_fifo(sc, 5);
    435 	mgx_write_1(sc, ATR_ROP, ROP_SRC);
    436 	mgx_write_4(sc, ATR_FG, col);
    437 	mgx_write_4(sc, ATR_DEC, dec);
    438 	mgx_write_4(sc, ATR_DST_XY, (y << 16) | x);
    439 	mgx_write_4(sc, ATR_WH, (he << 16) | wi);
    440 }
    441 
    442 static void
    443 mgx_putchar(void *cookie, int row, int col, u_int c, long attr)
    444 {
    445 	struct rasops_info *ri = cookie;
    446 	struct wsdisplay_font *font = PICK_FONT(ri, c);
    447 	struct vcons_screen *scr = ri->ri_hw;
    448 	struct mgx_softc *sc = scr->scr_cookie;
    449 	uint32_t fg, bg;
    450 	int x, y, wi, he, rv;
    451 
    452 	wi = font->fontwidth;
    453 	he = font->fontheight;
    454 
    455 	bg = (attr >> 16) & 0xf;
    456 	fg = (attr >> 24) & 0xf;
    457 
    458 	x = ri->ri_xorigin + col * wi;
    459 	y = ri->ri_yorigin + row * he;
    460 
    461 	if (c == 0x20) {
    462 		mgx_rectfill(sc, x, y, wi, he, bg);
    463 		if (attr & 1)
    464 			mgx_rectfill(sc, x, y + he - 2, wi, 1, fg);
    465 		return;
    466 	}
    467 	rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
    468 	if (rv == GC_OK)
    469 		return;
    470 	mgx_wait_engine(sc);
    471 	sc->sc_putchar(cookie, row, col, c, attr & ~1);
    472 
    473 	if (rv == GC_ADD) {
    474 		glyphcache_add(&sc->sc_gc, c, x, y);
    475 	} else {
    476 		if (attr & 1)
    477 			mgx_rectfill(sc, x, y + he - 2, wi, 1, fg);
    478 	}
    479 }
    480 
    481 static void
    482 mgx_cursor(void *cookie, int on, int row, int col)
    483 {
    484 	struct rasops_info *ri = cookie;
    485 	struct vcons_screen *scr = ri->ri_hw;
    486 	struct mgx_softc *sc = scr->scr_cookie;
    487 	int x, y, wi,he;
    488 
    489 	wi = ri->ri_font->fontwidth;
    490 	he = ri->ri_font->fontheight;
    491 
    492 	if (ri->ri_flg & RI_CURSOR) {
    493 		x = ri->ri_ccol * wi + ri->ri_xorigin;
    494 		y = ri->ri_crow * he + ri->ri_yorigin;
    495 		mgx_bitblt(sc, x, y, x, y, wi, he, ROP_INV);
    496 		ri->ri_flg &= ~RI_CURSOR;
    497 	}
    498 
    499 	ri->ri_crow = row;
    500 	ri->ri_ccol = col;
    501 
    502 	if (on)
    503 	{
    504 		x = ri->ri_ccol * wi + ri->ri_xorigin;
    505 		y = ri->ri_crow * he + ri->ri_yorigin;
    506 		mgx_bitblt(sc, x, y, x, y, wi, he, ROP_INV);
    507 		ri->ri_flg |= RI_CURSOR;
    508 	}
    509 }
    510 
    511 static void
    512 mgx_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
    513 {
    514 	struct rasops_info *ri = cookie;
    515 	struct vcons_screen *scr = ri->ri_hw;
    516 	struct mgx_softc *sc = scr->scr_cookie;
    517 	int32_t xs, xd, y, width, height;
    518 
    519 	xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
    520 	xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
    521 	y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    522 	width = ri->ri_font->fontwidth * ncols;
    523 	height = ri->ri_font->fontheight;
    524 	mgx_bitblt(sc, xs, y, xd, y, width, height, ROP_SRC);
    525 }
    526 
    527 static void
    528 mgx_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
    529 {
    530 	struct rasops_info *ri = cookie;
    531 	struct vcons_screen *scr = ri->ri_hw;
    532 	struct mgx_softc *sc = scr->scr_cookie;
    533 	int32_t x, y, width, height, bg;
    534 
    535 	x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
    536 	y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    537 	width = ri->ri_font->fontwidth * ncols;
    538 	height = ri->ri_font->fontheight;
    539 	bg = (fillattr >> 16) & 0xff;
    540 	mgx_rectfill(sc, x, y, width, height, bg);
    541 }
    542 
    543 static void
    544 mgx_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
    545 {
    546 	struct rasops_info *ri = cookie;
    547 	struct vcons_screen *scr = ri->ri_hw;
    548 	struct mgx_softc *sc = scr->scr_cookie;
    549 	int32_t x, ys, yd, width, height;
    550 
    551 	x = ri->ri_xorigin;
    552 	ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
    553 	yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
    554 	width = ri->ri_emuwidth;
    555 	height = ri->ri_font->fontheight * nrows;
    556 	mgx_bitblt(sc, x, ys, x, yd, width, height, ROP_SRC);
    557 }
    558 
    559 static void
    560 mgx_eraserows(void *cookie, int row, int nrows, long fillattr)
    561 {
    562 	struct rasops_info *ri = cookie;
    563 	struct vcons_screen *scr = ri->ri_hw;
    564 	struct mgx_softc *sc = scr->scr_cookie;
    565 	int32_t x, y, width, height, bg;
    566 
    567 	if ((row == 0) && (nrows == ri->ri_rows)) {
    568 		x = y = 0;
    569 		width = ri->ri_width;
    570 		height = ri->ri_height;
    571 	} else {
    572 		x = ri->ri_xorigin;
    573 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    574 		width = ri->ri_emuwidth;
    575 		height = ri->ri_font->fontheight * nrows;
    576 	}
    577 	bg = (fillattr >> 16) & 0xff;
    578 	mgx_rectfill(sc, x, y, width, height, bg);
    579 }
    580 
    581 static void
    582 mgx_init_screen(void *cookie, struct vcons_screen *scr,
    583     int existing, long *defattr)
    584 {
    585 	struct mgx_softc *sc = cookie;
    586 	struct rasops_info *ri = &scr->scr_ri;
    587 
    588 	ri->ri_depth = 8;
    589 	ri->ri_width = sc->sc_width;
    590 	ri->ri_height = sc->sc_height;
    591 	ri->ri_stride = sc->sc_stride;
    592 	ri->ri_flg = RI_CENTER;
    593 
    594 #if _LP64
    595 	/*
    596 	 * XXX
    597 	 * Assuming all 64bit SPARCs are fast enough to render anti-aliased
    598 	 * text on the fly. Matters only as long as we don't have acceleration
    599 	 * and glyphcache.
    600 	 */
    601 	if (ri->ri_depth == 8)
    602 		ri->ri_flg |= RI_8BIT_IS_RGB | RI_ENABLE_ALPHA;
    603 #endif
    604 
    605 	ri->ri_bits = sc->sc_fbaddr;
    606 
    607 	rasops_init(ri, 0, 0);
    608 	sc->sc_putchar = ri->ri_ops.putchar;
    609 
    610 	ri->ri_caps = WSSCREEN_REVERSE | WSSCREEN_WSCOLORS;
    611 
    612 	rasops_reconfig(ri, ri->ri_height / ri->ri_font->fontheight,
    613 		    ri->ri_width / ri->ri_font->fontwidth);
    614 
    615 	ri->ri_hw = scr;
    616 	ri->ri_ops.putchar   = mgx_putchar;
    617 	ri->ri_ops.cursor    = mgx_cursor;
    618 	ri->ri_ops.copyrows  = mgx_copyrows;
    619 	ri->ri_ops.eraserows = mgx_eraserows;
    620 	ri->ri_ops.copycols  = mgx_copycols;
    621 	ri->ri_ops.erasecols = mgx_erasecols;
    622 }
    623 
    624 static int
    625 mgx_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    626     struct lwp *l)
    627 {
    628 	struct vcons_data *vd = v;
    629 	struct mgx_softc *sc = vd->cookie;
    630 	struct wsdisplay_fbinfo *wdf;
    631 	struct vcons_screen *ms = vd->active;
    632 
    633 	switch (cmd) {
    634 		case WSDISPLAYIO_GTYPE:
    635 			*(u_int *)data = WSDISPLAY_TYPE_MGX;
    636 			return 0;
    637 
    638 		case WSDISPLAYIO_GINFO:
    639 			wdf = (void *)data;
    640 			wdf->height = sc->sc_height;
    641 			wdf->width = sc->sc_width;
    642 			wdf->depth = 8;
    643 			wdf->cmsize = 256;
    644 			return 0;
    645 
    646 		case FBIOGVIDEO:
    647 		case WSDISPLAYIO_GVIDEO:
    648 			*(int *)data = 1;
    649 			return 0;
    650 
    651 		case WSDISPLAYIO_SVIDEO:
    652 		case FBIOSVIDEO:
    653 			return 0;
    654 
    655 		case WSDISPLAYIO_LINEBYTES:
    656 			{
    657 				int *ret = (int *)data;
    658 				*ret = sc->sc_stride;
    659 			}
    660 			return 0;
    661 
    662 		case WSDISPLAYIO_SMODE:
    663 			{
    664 				int new_mode = *(int*)data;
    665 				if (new_mode != sc->sc_mode)
    666 				{
    667 					sc->sc_mode = new_mode;
    668 					if (new_mode == WSDISPLAYIO_MODE_EMUL)
    669 					{
    670 						mgx_setup(sc, 8);
    671 						vcons_redraw_screen(ms);
    672 					} else {
    673 						mgx_setup(sc, 32);
    674 					}
    675 				}
    676 			}
    677 	}
    678 
    679 	return EPASSTHROUGH;
    680 }
    681 
    682 static paddr_t
    683 mgx_mmap(void *v, void *vs, off_t offset, int prot)
    684 {
    685 	struct vcons_data *vd = v;
    686 	struct mgx_softc *sc = vd->cookie;
    687 
    688 	/* regular fb mapping at 0 */
    689 	if ((offset >= 0) && (offset < 0x400000)) {
    690 		return bus_space_mmap(sc->sc_tag, sc->sc_paddr,
    691 		    offset, prot, BUS_SPACE_MAP_LINEAR);
    692 	}
    693 
    694 	return -1;
    695 }
    696