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