Home | History | Annotate | Line # | Download | only in sbus
mgx.c revision 1.12
      1 /*	$NetBSD: mgx.c,v 1.12 2017/08/04 23:54:46 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.12 2017/08/04 23:54:46 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 #include <sys/kauth.h>
     42 #include <sys/atomic.h>
     43 
     44 #include <sys/bus.h>
     45 #include <machine/autoconf.h>
     46 
     47 #include <dev/sbus/sbusvar.h>
     48 #include <dev/sun/fbio.h>
     49 #include <dev/sun/fbvar.h>
     50 
     51 #include <dev/wscons/wsdisplayvar.h>
     52 #include <dev/wscons/wsconsio.h>
     53 #include <dev/wsfont/wsfont.h>
     54 #include <dev/rasops/rasops.h>
     55 
     56 #include <dev/wscons/wsdisplay_vconsvar.h>
     57 #include <dev/wscons/wsdisplay_glyphcachevar.h>
     58 
     59 #include <dev/ic/vgareg.h>
     60 #include <dev/sbus/mgxreg.h>
     61 
     62 #include "ioconf.h"
     63 
     64 #include "opt_wsemul.h"
     65 #include "opt_mgx.h"
     66 
     67 struct mgx_softc {
     68 	device_t	sc_dev;
     69 	struct fbdevice	sc_fb;		/* frame buffer device */
     70 	bus_space_tag_t sc_tag;
     71 	bus_space_handle_t sc_blith;
     72 	bus_space_handle_t sc_vgah;
     73 	bus_addr_t	sc_paddr, sc_rpaddr;
     74 	void		*sc_fbaddr;
     75 	uint8_t		*sc_cursor;
     76 	int		sc_width;
     77 	int		sc_height;
     78 	int		sc_stride;
     79 	int		sc_depth;
     80 	int		sc_fbsize;
     81 	int		sc_mode;
     82 	char		sc_name[8];
     83 	uint32_t	sc_dec;
     84 	u_char		sc_cmap_red[256];
     85 	u_char		sc_cmap_green[256];
     86 	u_char		sc_cmap_blue[256];
     87 	int		sc_cursor_x, sc_cursor_y;
     88 	int		sc_hotspot_x, sc_hotspot_y;
     89 	int		sc_video, sc_buf;
     90 	void (*sc_putchar)(void *, int, int, u_int, long);
     91 	struct vcons_screen sc_console_screen;
     92 	struct wsscreen_descr sc_defaultscreen_descr;
     93 	const struct wsscreen_descr *sc_screens[1];
     94 	struct wsscreen_list sc_screenlist;
     95 	struct vcons_data vd;
     96 	glyphcache 	sc_gc;
     97 	uint8_t		sc_in[256];
     98 };
     99 
    100 static int	mgx_match(device_t, cfdata_t, void *);
    101 static void	mgx_attach(device_t, device_t, void *);
    102 static int	mgx_ioctl(void *, void *, u_long, void *, int,
    103 				 struct lwp*);
    104 static paddr_t	mgx_mmap(void *, void *, off_t, int);
    105 static void	mgx_init_screen(void *, struct vcons_screen *, int,
    106 				 long *);
    107 static void	mgx_write_dac(struct mgx_softc *, int, int, int, int);
    108 static void	mgx_setup(struct mgx_softc *, int);
    109 static void	mgx_init_palette(struct mgx_softc *);
    110 static int	mgx_putcmap(struct mgx_softc *, struct wsdisplay_cmap *);
    111 static int 	mgx_getcmap(struct mgx_softc *, struct wsdisplay_cmap *);
    112 static int	mgx_wait_engine(struct mgx_softc *);
    113 __unused static int	mgx_wait_host(struct mgx_softc *);
    114 /*static*/ int	mgx_wait_fifo(struct mgx_softc *, unsigned int);
    115 
    116 static void	mgx_bitblt(void *, int, int, int, int, int, int, int);
    117 static void 	mgx_rectfill(void *, int, int, int, int, long);
    118 
    119 static void	mgx_putchar_aa(void *, int, int, u_int, long);
    120 static void	mgx_putchar_mono(void *, int, int, u_int, long);
    121 static void	mgx_cursor(void *, int, int, int);
    122 static void	mgx_copycols(void *, int, int, int, int);
    123 static void	mgx_erasecols(void *, int, int, int, long);
    124 static void	mgx_copyrows(void *, int, int, int);
    125 static void	mgx_eraserows(void *, int, int, long);
    126 static void	mgx_adapt(struct vcons_screen *, void *);
    127 
    128 static int	mgx_do_cursor(struct mgx_softc *, struct wsdisplay_cursor *);
    129 static void	mgx_set_cursor(struct mgx_softc *);
    130 static void	mgx_set_video(struct mgx_softc *, int);
    131 
    132 CFATTACH_DECL_NEW(mgx, sizeof(struct mgx_softc),
    133     mgx_match, mgx_attach, NULL, NULL);
    134 
    135 struct wsdisplay_accessops mgx_accessops = {
    136 	mgx_ioctl,
    137 	mgx_mmap,
    138 	NULL,	/* vcons_alloc_screen */
    139 	NULL,	/* vcons_free_screen */
    140 	NULL,	/* vcons_show_screen */
    141 	NULL,	/* load_font */
    142 	NULL,	/* polls */
    143 	NULL,	/* scroll */
    144 };
    145 
    146 static void	mgx_unblank(device_t);
    147 
    148 dev_type_open(mgxopen);
    149 dev_type_close(mgxclose);
    150 dev_type_ioctl(mgxioctl);
    151 dev_type_mmap(mgxmmap);
    152 
    153 const struct cdevsw mgx_cdevsw = {
    154 	.d_open = mgxopen,
    155 	.d_close = mgxclose,
    156 	.d_read = noread,
    157 	.d_write = nowrite,
    158 	.d_ioctl = mgxioctl,
    159 	.d_stop = nostop,
    160 	.d_tty = notty,
    161 	.d_poll = nopoll,
    162 	.d_mmap = mgxmmap,
    163 	.d_kqfilter = nokqfilter,
    164 	.d_discard = nodiscard,
    165 	.d_flag = D_OTHER
    166 };
    167 
    168 /* frame buffer generic driver */
    169 static struct fbdriver mgx_fbdriver = {
    170 	mgx_unblank, mgxopen, mgxclose, mgxioctl, nopoll, mgxmmap,
    171 	nokqfilter
    172 };
    173 
    174 
    175 static inline void
    176 mgx_write_vga(struct mgx_softc *sc, uint32_t reg, uint8_t val)
    177 {
    178 	bus_space_write_1(sc->sc_tag, sc->sc_vgah, reg ^ 3, val);
    179 }
    180 
    181 static inline uint8_t
    182 mgx_read_vga(struct mgx_softc *sc, uint32_t reg)
    183 {
    184 	return bus_space_read_1(sc->sc_tag, sc->sc_vgah, reg ^ 3);
    185 }
    186 
    187 static inline void
    188 mgx_write_1(struct mgx_softc *sc, uint32_t reg, uint8_t val)
    189 {
    190 	bus_space_write_1(sc->sc_tag, sc->sc_blith, reg ^ 3, val);
    191 }
    192 
    193 static inline uint8_t
    194 mgx_read_1(struct mgx_softc *sc, uint32_t reg)
    195 {
    196 	return bus_space_read_1(sc->sc_tag, sc->sc_blith, reg ^ 3);
    197 }
    198 
    199 #if 0
    200 static inline uint32_t
    201 mgx_read_4(struct mgx_softc *sc, uint32_t reg)
    202 {
    203 	return bus_space_read_4(sc->sc_tag, sc->sc_blith, reg);
    204 }
    205 #endif
    206 
    207 static inline void
    208 mgx_write_2(struct mgx_softc *sc, uint32_t reg, uint16_t val)
    209 {
    210 	bus_space_write_2(sc->sc_tag, sc->sc_blith, reg ^ 2, val);
    211 }
    212 
    213 static inline void
    214 mgx_write_4(struct mgx_softc *sc, uint32_t reg, uint32_t val)
    215 {
    216 	bus_space_write_4(sc->sc_tag, sc->sc_blith, reg, val);
    217 }
    218 
    219 static int
    220 mgx_match(device_t parent, cfdata_t cf, void *aux)
    221 {
    222 	struct sbus_attach_args *sa = aux;
    223 
    224 	if (strcmp("SMSI,mgx", sa->sa_name) == 0)
    225 		return 100;
    226 	return 0;
    227 }
    228 
    229 /*
    230  * Attach a display.  We need to notice if it is the console, too.
    231  */
    232 static void
    233 mgx_attach(device_t parent, device_t self, void *args)
    234 {
    235 	struct mgx_softc *sc = device_private(self);
    236 	struct sbus_attach_args *sa = args;
    237 	struct wsemuldisplaydev_attach_args aa;
    238 	struct fbdevice *fb = &sc->sc_fb;
    239 	struct rasops_info *ri;
    240 	unsigned long defattr;
    241 	bus_space_handle_t bh;
    242 	int node = sa->sa_node, bsize;
    243 	int isconsole;
    244 
    245 	aprint_normal("\n");
    246 	sc->sc_dev = self;
    247 	sc->sc_tag = sa->sa_bustag;
    248 
    249 	sc->sc_paddr = sbus_bus_addr(sa->sa_bustag, sa->sa_slot,
    250 	    sa->sa_reg[8].oa_base);
    251 	sc->sc_rpaddr = sbus_bus_addr(sa->sa_bustag, sa->sa_slot,
    252 	    sa->sa_reg[5].oa_base + MGX_REG_ATREG_OFFSET);
    253 
    254 	/* read geometry information from the device tree */
    255 	sc->sc_width = prom_getpropint(sa->sa_node, "width", 1152);
    256 	sc->sc_height = prom_getpropint(sa->sa_node, "height", 900);
    257 	sc->sc_stride = prom_getpropint(sa->sa_node, "linebytes", 1152);
    258 	sc->sc_fbsize = prom_getpropint(sa->sa_node, "fb_size", 0x00400000);
    259 	sc->sc_fbaddr = NULL;
    260 	if (sc->sc_fbaddr == NULL) {
    261 		if (sbus_bus_map(sa->sa_bustag,
    262 			 sa->sa_slot,
    263 			 sa->sa_reg[8].oa_base,
    264 			 sc->sc_fbsize,
    265 			 BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_LARGE,
    266 			 &bh) != 0) {
    267 			aprint_error_dev(self, "couldn't map framebuffer\n");
    268 			return;
    269 		}
    270 		sc->sc_fbaddr = bus_space_vaddr(sa->sa_bustag, bh);
    271 	}
    272 
    273 	if (sbus_bus_map(sa->sa_bustag,
    274 			 sa->sa_slot,
    275 			 sa->sa_reg[4].oa_base, 0x1000, 0,
    276 			 &sc->sc_vgah) != 0) {
    277 		aprint_error("%s: couldn't map VGA registers\n",
    278 		    device_xname(sc->sc_dev));
    279 		return;
    280 	}
    281 
    282 	if (sbus_bus_map(sa->sa_bustag,
    283 			 sa->sa_slot,
    284 			 sa->sa_reg[5].oa_base + MGX_REG_ATREG_OFFSET, 0x1000,
    285 			 0, &sc->sc_blith) != 0) {
    286 		aprint_error("%s: couldn't map blitter registers\n",
    287 		    device_xname(sc->sc_dev));
    288 		return;
    289 	}
    290 
    291 	mgx_setup(sc, MGX_DEPTH);
    292 
    293 	aprint_normal_dev(self, "[%s] %d MB framebuffer, %d x %d\n",
    294 		sc->sc_name, sc->sc_fbsize >> 20, sc->sc_width, sc->sc_height);
    295 
    296 
    297 	sc->sc_defaultscreen_descr = (struct wsscreen_descr) {
    298 		"default",
    299 		0, 0,
    300 		NULL,
    301 		8, 16,
    302 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE |
    303 		WSSCREEN_RESIZE,
    304 		NULL
    305 	};
    306 
    307 	sc->sc_cursor_x = 0;
    308 	sc->sc_cursor_y = 0;
    309 	sc->sc_hotspot_x = 0;
    310 	sc->sc_hotspot_y = 0;
    311 	sc->sc_video = WSDISPLAYIO_VIDEO_ON;
    312 
    313 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
    314 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
    315 
    316 	isconsole = fb_is_console(node);
    317 
    318 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    319 	wsfont_init();
    320 
    321 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr, &mgx_accessops);
    322 	sc->vd.init_screen = mgx_init_screen;
    323 	sc->vd.show_screen_cookie = sc;
    324 	sc->vd.show_screen_cb = mgx_adapt;
    325 
    326 	vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1, &defattr);
    327 	sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    328 
    329 	ri = &sc->sc_console_screen.scr_ri;
    330 
    331 	sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    332 	sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    333 	sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    334 	sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    335 
    336 	sc->sc_gc.gc_bitblt = mgx_bitblt;
    337 	sc->sc_gc.gc_rectfill = mgx_rectfill;
    338 	sc->sc_gc.gc_blitcookie = sc;
    339 	sc->sc_gc.gc_rop = ROP_SRC;
    340 
    341 	/*
    342 	 * leave some room between visible screen and glyph cache for upload
    343 	 * buffers used by putchar_mono()
    344 	 */
    345 	bsize = (32 * 1024 * sc->sc_stride - 1) / sc->sc_stride;
    346 	glyphcache_init(&sc->sc_gc,
    347 	    sc->sc_height + bsize,
    348 	    (0x400000 / sc->sc_stride) - (sc->sc_height + bsize),
    349 	    sc->sc_width,
    350 	    ri->ri_font->fontwidth,
    351 	    ri->ri_font->fontheight,
    352 	    defattr);
    353 
    354 	mgx_init_palette(sc);
    355 
    356 	if(isconsole) {
    357 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
    358 		    defattr);
    359 		vcons_replay_msgbuf(&sc->sc_console_screen);
    360 	}
    361 
    362 	aa.console = isconsole;
    363 	aa.scrdata = &sc->sc_screenlist;
    364 	aa.accessops = &mgx_accessops;
    365 	aa.accesscookie = &sc->vd;
    366 
    367 	config_found(self, &aa, wsemuldisplaydevprint);
    368 
    369 	/* now the Sun fb goop */
    370 	fb->fb_driver = &mgx_fbdriver;
    371 	fb->fb_device = sc->sc_dev;
    372 	fb->fb_flags = device_cfdata(sc->sc_dev)->cf_flags & FB_USERMASK;
    373 	fb->fb_type.fb_type = FBTYPE_MGX;
    374 	fb->fb_pixels = NULL;
    375 
    376 	fb->fb_type.fb_depth = 32;
    377 	fb->fb_type.fb_width = sc->sc_width;
    378 	fb->fb_type.fb_height = sc->sc_height;
    379 	fb->fb_linebytes = sc->sc_stride * 4;
    380 
    381 	fb->fb_type.fb_cmsize = 256;
    382 	fb->fb_type.fb_size = sc->sc_fbsize;
    383 	fb_attach(&sc->sc_fb, isconsole);
    384 }
    385 
    386 static void
    387 mgx_write_dac(struct mgx_softc *sc, int idx, int r, int g, int b)
    388 {
    389 	mgx_write_vga(sc, VGA_BASE + VGA_DAC_ADDRW, idx);
    390 	mgx_write_vga(sc, VGA_BASE + VGA_DAC_PALETTE, r);
    391 	mgx_write_vga(sc, VGA_BASE + VGA_DAC_PALETTE, g);
    392 	mgx_write_vga(sc, VGA_BASE + VGA_DAC_PALETTE, b);
    393 }
    394 
    395 static void
    396 mgx_init_palette(struct mgx_softc *sc)
    397 {
    398 	struct rasops_info *ri = &sc->sc_console_screen.scr_ri;
    399 	int i, j = 0;
    400 	uint8_t cmap[768];
    401 
    402 	if (sc->sc_depth == 8) {
    403 		rasops_get_cmap(ri, cmap, sizeof(cmap));
    404 		for (i = 0; i < 256; i++) {
    405 			sc->sc_cmap_red[i] = cmap[j];
    406 			sc->sc_cmap_green[i] = cmap[j + 1];
    407 			sc->sc_cmap_blue[i] = cmap[j + 2];
    408 			mgx_write_dac(sc, i, cmap[j], cmap[j + 1], cmap[j + 2]);
    409 			j += 3;
    410 		}
    411 	} else {
    412 		/* linear ramp for true colour modes */
    413 		for (i = 0; i < 256; i++) {
    414 			mgx_write_dac(sc, i, i, i, i);
    415 		}
    416 	}
    417 }
    418 
    419 static int
    420 mgx_putcmap(struct mgx_softc *sc, struct wsdisplay_cmap *cm)
    421 {
    422 	u_char *r, *g, *b;
    423 	u_int index = cm->index;
    424 	u_int count = cm->count;
    425 	int i, error;
    426 	u_char rbuf[256], gbuf[256], bbuf[256];
    427 
    428 	if (cm->index >= 256 || cm->count > 256 ||
    429 	    (cm->index + cm->count) > 256)
    430 		return EINVAL;
    431 	error = copyin(cm->red, &rbuf[index], count);
    432 	if (error)
    433 		return error;
    434 	error = copyin(cm->green, &gbuf[index], count);
    435 	if (error)
    436 		return error;
    437 	error = copyin(cm->blue, &bbuf[index], count);
    438 	if (error)
    439 		return error;
    440 
    441 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    442 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    443 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    444 
    445 	r = &sc->sc_cmap_red[index];
    446 	g = &sc->sc_cmap_green[index];
    447 	b = &sc->sc_cmap_blue[index];
    448 
    449 	for (i = 0; i < count; i++) {
    450 		mgx_write_dac(sc, index, *r, *g, *b);
    451 		index++;
    452 		r++, g++, b++;
    453 	}
    454 	return 0;
    455 }
    456 
    457 static int
    458 mgx_getcmap(struct mgx_softc *sc, struct wsdisplay_cmap *cm)
    459 {
    460 	u_int index = cm->index;
    461 	u_int count = cm->count;
    462 	int error;
    463 
    464 	if (index >= 255 || count > 256 || index + count > 256)
    465 		return EINVAL;
    466 
    467 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    468 	if (error)
    469 		return error;
    470 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    471 	if (error)
    472 		return error;
    473 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    474 	if (error)
    475 		return error;
    476 
    477 	return 0;
    478 }
    479 
    480 static int
    481 mgx_wait_engine(struct mgx_softc *sc)
    482 {
    483 	unsigned int i;
    484 	uint8_t stat;
    485 
    486 	for (i = 100000; i != 0; i--) {
    487 		stat = mgx_read_1(sc, ATR_BLT_STATUS);
    488 		if ((stat & (BLT_HOST_BUSY | BLT_ENGINE_BUSY)) == 0)
    489 			break;
    490 	}
    491 
    492 	return i;
    493 }
    494 
    495 static inline int
    496 mgx_wait_host(struct mgx_softc *sc)
    497 {
    498 	unsigned int i;
    499 	uint8_t stat;
    500 
    501 	for (i = 10000; i != 0; i--) {
    502 		stat = mgx_read_1(sc, ATR_BLT_STATUS);
    503 		if ((stat & BLT_HOST_BUSY) == 0)
    504 			break;
    505 	}
    506 
    507 	return i;
    508 }
    509 
    510 /*static inline*/ int
    511 mgx_wait_fifo(struct mgx_softc *sc, unsigned int nfifo)
    512 {
    513 	unsigned int i;
    514 	uint8_t stat;
    515 
    516 	for (i = 100000; i != 0; i--) {
    517 		stat = mgx_read_1(sc, ATR_FIFO_STATUS);
    518 		stat = (stat & FIFO_MASK) >> FIFO_SHIFT;
    519 		if (stat >= nfifo)
    520 			break;
    521 		mgx_write_1(sc, ATR_FIFO_STATUS, 0);
    522 	}
    523 
    524 	return i;
    525 }
    526 
    527 static void
    528 mgx_setup(struct mgx_softc *sc, int depth)
    529 {
    530 	uint32_t stride;
    531 	int i;
    532 	uint8_t reg;
    533 
    534 	/* wait for everything to go idle */
    535 	if (mgx_wait_engine(sc) == 0)
    536 		return;
    537 	if (mgx_wait_fifo(sc, FIFO_AT24) == 0)
    538 		return;
    539 
    540 	sc->sc_buf = 0;
    541 	/* read name from sequencer */
    542 	for (i = 0; i < 8; i++) {
    543 		mgx_write_vga(sc, SEQ_INDEX, i + 0x11);
    544 		sc->sc_name[i] = mgx_read_vga(sc, SEQ_DATA);
    545 	}
    546 	sc->sc_name[7] = 0;
    547 
    548 	reg = mgx_read_1(sc, ATR_PIXEL);
    549 	reg &= ~PIXEL_DEPTH_MASK;
    550 
    551 	switch (depth) {
    552 		case 8:
    553 			sc->sc_dec = DEC_DEPTH_8 << DEC_DEPTH_SHIFT;
    554 			reg |= PIXEL_8;
    555 			break;
    556 		case 15:
    557 			sc->sc_dec = DEC_DEPTH_16 << DEC_DEPTH_SHIFT;
    558 			reg |= PIXEL_15;
    559 			break;
    560 		case 16:
    561 			sc->sc_dec = DEC_DEPTH_16 << DEC_DEPTH_SHIFT;
    562 			reg |= PIXEL_16;
    563 			break;
    564 		case 32:
    565 			sc->sc_dec = DEC_DEPTH_32 << DEC_DEPTH_SHIFT;
    566 			reg |= PIXEL_32;
    567 			break;
    568 		default:
    569 			return; /* not supported */
    570 	}
    571 
    572 	/* the chip wants stride in units of 8 bytes */
    573 	sc->sc_stride = sc->sc_width * (depth >> 3);
    574 	stride = sc->sc_stride >> 3;
    575 #ifdef MGX_DEBUG
    576 	sc->sc_height -= 150;
    577 #endif
    578 
    579 	sc->sc_depth = depth;
    580 
    581 	switch (sc->sc_width) {
    582 		case 640:
    583 			sc->sc_dec |= DEC_WIDTH_640 << DEC_WIDTH_SHIFT;
    584 			break;
    585 		case 800:
    586 			sc->sc_dec |= DEC_WIDTH_800 << DEC_WIDTH_SHIFT;
    587 			break;
    588 		case 1024:
    589 			sc->sc_dec |= DEC_WIDTH_1024 << DEC_WIDTH_SHIFT;
    590 			break;
    591 		case 1152:
    592 			sc->sc_dec |= DEC_WIDTH_1152 << DEC_WIDTH_SHIFT;
    593 			break;
    594 		case 1280:
    595 			sc->sc_dec |= DEC_WIDTH_1280 << DEC_WIDTH_SHIFT;
    596 			break;
    597 		case 1600:
    598 			sc->sc_dec |= DEC_WIDTH_1600 << DEC_WIDTH_SHIFT;
    599 			break;
    600 		default:
    601 			return; /* not supported */
    602 	}
    603 	mgx_wait_fifo(sc, 4);
    604 	mgx_write_1(sc, ATR_CLIP_CONTROL, 0);
    605 	mgx_write_1(sc, ATR_BYTEMASK, 0xff);
    606 	mgx_write_1(sc, ATR_PIXEL, reg);
    607 	mgx_write_4(sc, ATR_OFFSET, 0);
    608 	mgx_wait_fifo(sc, 4);
    609 	mgx_write_vga(sc, CRTC_INDEX, 0x13);
    610 	mgx_write_vga(sc, CRTC_DATA, stride & 0xff);
    611 	mgx_write_vga(sc, CRTC_INDEX, 0x1c);
    612 	mgx_write_vga(sc, CRTC_DATA, (stride & 0xf00) >> 4);
    613 
    614 	/* clean up the screen if we're switching to != 8bit */
    615 	if (depth != MGX_DEPTH)
    616 		mgx_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height, 0);
    617 
    618 	mgx_wait_fifo(sc, 4);
    619 	/* initialize hardware cursor stuff */
    620 	mgx_write_2(sc, ATR_CURSOR_ADDRESS, (sc->sc_fbsize - 1024) >> 10);
    621 	mgx_write_1(sc, ATR_CURSOR_ENABLE, 0);
    622 	sc->sc_cursor = (uint8_t *)sc->sc_fbaddr + sc->sc_fbsize - 1024;
    623 	memset(sc->sc_cursor, 0xf0, 1024);
    624 	memset(sc->sc_in, 0, sizeof(sc->sc_in));
    625 }
    626 
    627 static void
    628 mgx_bitblt(void *cookie, int xs, int ys, int xd, int yd, int wi, int he,
    629              int rop)
    630 {
    631 	struct mgx_softc *sc = cookie;
    632 	uint32_t dec = sc->sc_dec;
    633 
    634         dec |= (DEC_COMMAND_BLT << DEC_COMMAND_SHIFT) |
    635 	       (DEC_START_DIMX << DEC_START_SHIFT);
    636 	if (xs < xd) {
    637 		xs += wi - 1;
    638 		xd += wi - 1;
    639 		dec |= DEC_DIR_X_REVERSE;
    640 	}
    641 	if (ys < yd) {
    642 		ys += he - 1;
    643 		yd += he - 1;
    644 		dec |= DEC_DIR_Y_REVERSE;
    645 	}
    646 	mgx_wait_fifo(sc, 5);
    647 	mgx_write_1(sc, ATR_ROP, rop);
    648 	mgx_write_4(sc, ATR_DEC, dec);
    649 	mgx_write_4(sc, ATR_SRC_XY, (ys << 16) | xs);
    650 	mgx_write_4(sc, ATR_DST_XY, (yd << 16) | xd);
    651 	mgx_write_4(sc, ATR_WH, (he << 16) | wi);
    652 }
    653 
    654 static void
    655 mgx_rectfill(void *cookie, int x, int y, int wi, int he, long fg)
    656 {
    657 	struct mgx_softc *sc = cookie;
    658 	struct vcons_screen *scr = sc->vd.active;
    659 	uint32_t dec = sc->sc_dec;
    660 	uint32_t col;
    661 
    662 	if (scr == NULL)
    663 		return;
    664 	col = scr->scr_ri.ri_devcmap[fg];
    665 
    666 	dec = sc->sc_dec;
    667 	dec |= (DEC_COMMAND_RECT << DEC_COMMAND_SHIFT) |
    668 	       (DEC_START_DIMX << DEC_START_SHIFT);
    669 	mgx_wait_fifo(sc, 5);
    670 	mgx_write_1(sc, ATR_ROP, ROP_SRC);
    671 	mgx_write_4(sc, ATR_FG, col);
    672 	mgx_write_4(sc, ATR_DEC, dec);
    673 	mgx_write_4(sc, ATR_DST_XY, (y << 16) | x);
    674 	mgx_write_4(sc, ATR_WH, (he << 16) | wi);
    675 }
    676 
    677 static void
    678 mgx_putchar_aa(void *cookie, int row, int col, u_int c, long attr)
    679 {
    680 	struct rasops_info *ri = cookie;
    681 	struct wsdisplay_font *font = PICK_FONT(ri, c);
    682 	struct vcons_screen *scr = ri->ri_hw;
    683 	struct mgx_softc *sc = scr->scr_cookie;
    684 	uint32_t fg, bg;
    685 	int x, y, wi, he, rv;
    686 
    687 	wi = font->fontwidth;
    688 	he = font->fontheight;
    689 
    690 	bg = (attr >> 16) & 0xf;
    691 	fg = (attr >> 24) & 0xf;
    692 
    693 	x = ri->ri_xorigin + col * wi;
    694 	y = ri->ri_yorigin + row * he;
    695 
    696 	if (c == 0x20) {
    697 		mgx_rectfill(sc, x, y, wi, he, bg);
    698 		if (attr & 1)
    699 			mgx_rectfill(sc, x, y + he - 2, wi, 1, fg);
    700 		return;
    701 	}
    702 
    703 	rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
    704 	if (rv != GC_OK) {
    705 		volatile uint32_t junk;
    706 
    707 		mgx_wait_engine(sc);
    708 		sc->sc_putchar(cookie, row, col, c, attr & ~1);
    709 		if (rv == GC_ADD) {
    710 			/*
    711 			 * try to make sure the glyph made it all the way to
    712 			 * video memory before trying to blit it into the cache
    713 			 */
    714 			junk = *(uint32_t *)sc->sc_fbaddr;
    715 			__USE(junk);
    716 			glyphcache_add(&sc->sc_gc, c, x, y);
    717 		}
    718 	}
    719 	if (attr & 1)
    720 		mgx_rectfill(sc, x, y + he - 2, wi, 1, fg);
    721 }
    722 
    723 static void
    724 mgx_putchar_mono(void *cookie, int row, int col, u_int c, long attr)
    725 {
    726 	struct rasops_info *ri = cookie;
    727 	struct wsdisplay_font *font = PICK_FONT(ri, c);
    728 	struct vcons_screen *scr = ri->ri_hw;
    729 	struct mgx_softc *sc = scr->scr_cookie;
    730 	uint8_t *s, *d;
    731 	uint32_t fg, bg, scratch = ((sc->sc_stride * sc->sc_height) + 7) & ~7;
    732 	int x, y, wi, he, len, i;
    733 
    734 	wi = font->fontwidth;
    735 	he = font->fontheight;
    736 
    737 	bg = (attr >> 16) & 0xf;
    738 	fg = (attr >> 24) & 0xf;
    739 
    740 	x = ri->ri_xorigin + col * wi;
    741 	y = ri->ri_yorigin + row * he;
    742 
    743 	if (!CHAR_IN_FONT(c, font)) {
    744 		c = 0x20;
    745 #ifdef MGX_DEBUG
    746 		bg = WSCOL_LIGHT_BLUE;
    747 #endif
    748 	}
    749 	if (c == 0x20) {
    750 		mgx_rectfill(sc, x, y, wi, he, bg);
    751 		if (attr & 1)
    752 			mgx_rectfill(sc, x, y + he - 2, wi, 1, fg);
    753 		return;
    754 	}
    755 
    756 	mgx_wait_fifo(sc, 3);
    757 	mgx_write_4(sc, ATR_FG, ri->ri_devcmap[fg]);
    758 	mgx_write_4(sc, ATR_BG, ri->ri_devcmap[bg]);
    759 	mgx_write_1(sc, ATR_ROP, ROP_SRC);
    760 
    761 	/*
    762 	 * do hardware colour expansion
    763 	 * there has to be an upload port somewhere, since there are host blit
    764 	 * commands, but it's not used or mentioned in the xf86-video-apm driver
    765 	 * so for now we use the vram-to-vram blits to draw characters.
    766 	 * stash most of the font in vram for speed, also:
    767 	 * - the sbus-pci bridge doesn't seem to support 64bit accesses,
    768 	 *   they will cause occasional data corruption and all sorts of weird
    769 	 *   side effects, so copy font bitmaps byte-wise
    770 	 * - at least it doesn't seem to need any kind of buffer flushing
    771 	 * - still use rotation buffers for characters that don't fall into the
    772 	 *   8 bit range
    773 	 */
    774 
    775 	len = (ri->ri_fontscale + 7) & ~7;
    776 	s = WSFONT_GLYPH(c, font);
    777 	if ((c > 32) && (c < 256)) {
    778 		scratch += len * c;
    779 		if (sc->sc_in[c] == 0) {
    780 			d = (uint8_t *)sc->sc_fbaddr + scratch;
    781 			for (i = 0; i < ri->ri_fontscale; i++)
    782 				d[i] = s[i];
    783 			sc->sc_in[c] = 1;
    784 		}
    785 	} else {
    786 		sc->sc_buf = (sc->sc_buf + 1) & 7; /* rotate through 8 buffers */
    787 		scratch += sc->sc_buf * len;
    788 		d = (uint8_t *)sc->sc_fbaddr + scratch;
    789 		for (i = 0; i < ri->ri_fontscale; i++)
    790 			d[i] = s[i];
    791 	}
    792 	mgx_wait_fifo(sc, 5);
    793 	mgx_write_4(sc, ATR_DEC, sc->sc_dec | (DEC_COMMAND_BLT << DEC_COMMAND_SHIFT) |
    794 	       (DEC_START_DIMX << DEC_START_SHIFT) |
    795 	       DEC_SRC_LINEAR | DEC_SRC_CONTIGUOUS | DEC_MONOCHROME);
    796 	mgx_write_4(sc, ATR_SRC_XY, ((scratch & 0xfff000) << 4) | (scratch & 0xfff));
    797 	mgx_write_4(sc, ATR_DST_XY, (y << 16) | x);
    798 	mgx_write_4(sc, ATR_WH, (he << 16) | wi);
    799 
    800 	if (attr & 1)
    801 		mgx_rectfill(sc, x, y + he - 2, wi, 1, fg);
    802 }
    803 
    804 static void
    805 mgx_cursor(void *cookie, int on, int row, int col)
    806 {
    807 	struct rasops_info *ri = cookie;
    808 	struct vcons_screen *scr = ri->ri_hw;
    809 	struct mgx_softc *sc = scr->scr_cookie;
    810 	int x, y, wi,he;
    811 
    812 	wi = ri->ri_font->fontwidth;
    813 	he = ri->ri_font->fontheight;
    814 
    815 	if (ri->ri_flg & RI_CURSOR) {
    816 		x = ri->ri_ccol * wi + ri->ri_xorigin;
    817 		y = ri->ri_crow * he + ri->ri_yorigin;
    818 		mgx_bitblt(sc, x, y, x, y, wi, he, ROP_INV);
    819 		ri->ri_flg &= ~RI_CURSOR;
    820 	}
    821 
    822 	ri->ri_crow = row;
    823 	ri->ri_ccol = col;
    824 
    825 	if (on)
    826 	{
    827 		x = ri->ri_ccol * wi + ri->ri_xorigin;
    828 		y = ri->ri_crow * he + ri->ri_yorigin;
    829 		mgx_bitblt(sc, x, y, x, y, wi, he, ROP_INV);
    830 		ri->ri_flg |= RI_CURSOR;
    831 	}
    832 }
    833 
    834 static void
    835 mgx_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
    836 {
    837 	struct rasops_info *ri = cookie;
    838 	struct vcons_screen *scr = ri->ri_hw;
    839 	struct mgx_softc *sc = scr->scr_cookie;
    840 	int32_t xs, xd, y, width, height;
    841 
    842 	xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
    843 	xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
    844 	y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    845 	width = ri->ri_font->fontwidth * ncols;
    846 	height = ri->ri_font->fontheight;
    847 	mgx_bitblt(sc, xs, y, xd, y, width, height, ROP_SRC);
    848 }
    849 
    850 static void
    851 mgx_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
    852 {
    853 	struct rasops_info *ri = cookie;
    854 	struct vcons_screen *scr = ri->ri_hw;
    855 	struct mgx_softc *sc = scr->scr_cookie;
    856 	int32_t x, y, width, height, bg;
    857 
    858 	x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
    859 	y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    860 	width = ri->ri_font->fontwidth * ncols;
    861 	height = ri->ri_font->fontheight;
    862 	bg = (fillattr >> 16) & 0xff;
    863 	mgx_rectfill(sc, x, y, width, height, bg);
    864 }
    865 
    866 static void
    867 mgx_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
    868 {
    869 	struct rasops_info *ri = cookie;
    870 	struct vcons_screen *scr = ri->ri_hw;
    871 	struct mgx_softc *sc = scr->scr_cookie;
    872 	int32_t x, ys, yd, width, height;
    873 
    874 	x = ri->ri_xorigin;
    875 	ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
    876 	yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
    877 	width = ri->ri_emuwidth;
    878 	height = ri->ri_font->fontheight * nrows;
    879 	mgx_bitblt(sc, x, ys, x, yd, width, height, ROP_SRC);
    880 }
    881 
    882 static void
    883 mgx_eraserows(void *cookie, int row, int nrows, long fillattr)
    884 {
    885 	struct rasops_info *ri = cookie;
    886 	struct vcons_screen *scr = ri->ri_hw;
    887 	struct mgx_softc *sc = scr->scr_cookie;
    888 	int32_t x, y, width, height, bg;
    889 
    890 	if ((row == 0) && (nrows == ri->ri_rows)) {
    891 		x = y = 0;
    892 		width = ri->ri_width;
    893 		height = ri->ri_height;
    894 	} else {
    895 		x = ri->ri_xorigin;
    896 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    897 		width = ri->ri_emuwidth;
    898 		height = ri->ri_font->fontheight * nrows;
    899 	}
    900 	bg = (fillattr >> 16) & 0xff;
    901 	mgx_rectfill(sc, x, y, width, height, bg);
    902 }
    903 
    904 static void
    905 mgx_adapt(struct vcons_screen *scr, void *cookie)
    906 {
    907 	struct mgx_softc *sc = cookie;
    908 	memset(sc->sc_in, 0, sizeof(sc->sc_in));
    909 	glyphcache_adapt(scr, &sc->sc_gc);
    910 }
    911 
    912 static void
    913 mgx_init_screen(void *cookie, struct vcons_screen *scr,
    914     int existing, long *defattr)
    915 {
    916 	struct mgx_softc *sc = cookie;
    917 	struct rasops_info *ri = &scr->scr_ri;
    918 
    919 	ri->ri_depth = sc->sc_depth;
    920 	ri->ri_width = sc->sc_width;
    921 	ri->ri_height = sc->sc_height;
    922 	ri->ri_stride = sc->sc_stride;
    923 	ri->ri_flg = RI_CENTER | RI_ENABLE_ALPHA;
    924 
    925 	if (ri->ri_depth == 8)
    926 		ri->ri_flg |= RI_8BIT_IS_RGB;
    927 
    928 #ifdef MGX_NOACCEL
    929 	scr->scr_flags |= VCONS_DONT_READ;
    930 #endif
    931 	scr->scr_flags |= VCONS_LOADFONT;
    932 
    933 	ri->ri_rnum = 8;
    934 	ri->ri_rpos = 0;
    935 	ri->ri_gnum = 8;
    936 	ri->ri_gpos = 8;
    937 	ri->ri_bnum = 8;
    938 	ri->ri_bpos = 16;
    939 
    940 	ri->ri_bits = sc->sc_fbaddr;
    941 
    942 	rasops_init(ri, 0, 0);
    943 
    944 	ri->ri_caps = WSSCREEN_REVERSE | WSSCREEN_WSCOLORS | WSSCREEN_UNDERLINE;
    945 
    946 	rasops_reconfig(ri, ri->ri_height / ri->ri_font->fontheight,
    947 		    ri->ri_width / ri->ri_font->fontwidth);
    948 
    949 	ri->ri_hw = scr;
    950 
    951 #ifdef MGX_NOACCEL
    952 if (0)
    953 #endif
    954 	{
    955 		if (FONT_IS_ALPHA(ri->ri_font)) {
    956 			sc->sc_putchar = ri->ri_ops.putchar;
    957 			ri->ri_ops.putchar   = mgx_putchar_aa;
    958 		} else {
    959 			ri->ri_ops.putchar   = mgx_putchar_mono;
    960 		}
    961 		ri->ri_ops.cursor    = mgx_cursor;
    962 		ri->ri_ops.copyrows  = mgx_copyrows;
    963 		ri->ri_ops.eraserows = mgx_eraserows;
    964 		ri->ri_ops.copycols  = mgx_copycols;
    965 		ri->ri_ops.erasecols = mgx_erasecols;
    966 	}
    967 }
    968 
    969 static int
    970 mgx_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    971     struct lwp *l)
    972 {
    973 	struct vcons_data *vd = v;
    974 	struct mgx_softc *sc = vd->cookie;
    975 	struct wsdisplay_fbinfo *wdf;
    976 	struct vcons_screen *ms = vd->active;
    977 
    978 	switch (cmd) {
    979 		case WSDISPLAYIO_GTYPE:
    980 			*(u_int *)data = WSDISPLAY_TYPE_MGX;
    981 			return 0;
    982 
    983 		case WSDISPLAYIO_GINFO:
    984 			wdf = (void *)data;
    985 			wdf->height = sc->sc_height;
    986 			wdf->width = sc->sc_width;
    987 			wdf->depth = 8;
    988 			wdf->cmsize = 256;
    989 			return 0;
    990 
    991 	case FBIOGTYPE:
    992 		*(struct fbtype *)data = sc->sc_fb.fb_type;
    993 		break;
    994 
    995 	case FBIOGATTR:
    996 #define fba ((struct fbgattr *)data)
    997 		fba->real_type = sc->sc_fb.fb_type.fb_type;
    998 		fba->owner = 0;		/* XXX ??? */
    999 		fba->fbtype = sc->sc_fb.fb_type;
   1000 		fba->sattr.flags = 0;
   1001 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
   1002 		fba->sattr.dev_specific[0] = -1;
   1003 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
   1004 		fba->emu_types[1] = -1;
   1005 #undef fba
   1006 		break;
   1007 		case FBIOGVIDEO:
   1008 		case WSDISPLAYIO_GVIDEO:
   1009 			*(int *)data = sc->sc_video;
   1010 			return 0;
   1011 
   1012 		case WSDISPLAYIO_SVIDEO:
   1013 		case FBIOSVIDEO:
   1014 			mgx_set_video(sc, *(int *)data);
   1015 			return 0;
   1016 
   1017 		case WSDISPLAYIO_LINEBYTES:
   1018 			{
   1019 				int *ret = (int *)data;
   1020 				*ret = sc->sc_stride;
   1021 			}
   1022 			return 0;
   1023 
   1024 		case WSDISPLAYIO_SMODE:
   1025 			{
   1026 				int new_mode = *(int*)data;
   1027 				if (new_mode != sc->sc_mode)
   1028 				{
   1029 					sc->sc_mode = new_mode;
   1030 					if (new_mode == WSDISPLAYIO_MODE_EMUL)
   1031 					{
   1032 						mgx_setup(sc, MGX_DEPTH);
   1033 						glyphcache_wipe(&sc->sc_gc);
   1034 						mgx_init_palette(sc);
   1035 						vcons_redraw_screen(ms);
   1036 					} else {
   1037 						mgx_setup(sc, 32);
   1038 						mgx_init_palette(sc);
   1039 					}
   1040 				}
   1041 			}
   1042 			return 0;
   1043 
   1044 		case WSDISPLAYIO_GETCMAP:
   1045 			return mgx_getcmap(sc, (struct wsdisplay_cmap *)data);
   1046 
   1047 		case WSDISPLAYIO_PUTCMAP:
   1048 			return mgx_putcmap(sc, (struct wsdisplay_cmap *)data);
   1049 
   1050 		case WSDISPLAYIO_GCURPOS:
   1051 			{
   1052 				struct wsdisplay_curpos *cp = (void *)data;
   1053 
   1054 				cp->x = sc->sc_cursor_x;
   1055 				cp->y = sc->sc_cursor_y;
   1056 			}
   1057 			return 0;
   1058 
   1059 		case WSDISPLAYIO_SCURPOS:
   1060 			{
   1061 				struct wsdisplay_curpos *cp = (void *)data;
   1062 
   1063 				sc->sc_cursor_x = cp->x;
   1064 				sc->sc_cursor_y = cp->y;
   1065 				mgx_set_cursor(sc);
   1066 			}
   1067 			return 0;
   1068 
   1069 		case WSDISPLAYIO_GCURMAX:
   1070 			{
   1071 				struct wsdisplay_curpos *cp = (void *)data;
   1072 
   1073 				cp->x = 64;
   1074 				cp->y = 64;
   1075 			}
   1076 			return 0;
   1077 
   1078 		case WSDISPLAYIO_SCURSOR:
   1079 			{
   1080 				struct wsdisplay_cursor *cursor = (void *)data;
   1081 
   1082 				return mgx_do_cursor(sc, cursor);
   1083 			}
   1084 		case WSDISPLAYIO_GET_FBINFO:
   1085 			{
   1086 				struct wsdisplayio_fbinfo *fbi = data;
   1087 
   1088 				fbi->fbi_fbsize = sc->sc_fbsize - 1024;
   1089 				fbi->fbi_width = sc->sc_width;
   1090 				fbi->fbi_height = sc->sc_height;
   1091 				fbi->fbi_bitsperpixel = sc->sc_depth;
   1092 				fbi->fbi_stride = sc->sc_stride;
   1093 				fbi->fbi_pixeltype = WSFB_RGB;
   1094 				fbi->fbi_subtype.fbi_rgbmasks.red_offset = 8;
   1095 				fbi->fbi_subtype.fbi_rgbmasks.red_size = 8;
   1096 				fbi->fbi_subtype.fbi_rgbmasks.green_offset = 16;
   1097 				fbi->fbi_subtype.fbi_rgbmasks.green_size = 8;
   1098 				fbi->fbi_subtype.fbi_rgbmasks.blue_offset = 24;
   1099 				fbi->fbi_subtype.fbi_rgbmasks.blue_size = 8;
   1100 				fbi->fbi_subtype.fbi_rgbmasks.alpha_offset = 0;
   1101 				fbi->fbi_subtype.fbi_rgbmasks.alpha_size = 8;
   1102 				return 0;
   1103 			}
   1104 	}
   1105 	return EPASSTHROUGH;
   1106 }
   1107 
   1108 static paddr_t
   1109 mgx_mmap(void *v, void *vs, off_t offset, int prot)
   1110 {
   1111 	struct vcons_data *vd = v;
   1112 	struct mgx_softc *sc = vd->cookie;
   1113 
   1114 	/* regular fb mapping at 0 */
   1115 	if ((offset >= 0) && (offset < sc->sc_fbsize)) {
   1116 		return bus_space_mmap(sc->sc_tag, sc->sc_paddr,
   1117 		    offset, prot, BUS_SPACE_MAP_LINEAR);
   1118 	}
   1119 
   1120 	/*
   1121 	 * Blitter registers at 0x80000000, only in mapped mode.
   1122 	 * Restrict to root, even though I'm fairly sure the DMA engine lives
   1123 	 * elsewhere ( and isn't documented anyway )
   1124 	 */
   1125 	if (kauth_authorize_machdep(kauth_cred_get(),
   1126 	    KAUTH_MACHDEP_UNMANAGEDMEM,
   1127 	    NULL, NULL, NULL, NULL) != 0) {
   1128 		aprint_normal("%s: mmap() rejected.\n",
   1129 		    device_xname(sc->sc_dev));
   1130 		return -1;
   1131 	}
   1132 	if ((sc->sc_mode == WSDISPLAYIO_MODE_MAPPED) &&
   1133 	    (offset >= 0x80000000) && (offset < 0x80001000)) {
   1134 		return bus_space_mmap(sc->sc_tag, sc->sc_rpaddr,
   1135 		    offset, prot, BUS_SPACE_MAP_LINEAR);
   1136 	}
   1137 	return -1;
   1138 }
   1139 
   1140 static int
   1141 mgx_do_cursor(struct mgx_softc *sc, struct wsdisplay_cursor *cur)
   1142 {
   1143 	int i;
   1144 	if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
   1145 
   1146 		if (cur->enable) {
   1147 			mgx_set_cursor(sc);
   1148 			mgx_write_1(sc, ATR_CURSOR_ENABLE, 1);
   1149 		} else {
   1150 			mgx_write_1(sc, ATR_CURSOR_ENABLE, 0);
   1151 		}
   1152 	}
   1153 	if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
   1154 
   1155 		sc->sc_hotspot_x = cur->hot.x;
   1156 		sc->sc_hotspot_y = cur->hot.y;
   1157 		mgx_set_cursor(sc);
   1158 	}
   1159 	if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
   1160 
   1161 		sc->sc_cursor_x = cur->pos.x;
   1162 		sc->sc_cursor_y = cur->pos.y;
   1163 		mgx_set_cursor(sc);
   1164 	}
   1165 	if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
   1166 		int cnt = min(2, cur->cmap.count);
   1167 		uint8_t c;
   1168 		uint8_t r[2], g[2], b[2];
   1169 
   1170 		copyin(cur->cmap.red, r, cnt);
   1171 		copyin(cur->cmap.green, g, cnt);
   1172 		copyin(cur->cmap.blue, b, cnt);
   1173 
   1174 		for (i = 0; i < cnt; i++) {
   1175 			c = r[i] & 0xe0;
   1176 			c |= (g[i] & 0xe0) >> 3;
   1177 			c |= (b[i] & 0xc0) >> 6;
   1178 			mgx_write_1(sc, ATR_CURSOR_FG + i, c);
   1179 		}
   1180 	}
   1181 	if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
   1182 		int j;
   1183 		uint8_t *fb = sc->sc_cursor;
   1184 		uint8_t temp;
   1185 		uint8_t im, ma, px;
   1186 
   1187 		for (i = 0; i < 512; i++) {
   1188 			temp = 0;
   1189 			copyin(&cur->image[i], &im, 1);
   1190 			copyin(&cur->mask[i], &ma, 1);
   1191 			for (j = 0; j < 4; j++) {
   1192 				temp >>= 2;
   1193 				px = (ma & 1) ? 0 : 0x80;
   1194 				if (px == 0)
   1195 					px |= (im & 1) ? 0 : 0x40;
   1196 				temp |= px;
   1197 				im >>= 1;
   1198 				ma >>= 1;
   1199 			}
   1200 			*fb = temp;
   1201 			fb++;
   1202 			temp = 0;
   1203 			for (j = 0; j < 4; j++) {
   1204 				temp >>= 2;
   1205 				px = (ma & 1) ? 0 : 0x80;
   1206 				if (px == 0)
   1207 					px |= (im & 1) ? 0 : 0x40;
   1208 				temp |= px;
   1209 				im >>= 1;
   1210 				ma >>= 1;
   1211 			}
   1212 			*fb = temp;
   1213 			fb++;
   1214 		}
   1215 	}
   1216 	return 0;
   1217 }
   1218 
   1219 static void
   1220 mgx_set_cursor(struct mgx_softc *sc)
   1221 {
   1222 	uint32_t reg;
   1223 	uint16_t hot;
   1224 
   1225 	reg = (sc->sc_cursor_y << 16) | (sc->sc_cursor_x & 0xffff);
   1226 	mgx_write_4(sc, ATR_CURSOR_POSITION, reg);
   1227 	hot = (sc->sc_hotspot_y << 8) | (sc->sc_hotspot_x & 0xff);
   1228 	mgx_write_2(sc, ATR_CURSOR_HOTSPOT, hot);
   1229 }
   1230 
   1231 static void
   1232 mgx_set_video(struct mgx_softc *sc, int v)
   1233 {
   1234 	uint8_t reg;
   1235 
   1236 	if (sc->sc_video == v)
   1237 		return;
   1238 
   1239 	sc->sc_video = v;
   1240 	reg = mgx_read_1(sc, ATR_DPMS);
   1241 
   1242 	if (v == WSDISPLAYIO_VIDEO_ON) {
   1243 		reg &= ~DPMS_SYNC_DISABLE_ALL;
   1244 	} else {
   1245 		reg |= DPMS_SYNC_DISABLE_ALL;
   1246 	}
   1247 	mgx_write_1(sc, ATR_DPMS, reg);
   1248 }
   1249 
   1250 /* Sun fb dev goop */
   1251 static void
   1252 mgx_unblank(device_t dev)
   1253 {
   1254 	struct mgx_softc *sc = device_private(dev);
   1255 
   1256 	mgx_set_video(sc, WSDISPLAYIO_VIDEO_ON);
   1257 }
   1258 
   1259 paddr_t
   1260 mgxmmap(dev_t dev, off_t offset, int prot)
   1261 {
   1262 	struct mgx_softc *sc = device_lookup_private(&mgx_cd, minor(dev));
   1263 
   1264 	/* regular fb mapping at 0 */
   1265 	if ((offset >= 0) && (offset < sc->sc_fbsize)) {
   1266 		return bus_space_mmap(sc->sc_tag, sc->sc_paddr,
   1267 		    offset, prot, BUS_SPACE_MAP_LINEAR);
   1268 	}
   1269 
   1270 	/*
   1271 	 * Blitter registers at 0x80000000, only in mapped mode.
   1272 	 * Restrict to root, even though I'm fairly sure the DMA engine lives
   1273 	 * elsewhere ( and isn't documented anyway )
   1274 	 */
   1275 	if (kauth_authorize_machdep(kauth_cred_get(),
   1276 	    KAUTH_MACHDEP_UNMANAGEDMEM,
   1277 	    NULL, NULL, NULL, NULL) != 0) {
   1278 		aprint_normal("%s: mmap() rejected.\n",
   1279 		    device_xname(sc->sc_dev));
   1280 		return -1;
   1281 	}
   1282 	if ((sc->sc_mode == WSDISPLAYIO_MODE_MAPPED) &&
   1283 	    (offset >= 0x80000000) && (offset < 0x80001000)) {
   1284 		return bus_space_mmap(sc->sc_tag, sc->sc_rpaddr,
   1285 		    offset, prot, BUS_SPACE_MAP_LINEAR);
   1286 	}
   1287 	return -1;
   1288 }
   1289 
   1290 int
   1291 mgxopen(dev_t dev, int flags, int mode, struct lwp *l)
   1292 {
   1293 	device_t dv = device_lookup(&mgx_cd, minor(dev));
   1294 	struct mgx_softc *sc = device_private(dv);
   1295 
   1296 	if (dv == NULL)
   1297 		return ENXIO;
   1298 	if (sc->sc_mode == WSDISPLAYIO_MODE_MAPPED)
   1299 		return 0;
   1300 	sc->sc_mode = WSDISPLAYIO_MODE_MAPPED;
   1301 	mgx_setup(sc, 32);
   1302 	mgx_init_palette(sc);
   1303 	return 0;
   1304 }
   1305 
   1306 int
   1307 mgxclose(dev_t dev, int flags, int mode, struct lwp *l)
   1308 {
   1309 	device_t dv = device_lookup(&mgx_cd, minor(dev));
   1310 	struct mgx_softc *sc = device_private(dv);
   1311 	struct vcons_screen *ms = sc->vd.active;
   1312 
   1313 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)
   1314 		return 0;
   1315 
   1316 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
   1317 
   1318 	mgx_setup(sc, MGX_DEPTH);
   1319 	glyphcache_wipe(&sc->sc_gc);
   1320 	mgx_init_palette(sc);
   1321 	if (ms != NULL) {
   1322 		vcons_redraw_screen(ms);
   1323 	}
   1324 	return 0;
   1325 }
   1326 
   1327 int
   1328 mgxioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
   1329 {
   1330 	struct mgx_softc *sc = device_lookup_private(&mgx_cd, minor(dev));
   1331 
   1332 	return mgx_ioctl(&sc->vd, NULL, cmd, data, flags, l);
   1333 }
   1334