Home | History | Annotate | Line # | Download | only in sbus
mgx.c revision 1.22
      1 /*	$NetBSD: mgx.c,v 1.22 2023/06/28 08:53:43 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.22 2023/06/28 08:53:43 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, CFARGS_NONE);
    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_wait_engine(sc);
    671 	mgx_write_1(sc, ATR_ROP, ROP_SRC);
    672 	mgx_write_4(sc, ATR_FG, col);
    673 	mgx_write_4(sc, ATR_DEC, dec);
    674 	mgx_write_4(sc, ATR_DST_XY, (y << 16) | x);
    675 	mgx_write_4(sc, ATR_WH, (he << 16) | wi);
    676 }
    677 
    678 static void
    679 mgx_putchar_aa(void *cookie, int row, int col, u_int c, long attr)
    680 {
    681 	struct rasops_info *ri = cookie;
    682 	struct wsdisplay_font *font = PICK_FONT(ri, c);
    683 	struct vcons_screen *scr = ri->ri_hw;
    684 	struct mgx_softc *sc = scr->scr_cookie;
    685 	uint32_t fg, bg;
    686 	int x, y, wi, he, rv;
    687 
    688 	wi = font->fontwidth;
    689 	he = font->fontheight;
    690 
    691 	bg = (attr >> 16) & 0xf;
    692 	fg = (attr >> 24) & 0xf;
    693 
    694 	x = ri->ri_xorigin + col * wi;
    695 	y = ri->ri_yorigin + row * he;
    696 
    697 	if (c == 0x20) {
    698 		mgx_rectfill(sc, x, y, wi, he, bg);
    699 		if (attr & 1)
    700 			mgx_rectfill(sc, x, y + he - 2, wi, 1, fg);
    701 		return;
    702 	}
    703 
    704 	rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
    705 	if (rv != GC_OK) {
    706 		volatile uint32_t junk;
    707 
    708 		mgx_wait_engine(sc);
    709 		sc->sc_putchar(cookie, row, col, c, attr & ~1);
    710 		if (rv == GC_ADD) {
    711 			/*
    712 			 * try to make sure the glyph made it all the way to
    713 			 * video memory before trying to blit it into the cache
    714 			 */
    715 			junk = *(uint32_t *)sc->sc_fbaddr;
    716 			__USE(junk);
    717 			glyphcache_add(&sc->sc_gc, c, x, y);
    718 		}
    719 	}
    720 	if (attr & 1)
    721 		mgx_rectfill(sc, x, y + he - 2, wi, 1, fg);
    722 }
    723 
    724 static void
    725 mgx_putchar_mono(void *cookie, int row, int col, u_int c, long attr)
    726 {
    727 	struct rasops_info *ri = cookie;
    728 	struct wsdisplay_font *font = PICK_FONT(ri, c);
    729 	struct vcons_screen *scr = ri->ri_hw;
    730 	struct mgx_softc *sc = scr->scr_cookie;
    731 	uint8_t *s, *d;
    732 	uint32_t fg, bg, scratch = ((sc->sc_stride * sc->sc_height) + 7) & ~7;
    733 	int x, y, wi, he, len, i;
    734 
    735 	wi = font->fontwidth;
    736 	he = font->fontheight;
    737 
    738 	bg = (attr >> 16) & 0xf;
    739 	fg = (attr >> 24) & 0xf;
    740 
    741 	x = ri->ri_xorigin + col * wi;
    742 	y = ri->ri_yorigin + row * he;
    743 
    744 	if (!CHAR_IN_FONT(c, font)) {
    745 		c = 0x20;
    746 #ifdef MGX_DEBUG
    747 		bg = WSCOL_LIGHT_BLUE;
    748 #endif
    749 	}
    750 	if (c == 0x20) {
    751 		mgx_rectfill(sc, x, y, wi, he, bg);
    752 		if (attr & 1)
    753 			mgx_rectfill(sc, x, y + he - 2, wi, 1, fg);
    754 		return;
    755 	}
    756 
    757 	mgx_wait_fifo(sc, 3);
    758 	mgx_write_4(sc, ATR_FG, ri->ri_devcmap[fg]);
    759 	mgx_write_4(sc, ATR_BG, ri->ri_devcmap[bg]);
    760 	mgx_write_1(sc, ATR_ROP, ROP_SRC);
    761 
    762 	/*
    763 	 * do hardware colour expansion
    764 	 * there has to be an upload port somewhere, since there are host blit
    765 	 * commands, but it's not used or mentioned in the xf86-video-apm driver
    766 	 * so for now we use the vram-to-vram blits to draw characters.
    767 	 * stash most of the font in vram for speed, also:
    768 	 * - the sbus-pci bridge doesn't seem to support 64bit accesses,
    769 	 *   they will cause occasional data corruption and all sorts of weird
    770 	 *   side effects, so copy font bitmaps byte-wise
    771 	 * - at least it doesn't seem to need any kind of buffer flushing
    772 	 * - still use rotation buffers for characters that don't fall into the
    773 	 *   8 bit range
    774 	 */
    775 
    776 	len = (ri->ri_fontscale + 7) & ~7;
    777 	s = WSFONT_GLYPH(c, font);
    778 	if ((c > 32) && (c < 256)) {
    779 		scratch += len * c;
    780 		if (sc->sc_in[c] == 0) {
    781 			d = (uint8_t *)sc->sc_fbaddr + scratch;
    782 			for (i = 0; i < ri->ri_fontscale; i++)
    783 				d[i] = s[i];
    784 			sc->sc_in[c] = 1;
    785 		}
    786 	} else {
    787 		sc->sc_buf = (sc->sc_buf + 1) & 7; /* rotate through 8 buffers */
    788 		scratch += sc->sc_buf * len;
    789 		d = (uint8_t *)sc->sc_fbaddr + scratch;
    790 		for (i = 0; i < ri->ri_fontscale; i++)
    791 			d[i] = s[i];
    792 	}
    793 	mgx_wait_fifo(sc, 5);
    794 	mgx_write_4(sc, ATR_DEC, sc->sc_dec | (DEC_COMMAND_BLT << DEC_COMMAND_SHIFT) |
    795 	       (DEC_START_DIMX << DEC_START_SHIFT) |
    796 	       DEC_SRC_LINEAR | DEC_SRC_CONTIGUOUS | DEC_MONOCHROME);
    797 	mgx_write_4(sc, ATR_SRC_XY, ((scratch & 0xfff000) << 4) | (scratch & 0xfff));
    798 	mgx_write_4(sc, ATR_DST_XY, (y << 16) | x);
    799 	mgx_write_4(sc, ATR_WH, (he << 16) | wi);
    800 
    801 	if (attr & 1)
    802 		mgx_rectfill(sc, x, y + he - 2, wi, 1, fg);
    803 }
    804 
    805 static void
    806 mgx_cursor(void *cookie, int on, int row, int col)
    807 {
    808 	struct rasops_info *ri = cookie;
    809 	struct vcons_screen *scr = ri->ri_hw;
    810 	struct mgx_softc *sc = scr->scr_cookie;
    811 	int x, y, wi,he;
    812 
    813 	wi = ri->ri_font->fontwidth;
    814 	he = ri->ri_font->fontheight;
    815 
    816 	if (ri->ri_flg & RI_CURSOR) {
    817 		x = ri->ri_ccol * wi + ri->ri_xorigin;
    818 		y = ri->ri_crow * he + ri->ri_yorigin;
    819 		mgx_bitblt(sc, x, y, x, y, wi, he, ROP_INV);
    820 		ri->ri_flg &= ~RI_CURSOR;
    821 	}
    822 
    823 	ri->ri_crow = row;
    824 	ri->ri_ccol = col;
    825 
    826 	if (on)
    827 	{
    828 		x = ri->ri_ccol * wi + ri->ri_xorigin;
    829 		y = ri->ri_crow * he + ri->ri_yorigin;
    830 		mgx_bitblt(sc, x, y, x, y, wi, he, ROP_INV);
    831 		ri->ri_flg |= RI_CURSOR;
    832 	}
    833 }
    834 
    835 static void
    836 mgx_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
    837 {
    838 	struct rasops_info *ri = cookie;
    839 	struct vcons_screen *scr = ri->ri_hw;
    840 	struct mgx_softc *sc = scr->scr_cookie;
    841 	int32_t xs, xd, y, width, height;
    842 
    843 	xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
    844 	xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
    845 	y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    846 	width = ri->ri_font->fontwidth * ncols;
    847 	height = ri->ri_font->fontheight;
    848 	mgx_bitblt(sc, xs, y, xd, y, width, height, ROP_SRC);
    849 }
    850 
    851 static void
    852 mgx_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
    853 {
    854 	struct rasops_info *ri = cookie;
    855 	struct vcons_screen *scr = ri->ri_hw;
    856 	struct mgx_softc *sc = scr->scr_cookie;
    857 	int32_t x, y, width, height, bg;
    858 
    859 	x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
    860 	y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    861 	width = ri->ri_font->fontwidth * ncols;
    862 	height = ri->ri_font->fontheight;
    863 	bg = (fillattr >> 16) & 0xff;
    864 	mgx_rectfill(sc, x, y, width, height, bg);
    865 }
    866 
    867 static void
    868 mgx_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
    869 {
    870 	struct rasops_info *ri = cookie;
    871 	struct vcons_screen *scr = ri->ri_hw;
    872 	struct mgx_softc *sc = scr->scr_cookie;
    873 	int32_t x, ys, yd, width, height;
    874 
    875 	x = ri->ri_xorigin;
    876 	ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
    877 	yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
    878 	width = ri->ri_emuwidth;
    879 	height = ri->ri_font->fontheight * nrows;
    880 	mgx_bitblt(sc, x, ys, x, yd, width, height, ROP_SRC);
    881 }
    882 
    883 static void
    884 mgx_eraserows(void *cookie, int row, int nrows, long fillattr)
    885 {
    886 	struct rasops_info *ri = cookie;
    887 	struct vcons_screen *scr = ri->ri_hw;
    888 	struct mgx_softc *sc = scr->scr_cookie;
    889 	int32_t x, y, width, height, bg;
    890 
    891 	if ((row == 0) && (nrows == ri->ri_rows)) {
    892 		x = y = 0;
    893 		width = ri->ri_width;
    894 		height = ri->ri_height;
    895 	} else {
    896 		x = ri->ri_xorigin;
    897 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    898 		width = ri->ri_emuwidth;
    899 		height = ri->ri_font->fontheight * nrows;
    900 	}
    901 	bg = (fillattr >> 16) & 0xff;
    902 	mgx_rectfill(sc, x, y, width, height, bg);
    903 }
    904 
    905 static void
    906 mgx_adapt(struct vcons_screen *scr, void *cookie)
    907 {
    908 	struct mgx_softc *sc = cookie;
    909 	memset(sc->sc_in, 0, sizeof(sc->sc_in));
    910 	glyphcache_adapt(scr, &sc->sc_gc);
    911 }
    912 
    913 static void
    914 mgx_init_screen(void *cookie, struct vcons_screen *scr,
    915     int existing, long *defattr)
    916 {
    917 	struct mgx_softc *sc = cookie;
    918 	struct rasops_info *ri = &scr->scr_ri;
    919 
    920 	ri->ri_depth = sc->sc_depth;
    921 	ri->ri_width = sc->sc_width;
    922 	ri->ri_height = sc->sc_height;
    923 	ri->ri_stride = sc->sc_stride;
    924 	ri->ri_flg = RI_CENTER | RI_ENABLE_ALPHA;
    925 
    926 	if (ri->ri_depth == 8)
    927 		ri->ri_flg |= RI_8BIT_IS_RGB;
    928 
    929 #ifdef MGX_NOACCEL
    930 	scr->scr_flags |= VCONS_DONT_READ;
    931 #endif
    932 	scr->scr_flags |= VCONS_LOADFONT;
    933 
    934 	ri->ri_rnum = 8;
    935 	ri->ri_rpos = 0;
    936 	ri->ri_gnum = 8;
    937 	ri->ri_gpos = 8;
    938 	ri->ri_bnum = 8;
    939 	ri->ri_bpos = 16;
    940 
    941 	ri->ri_bits = sc->sc_fbaddr;
    942 
    943 	rasops_init(ri, 0, 0);
    944 
    945 	ri->ri_caps = WSSCREEN_REVERSE | WSSCREEN_WSCOLORS |
    946 		      WSSCREEN_UNDERLINE | WSSCREEN_RESIZE;
    947 
    948 	rasops_reconfig(ri, ri->ri_height / ri->ri_font->fontheight,
    949 		    ri->ri_width / ri->ri_font->fontwidth);
    950 
    951 	ri->ri_hw = scr;
    952 
    953 #ifdef MGX_NOACCEL
    954 if (0)
    955 #endif
    956 	{
    957 		if (FONT_IS_ALPHA(ri->ri_font)) {
    958 			sc->sc_putchar = ri->ri_ops.putchar;
    959 			ri->ri_ops.putchar   = mgx_putchar_aa;
    960 		} else {
    961 			ri->ri_ops.putchar   = mgx_putchar_mono;
    962 		}
    963 		ri->ri_ops.cursor    = mgx_cursor;
    964 		ri->ri_ops.copyrows  = mgx_copyrows;
    965 		ri->ri_ops.eraserows = mgx_eraserows;
    966 		ri->ri_ops.copycols  = mgx_copycols;
    967 		ri->ri_ops.erasecols = mgx_erasecols;
    968 	}
    969 }
    970 
    971 static int
    972 mgx_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    973     struct lwp *l)
    974 {
    975 	struct vcons_data *vd = v;
    976 	struct mgx_softc *sc = vd->cookie;
    977 	struct wsdisplay_fbinfo *wdf;
    978 	struct vcons_screen *ms = vd->active;
    979 
    980 	switch (cmd) {
    981 	case WSDISPLAYIO_GTYPE:
    982 		*(u_int *)data = WSDISPLAY_TYPE_MGX;
    983 		return 0;
    984 
    985 	case WSDISPLAYIO_GINFO:
    986 		wdf = (void *)data;
    987 		wdf->height = sc->sc_height;
    988 		wdf->width = sc->sc_width;
    989 		wdf->depth = 8;
    990 		wdf->cmsize = 256;
    991 		return 0;
    992 
    993 	case FBIOGTYPE:
    994 		*(struct fbtype *)data = sc->sc_fb.fb_type;
    995 		return 0;
    996 
    997 	case FBIOGATTR:
    998 #define fba ((struct fbgattr *)data)
    999 		fba->real_type = sc->sc_fb.fb_type.fb_type;
   1000 		fba->owner = 0;		/* XXX ??? */
   1001 		fba->fbtype = sc->sc_fb.fb_type;
   1002 		fba->sattr.flags = 0;
   1003 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
   1004 		fba->sattr.dev_specific[0] = -1;
   1005 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
   1006 		fba->emu_types[1] = -1;
   1007 #undef fba
   1008 		return 0;
   1009 	case FBIOGVIDEO:
   1010 	case WSDISPLAYIO_GVIDEO:
   1011 		*(int *)data = sc->sc_video;
   1012 		return 0;
   1013 
   1014 	case WSDISPLAYIO_SVIDEO:
   1015 	case FBIOSVIDEO:
   1016 		mgx_set_video(sc, *(int *)data);
   1017 		return 0;
   1018 
   1019 	case WSDISPLAYIO_LINEBYTES:
   1020 		{
   1021 			int *ret = (int *)data;
   1022 			*ret = sc->sc_stride;
   1023 		}
   1024 		return 0;
   1025 
   1026 	case WSDISPLAYIO_SMODE:
   1027 		{
   1028 			int new_mode = *(int*)data;
   1029 			if (new_mode != sc->sc_mode)
   1030 			{
   1031 				sc->sc_mode = new_mode;
   1032 				if (new_mode == WSDISPLAYIO_MODE_EMUL)
   1033 				{
   1034 					mgx_setup(sc, MGX_DEPTH);
   1035 					glyphcache_wipe(&sc->sc_gc);
   1036 					mgx_init_palette(sc);
   1037 					vcons_redraw_screen(ms);
   1038 				} else {
   1039 					mgx_setup(sc, 32);
   1040 					mgx_init_palette(sc);
   1041 				}
   1042 			}
   1043 		}
   1044 		return 0;
   1045 
   1046 	case WSDISPLAYIO_GETCMAP:
   1047 		return mgx_getcmap(sc, (struct wsdisplay_cmap *)data);
   1048 
   1049 	case WSDISPLAYIO_PUTCMAP:
   1050 		return mgx_putcmap(sc, (struct wsdisplay_cmap *)data);
   1051 
   1052 	case WSDISPLAYIO_GCURPOS:
   1053 		{
   1054 			struct wsdisplay_curpos *cp = (void *)data;
   1055 
   1056 			cp->x = sc->sc_cursor_x;
   1057 			cp->y = sc->sc_cursor_y;
   1058 		}
   1059 		return 0;
   1060 
   1061 	case WSDISPLAYIO_SCURPOS:
   1062 		{
   1063 			struct wsdisplay_curpos *cp = (void *)data;
   1064 
   1065 			sc->sc_cursor_x = cp->x;
   1066 			sc->sc_cursor_y = cp->y;
   1067 			mgx_set_cursor(sc);
   1068 		}
   1069 		return 0;
   1070 
   1071 	case WSDISPLAYIO_GCURMAX:
   1072 		{
   1073 			struct wsdisplay_curpos *cp = (void *)data;
   1074 
   1075 			cp->x = 64;
   1076 			cp->y = 64;
   1077 		}
   1078 		return 0;
   1079 
   1080 	case WSDISPLAYIO_SCURSOR:
   1081 		{
   1082 			struct wsdisplay_cursor *cursor = (void *)data;
   1083 
   1084 			return mgx_do_cursor(sc, cursor);
   1085 		}
   1086 
   1087 	case WSDISPLAYIO_GET_FBINFO:
   1088 		{
   1089 			struct wsdisplayio_fbinfo *fbi = data;
   1090 
   1091 			fbi->fbi_fbsize = sc->sc_fbsize - 1024;
   1092 			fbi->fbi_width = sc->sc_width;
   1093 			fbi->fbi_height = sc->sc_height;
   1094 			fbi->fbi_bitsperpixel = sc->sc_depth;
   1095 			fbi->fbi_stride = sc->sc_stride;
   1096 			fbi->fbi_pixeltype = WSFB_RGB;
   1097 			fbi->fbi_subtype.fbi_rgbmasks.red_offset = 8;
   1098 			fbi->fbi_subtype.fbi_rgbmasks.red_size = 8;
   1099 			fbi->fbi_subtype.fbi_rgbmasks.green_offset = 16;
   1100 			fbi->fbi_subtype.fbi_rgbmasks.green_size = 8;
   1101 			fbi->fbi_subtype.fbi_rgbmasks.blue_offset = 24;
   1102 			fbi->fbi_subtype.fbi_rgbmasks.blue_size = 8;
   1103 			fbi->fbi_subtype.fbi_rgbmasks.alpha_offset = 0;
   1104 			fbi->fbi_subtype.fbi_rgbmasks.alpha_size = 8;
   1105 			return 0;
   1106 		}
   1107 	}
   1108 	return EPASSTHROUGH;
   1109 }
   1110 
   1111 static paddr_t
   1112 mgx_mmap(void *v, void *vs, off_t offset, int prot)
   1113 {
   1114 	struct vcons_data *vd = v;
   1115 	struct mgx_softc *sc = vd->cookie;
   1116 	uint32_t flags = BUS_SPACE_MAP_LINEAR;
   1117 
   1118 #ifdef BUS_SPACE_MAP_LITTLE
   1119 	if (offset & MGX_FLIPOFFSET) {
   1120 		offset &= ~MGX_FLIPOFFSET;
   1121 		flags |= BUS_SPACE_MAP_LITTLE;
   1122 	}
   1123 #endif
   1124 
   1125 	/* regular fb mapping at 0 */
   1126 	if ((offset >= 0) && (offset < sc->sc_fbsize)) {
   1127 		return bus_space_mmap(sc->sc_tag, sc->sc_paddr,
   1128 		    offset, prot, flags);
   1129 	}
   1130 
   1131 	/*
   1132 	 * Blitter registers at 0x00800000, only in mapped mode.
   1133 	 * Restrict to root, even though I'm fairly sure the DMA engine lives
   1134 	 * elsewhere ( and isn't documented anyway )
   1135 	 */
   1136 	if (kauth_authorize_machdep(kauth_cred_get(),
   1137 	    KAUTH_MACHDEP_UNMANAGEDMEM,
   1138 	    NULL, NULL, NULL, NULL) != 0) {
   1139 		aprint_normal("%s: mmap() rejected.\n",
   1140 		    device_xname(sc->sc_dev));
   1141 		return -1;
   1142 	}
   1143 	if ((sc->sc_mode == WSDISPLAYIO_MODE_MAPPED) &&
   1144 	    (offset >= MGX_BLTOFFSET) && (offset < MGX_BLTOFFSET + 0x1000)) {
   1145 		return bus_space_mmap(sc->sc_tag, sc->sc_rpaddr,
   1146 		    offset - MGX_BLTOFFSET, prot, BUS_SPACE_MAP_LINEAR);
   1147 	}
   1148 	return -1;
   1149 }
   1150 
   1151 static int
   1152 mgx_do_cursor(struct mgx_softc *sc, struct wsdisplay_cursor *cur)
   1153 {
   1154 	int i;
   1155 	if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
   1156 
   1157 		if (cur->enable) {
   1158 			mgx_set_cursor(sc);
   1159 			mgx_write_1(sc, ATR_CURSOR_ENABLE, 1);
   1160 		} else {
   1161 			mgx_write_1(sc, ATR_CURSOR_ENABLE, 0);
   1162 		}
   1163 	}
   1164 	if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
   1165 
   1166 		sc->sc_hotspot_x = cur->hot.x;
   1167 		sc->sc_hotspot_y = cur->hot.y;
   1168 		mgx_set_cursor(sc);
   1169 	}
   1170 	if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
   1171 
   1172 		sc->sc_cursor_x = cur->pos.x;
   1173 		sc->sc_cursor_y = cur->pos.y;
   1174 		mgx_set_cursor(sc);
   1175 	}
   1176 	if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
   1177 		int cnt = uimin(2, cur->cmap.count);
   1178 		uint8_t c;
   1179 		uint8_t r[2], g[2], b[2];
   1180 
   1181 		copyin(cur->cmap.red, r, cnt);
   1182 		copyin(cur->cmap.green, g, cnt);
   1183 		copyin(cur->cmap.blue, b, cnt);
   1184 
   1185 		for (i = 0; i < cnt; i++) {
   1186 			c = r[i] & 0xe0;
   1187 			c |= (g[i] & 0xe0) >> 3;
   1188 			c |= (b[i] & 0xc0) >> 6;
   1189 			mgx_write_1(sc, ATR_CURSOR_FG + i, c);
   1190 		}
   1191 	}
   1192 	if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
   1193 		int j;
   1194 		uint8_t *fb = sc->sc_cursor;
   1195 		uint8_t temp;
   1196 		uint8_t im, ma, px;
   1197 
   1198 		for (i = 0; i < 512; i++) {
   1199 			temp = 0;
   1200 			copyin(&cur->image[i], &im, 1);
   1201 			copyin(&cur->mask[i], &ma, 1);
   1202 			for (j = 0; j < 4; j++) {
   1203 				temp >>= 2;
   1204 				px = (ma & 1) ? 0 : 0x80;
   1205 				if (px == 0)
   1206 					px |= (im & 1) ? 0 : 0x40;
   1207 				temp |= px;
   1208 				im >>= 1;
   1209 				ma >>= 1;
   1210 			}
   1211 			*fb = temp;
   1212 			fb++;
   1213 			temp = 0;
   1214 			for (j = 0; j < 4; j++) {
   1215 				temp >>= 2;
   1216 				px = (ma & 1) ? 0 : 0x80;
   1217 				if (px == 0)
   1218 					px |= (im & 1) ? 0 : 0x40;
   1219 				temp |= px;
   1220 				im >>= 1;
   1221 				ma >>= 1;
   1222 			}
   1223 			*fb = temp;
   1224 			fb++;
   1225 		}
   1226 	}
   1227 	return 0;
   1228 }
   1229 
   1230 static void
   1231 mgx_set_cursor(struct mgx_softc *sc)
   1232 {
   1233 	uint32_t reg;
   1234 	uint16_t hot;
   1235 
   1236 	reg = (sc->sc_cursor_y << 16) | (sc->sc_cursor_x & 0xffff);
   1237 	mgx_write_4(sc, ATR_CURSOR_POSITION, reg);
   1238 	hot = (sc->sc_hotspot_y << 8) | (sc->sc_hotspot_x & 0xff);
   1239 	mgx_write_2(sc, ATR_CURSOR_HOTSPOT, hot);
   1240 }
   1241 
   1242 static void
   1243 mgx_set_video(struct mgx_softc *sc, int v)
   1244 {
   1245 	uint8_t reg;
   1246 
   1247 	if (sc->sc_video == v)
   1248 		return;
   1249 
   1250 	sc->sc_video = v;
   1251 	reg = mgx_read_1(sc, ATR_DPMS);
   1252 
   1253 	if (v == WSDISPLAYIO_VIDEO_ON) {
   1254 		reg &= ~DPMS_SYNC_DISABLE_ALL;
   1255 	} else {
   1256 		reg |= DPMS_SYNC_DISABLE_ALL;
   1257 	}
   1258 	mgx_write_1(sc, ATR_DPMS, reg);
   1259 }
   1260 
   1261 /* Sun fb dev goop */
   1262 static void
   1263 mgx_unblank(device_t dev)
   1264 {
   1265 	struct mgx_softc *sc = device_private(dev);
   1266 
   1267 	mgx_set_video(sc, WSDISPLAYIO_VIDEO_ON);
   1268 }
   1269 
   1270 paddr_t
   1271 mgxmmap(dev_t dev, off_t offset, int prot)
   1272 {
   1273 	struct mgx_softc *sc = device_lookup_private(&mgx_cd, minor(dev));
   1274 	uint32_t flags = BUS_SPACE_MAP_LINEAR;
   1275 
   1276 #ifdef BUS_SPACE_MAP_LITTLE
   1277 	if (offset & MGX_FLIPOFFSET) {
   1278 		offset &= ~MGX_FLIPOFFSET;
   1279 		flags |= BUS_SPACE_MAP_LITTLE;
   1280 	}
   1281 #endif
   1282 
   1283 	/* regular fb mapping at 0 */
   1284 	if ((offset >= 0) && (offset < sc->sc_fbsize)) {
   1285 		return bus_space_mmap(sc->sc_tag, sc->sc_paddr,
   1286 		    offset, prot, flags);
   1287 	}
   1288 
   1289 	/*
   1290 	 * Blitter registers at 0x00800000, only in mapped mode.
   1291 	 * Restrict to root, even though I'm fairly sure the DMA engine lives
   1292 	 * elsewhere ( and isn't documented anyway )
   1293 	 */
   1294 	if (kauth_authorize_machdep(kauth_cred_get(),
   1295 	    KAUTH_MACHDEP_UNMANAGEDMEM,
   1296 	    NULL, NULL, NULL, NULL) != 0) {
   1297 		aprint_normal("%s: mmap() rejected.\n",
   1298 		    device_xname(sc->sc_dev));
   1299 		return -1;
   1300 	}
   1301 	if ((sc->sc_mode == WSDISPLAYIO_MODE_MAPPED) &&
   1302 	    (offset >= MGX_BLTOFFSET) && (offset < MGX_BLTOFFSET + 0x1000)) {
   1303 		return bus_space_mmap(sc->sc_tag, sc->sc_rpaddr,
   1304 		    offset - MGX_BLTOFFSET, prot, BUS_SPACE_MAP_LINEAR);
   1305 	}
   1306 	return -1;
   1307 }
   1308 
   1309 int
   1310 mgxopen(dev_t dev, int flags, int mode, struct lwp *l)
   1311 {
   1312 	device_t dv = device_lookup(&mgx_cd, minor(dev));
   1313 	struct mgx_softc *sc = device_private(dv);
   1314 
   1315 	if (dv == NULL)
   1316 		return ENXIO;
   1317 	if (sc->sc_mode == WSDISPLAYIO_MODE_MAPPED)
   1318 		return 0;
   1319 	sc->sc_mode = WSDISPLAYIO_MODE_MAPPED;
   1320 	mgx_setup(sc, 32);
   1321 	mgx_init_palette(sc);
   1322 	return 0;
   1323 }
   1324 
   1325 int
   1326 mgxclose(dev_t dev, int flags, int mode, struct lwp *l)
   1327 {
   1328 	device_t dv = device_lookup(&mgx_cd, minor(dev));
   1329 	struct mgx_softc *sc = device_private(dv);
   1330 	struct vcons_screen *ms = sc->vd.active;
   1331 
   1332 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)
   1333 		return 0;
   1334 
   1335 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
   1336 
   1337 	mgx_setup(sc, MGX_DEPTH);
   1338 	glyphcache_wipe(&sc->sc_gc);
   1339 	mgx_init_palette(sc);
   1340 	if (ms != NULL) {
   1341 		vcons_redraw_screen(ms);
   1342 	}
   1343 	return 0;
   1344 }
   1345 
   1346 int
   1347 mgxioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
   1348 {
   1349 	struct mgx_softc *sc = device_lookup_private(&mgx_cd, minor(dev));
   1350 
   1351 	return mgx_ioctl(&sc->vd, NULL, cmd, data, flags, l);
   1352 }
   1353