Home | History | Annotate | Line # | Download | only in sbus
mgx.c revision 1.5
      1 /*	$NetBSD: mgx.c,v 1.5 2016/02/11 02:23:44 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.5 2016/02/11 02:23:44 macallan Exp $");
     33 
     34 #include <sys/param.h>
     35 #include <sys/systm.h>
     36 #include <sys/buf.h>
     37 #include <sys/device.h>
     38 #include <sys/ioctl.h>
     39 #include <sys/conf.h>
     40 #include <sys/kmem.h>
     41 
     42 #include <sys/bus.h>
     43 #include <machine/autoconf.h>
     44 
     45 #include <dev/sbus/sbusvar.h>
     46 #include <dev/sun/fbio.h>
     47 #include <dev/sun/fbvar.h>
     48 
     49 #include <dev/wscons/wsdisplayvar.h>
     50 #include <dev/wscons/wsconsio.h>
     51 #include <dev/wsfont/wsfont.h>
     52 #include <dev/rasops/rasops.h>
     53 
     54 #include <dev/wscons/wsdisplay_vconsvar.h>
     55 #include <dev/wscons/wsdisplay_glyphcachevar.h>
     56 
     57 #include <dev/ic/vgareg.h>
     58 #include <dev/sbus/mgxreg.h>
     59 
     60 #include "opt_wsemul.h"
     61 #include "opt_mgx.h"
     62 
     63 struct mgx_softc {
     64 	device_t	sc_dev;
     65 	bus_space_tag_t sc_tag;
     66 	bus_space_handle_t sc_blith;
     67 	bus_space_handle_t sc_vgah;
     68 	bus_addr_t	sc_paddr;
     69 	void		*sc_fbaddr;
     70 	int		sc_width;
     71 	int		sc_height;
     72 	int		sc_stride;
     73 	int		sc_depth;
     74 	int		sc_fbsize;
     75 	int		sc_mode;
     76 	char		sc_name[8];
     77 	uint32_t	sc_dec;
     78 	u_char		sc_cmap_red[256];
     79 	u_char		sc_cmap_green[256];
     80 	u_char		sc_cmap_blue[256];
     81 	void (*sc_putchar)(void *, int, int, u_int, long);
     82 	struct vcons_screen sc_console_screen;
     83 	struct wsscreen_descr sc_defaultscreen_descr;
     84 	const struct wsscreen_descr *sc_screens[1];
     85 	struct wsscreen_list sc_screenlist;
     86 	struct vcons_data vd;
     87 	glyphcache 	sc_gc;
     88 };
     89 
     90 static int	mgx_match(device_t, cfdata_t, void *);
     91 static void	mgx_attach(device_t, device_t, void *);
     92 static int	mgx_ioctl(void *, void *, u_long, void *, int,
     93 				 struct lwp*);
     94 static paddr_t	mgx_mmap(void *, void *, off_t, int);
     95 static void	mgx_init_screen(void *, struct vcons_screen *, int,
     96 				 long *);
     97 static void	mgx_write_dac(struct mgx_softc *, int, int, int, int);
     98 static void	mgx_setup(struct mgx_softc *, int);
     99 static void	mgx_init_palette(struct mgx_softc *);
    100 static int	mgx_putcmap(struct mgx_softc *, struct wsdisplay_cmap *);
    101 static int 	mgx_getcmap(struct mgx_softc *, struct wsdisplay_cmap *);
    102 static int	mgx_wait_engine(struct mgx_softc *);
    103 static int	mgx_wait_host(struct mgx_softc *);
    104 static int	mgx_wait_fifo(struct mgx_softc *, unsigned int);
    105 
    106 static void	mgx_bitblt(void *, int, int, int, int, int, int, int);
    107 static void 	mgx_rectfill(void *, int, int, int, int, long);
    108 
    109 static void	mgx_putchar(void *, int, int, u_int, long);
    110 static void	mgx_cursor(void *, int, int, int);
    111 static void	mgx_copycols(void *, int, int, int, int);
    112 static void	mgx_erasecols(void *, int, int, int, long);
    113 static void	mgx_copyrows(void *, int, int, int);
    114 static void	mgx_eraserows(void *, int, int, long);
    115 
    116 CFATTACH_DECL_NEW(mgx, sizeof(struct mgx_softc),
    117     mgx_match, mgx_attach, NULL, NULL);
    118 
    119 struct wsdisplay_accessops mgx_accessops = {
    120 	mgx_ioctl,
    121 	mgx_mmap,
    122 	NULL,	/* vcons_alloc_screen */
    123 	NULL,	/* vcons_free_screen */
    124 	NULL,	/* vcons_show_screen */
    125 	NULL,	/* load_font */
    126 	NULL,	/* polls */
    127 	NULL,	/* scroll */
    128 };
    129 
    130 static inline void
    131 mgx_write_vga(struct mgx_softc *sc, uint32_t reg, uint8_t val)
    132 {
    133 	bus_space_write_1(sc->sc_tag, sc->sc_vgah, reg ^ 3, val);
    134 }
    135 
    136 static inline uint8_t
    137 mgx_read_vga(struct mgx_softc *sc, uint32_t reg)
    138 {
    139 	return bus_space_read_1(sc->sc_tag, sc->sc_vgah, reg ^ 3);
    140 }
    141 
    142 static inline void
    143 mgx_write_1(struct mgx_softc *sc, uint32_t reg, uint8_t val)
    144 {
    145 	bus_space_write_1(sc->sc_tag, sc->sc_blith, reg ^ 3, val);
    146 }
    147 
    148 static inline uint8_t
    149 mgx_read_1(struct mgx_softc *sc, uint32_t reg)
    150 {
    151 	return bus_space_read_1(sc->sc_tag, sc->sc_blith, reg ^ 3);
    152 }
    153 
    154 static inline void
    155 mgx_write_4(struct mgx_softc *sc, uint32_t reg, uint32_t val)
    156 {
    157 	bus_space_write_4(sc->sc_tag, sc->sc_blith, reg, val);
    158 }
    159 
    160 static int
    161 mgx_match(device_t parent, cfdata_t cf, void *aux)
    162 {
    163 	struct sbus_attach_args *sa = aux;
    164 
    165 	if (strcmp("SMSI,mgx", sa->sa_name) == 0)
    166 		return 100;
    167 	return 0;
    168 }
    169 
    170 /*
    171  * Attach a display.  We need to notice if it is the console, too.
    172  */
    173 static void
    174 mgx_attach(device_t parent, device_t self, void *args)
    175 {
    176 	struct mgx_softc *sc = device_private(self);
    177 	struct sbus_attach_args *sa = args;
    178 	struct wsemuldisplaydev_attach_args aa;
    179 	struct rasops_info *ri;
    180 	unsigned long defattr;
    181 	bus_space_handle_t bh;
    182 	int node = sa->sa_node;
    183 	int isconsole;
    184 
    185 	aprint_normal("\n");
    186 	sc->sc_dev = self;
    187 	sc->sc_tag = sa->sa_bustag;
    188 
    189 	sc->sc_paddr = sbus_bus_addr(sa->sa_bustag, sa->sa_slot,
    190 	    sa->sa_reg[8].oa_base);
    191 
    192 	/* read geometry information from the device tree */
    193 	sc->sc_width = prom_getpropint(sa->sa_node, "width", 1152);
    194 	sc->sc_height = prom_getpropint(sa->sa_node, "height", 900);
    195 	sc->sc_stride = prom_getpropint(sa->sa_node, "linebytes", 1152);
    196 	sc->sc_fbsize = prom_getpropint(sa->sa_node, "fb_size", 0x00400000);
    197 	sc->sc_fbaddr = NULL;
    198 	if (sc->sc_fbaddr == NULL) {
    199 		if (sbus_bus_map(sa->sa_bustag,
    200 			 sa->sa_slot,
    201 			 sa->sa_reg[8].oa_base,
    202 			 sc->sc_fbsize,
    203 			 BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_LARGE,
    204 			 &bh) != 0) {
    205 			aprint_error_dev(self, "couldn't map framebuffer\n");
    206 			return;
    207 		}
    208 		sc->sc_fbaddr = bus_space_vaddr(sa->sa_bustag, bh);
    209 	}
    210 
    211 	if (sbus_bus_map(sa->sa_bustag,
    212 			 sa->sa_slot,
    213 			 sa->sa_reg[4].oa_base, 0x1000, 0,
    214 			 &sc->sc_vgah) != 0) {
    215 		aprint_error("%s: couldn't map VGA registers\n",
    216 		    device_xname(sc->sc_dev));
    217 		return;
    218 	}
    219 
    220 	if (sbus_bus_map(sa->sa_bustag,
    221 			 sa->sa_slot,
    222 			 sa->sa_reg[5].oa_base + MGX_REG_ATREG_OFFSET, 0x1000,
    223 			 0, &sc->sc_blith) != 0) {
    224 		aprint_error("%s: couldn't map blitter registers\n",
    225 		    device_xname(sc->sc_dev));
    226 		return;
    227 	}
    228 
    229 	mgx_setup(sc, MGX_DEPTH);
    230 
    231 	aprint_normal_dev(self, "[%s] %d MB framebuffer, %d x %d\n",
    232 		sc->sc_name, sc->sc_fbsize >> 20, sc->sc_width, sc->sc_height);
    233 
    234 
    235 	sc->sc_defaultscreen_descr = (struct wsscreen_descr) {
    236 		"default",
    237 		0, 0,
    238 		NULL,
    239 		8, 16,
    240 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    241 		NULL
    242 	};
    243 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
    244 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
    245 
    246 	isconsole = fb_is_console(node);
    247 
    248 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    249 	wsfont_init();
    250 
    251 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr, &mgx_accessops);
    252 	sc->vd.init_screen = mgx_init_screen;
    253 
    254 	vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1, &defattr);
    255 	sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    256 
    257 	ri = &sc->sc_console_screen.scr_ri;
    258 
    259 	sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    260 	sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    261 	sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    262 	sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    263 
    264 	sc->sc_gc.gc_bitblt = mgx_bitblt;
    265 	sc->sc_gc.gc_rectfill = mgx_rectfill;
    266 	sc->sc_gc.gc_blitcookie = sc;
    267 	sc->sc_gc.gc_rop = ROP_SRC;
    268 
    269 	glyphcache_init(&sc->sc_gc,
    270 	    sc->sc_height + 5,
    271 	    (0x400000 / sc->sc_stride) - sc->sc_height - 5,
    272 	    sc->sc_width,
    273 	    ri->ri_font->fontwidth,
    274 	    ri->ri_font->fontheight,
    275 	    defattr);
    276 
    277 	mgx_init_palette(sc);
    278 
    279 	if(isconsole) {
    280 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
    281 		    defattr);
    282 		vcons_replay_msgbuf(&sc->sc_console_screen);
    283 	}
    284 
    285 	aa.console = isconsole;
    286 	aa.scrdata = &sc->sc_screenlist;
    287 	aa.accessops = &mgx_accessops;
    288 	aa.accesscookie = &sc->vd;
    289 
    290 	config_found(self, &aa, wsemuldisplaydevprint);
    291 
    292 #if 0
    293 	uint32_t *fb = sc->sc_fbaddr;
    294 	int i, j;
    295 	for (i = 0; i < 256; i += 16) {
    296 		printf("%04x:", i);
    297 		for (j = 0; j < 16; j += 4) {
    298 			printf(" %08x", fb[(i + j) >> 2]);
    299 		}
    300 		printf("\n");
    301 	}
    302 #endif
    303 
    304 }
    305 
    306 static void
    307 mgx_write_dac(struct mgx_softc *sc, int idx, int r, int g, int b)
    308 {
    309 	mgx_write_vga(sc, VGA_BASE + VGA_DAC_ADDRW, idx);
    310 	mgx_write_vga(sc, VGA_BASE + VGA_DAC_PALETTE, r);
    311 	mgx_write_vga(sc, VGA_BASE + VGA_DAC_PALETTE, g);
    312 	mgx_write_vga(sc, VGA_BASE + VGA_DAC_PALETTE, b);
    313 }
    314 
    315 static void
    316 mgx_init_palette(struct mgx_softc *sc)
    317 {
    318 	struct rasops_info *ri = &sc->sc_console_screen.scr_ri;
    319 	int i, j = 0;
    320 	uint8_t cmap[768];
    321 
    322 	if (sc->sc_depth == 8) {
    323 		rasops_get_cmap(ri, cmap, sizeof(cmap));
    324 		for (i = 0; i < 256; i++) {
    325 			sc->sc_cmap_red[i] = cmap[j];
    326 			sc->sc_cmap_green[i] = cmap[j + 1];
    327 			sc->sc_cmap_blue[i] = cmap[j + 2];
    328 			mgx_write_dac(sc, i, cmap[j], cmap[j + 1], cmap[j + 2]);
    329 			j += 3;
    330 		}
    331 	} else {
    332 		/* linear ramp for true colour modes */
    333 		for (i = 0; i < 256; i++) {
    334 			mgx_write_dac(sc, i, i, i, i);
    335 		}
    336 	}
    337 }
    338 
    339 static int
    340 mgx_putcmap(struct mgx_softc *sc, struct wsdisplay_cmap *cm)
    341 {
    342 	u_char *r, *g, *b;
    343 	u_int index = cm->index;
    344 	u_int count = cm->count;
    345 	int i, error;
    346 	u_char rbuf[256], gbuf[256], bbuf[256];
    347 
    348 	if (cm->index >= 256 || cm->count > 256 ||
    349 	    (cm->index + cm->count) > 256)
    350 		return EINVAL;
    351 	error = copyin(cm->red, &rbuf[index], count);
    352 	if (error)
    353 		return error;
    354 	error = copyin(cm->green, &gbuf[index], count);
    355 	if (error)
    356 		return error;
    357 	error = copyin(cm->blue, &bbuf[index], count);
    358 	if (error)
    359 		return error;
    360 
    361 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    362 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    363 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    364 
    365 	r = &sc->sc_cmap_red[index];
    366 	g = &sc->sc_cmap_green[index];
    367 	b = &sc->sc_cmap_blue[index];
    368 
    369 	for (i = 0; i < count; i++) {
    370 		mgx_write_dac(sc, index, *r, *g, *b);
    371 		index++;
    372 		r++, g++, b++;
    373 	}
    374 	return 0;
    375 }
    376 
    377 static int
    378 mgx_getcmap(struct mgx_softc *sc, struct wsdisplay_cmap *cm)
    379 {
    380 	u_int index = cm->index;
    381 	u_int count = cm->count;
    382 	int error;
    383 
    384 	if (index >= 255 || count > 256 || index + count > 256)
    385 		return EINVAL;
    386 
    387 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    388 	if (error)
    389 		return error;
    390 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    391 	if (error)
    392 		return error;
    393 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    394 	if (error)
    395 		return error;
    396 
    397 	return 0;
    398 }
    399 
    400 static int
    401 mgx_wait_engine(struct mgx_softc *sc)
    402 {
    403 	unsigned int i;
    404 	uint8_t stat;
    405 
    406 	for (i = 100000; i != 0; i--) {
    407 		stat = mgx_read_1(sc, ATR_BLT_STATUS);
    408 		if ((stat & (BLT_HOST_BUSY | BLT_ENGINE_BUSY)) == 0)
    409 			break;
    410 	}
    411 
    412 	return i;
    413 }
    414 
    415 static inline int
    416 mgx_wait_host(struct mgx_softc *sc)
    417 {
    418 	unsigned int i;
    419 	uint8_t stat;
    420 
    421 	for (i = 10000; i != 0; i--) {
    422 		stat = mgx_read_1(sc, ATR_BLT_STATUS);
    423 		if ((stat & BLT_HOST_BUSY) == 0)
    424 			break;
    425 	}
    426 
    427 	return i;
    428 }
    429 
    430 static int
    431 mgx_wait_fifo(struct mgx_softc *sc, unsigned int nfifo)
    432 {
    433 	unsigned int i;
    434 	uint8_t stat;
    435 
    436 	for (i = 100000; i != 0; i--) {
    437 		stat = mgx_read_1(sc, ATR_FIFO_STATUS);
    438 		stat = (stat & FIFO_MASK) >> FIFO_SHIFT;
    439 		if (stat >= nfifo)
    440 			break;
    441 		mgx_write_1(sc, ATR_FIFO_STATUS, 0);
    442 	}
    443 
    444 	return i;
    445 }
    446 
    447 static void
    448 mgx_setup(struct mgx_softc *sc, int depth)
    449 {
    450 	uint32_t stride;
    451 	int i;
    452 	uint8_t reg;
    453 
    454 	/* wait for everything to go idle */
    455 	if (mgx_wait_engine(sc) == 0)
    456 		return;
    457 	if (mgx_wait_fifo(sc, FIFO_AT24) == 0)
    458 		return;
    459 
    460 	/* read name from sequencer */
    461 	for (i = 0; i < 8; i++) {
    462 		mgx_write_vga(sc, SEQ_INDEX, i + 0x11);
    463 		sc->sc_name[i] = mgx_read_vga(sc, SEQ_DATA);
    464 	}
    465 	sc->sc_name[7] = 0;
    466 
    467 	reg = mgx_read_1(sc, ATR_PIXEL);
    468 	reg &= ~PIXEL_DEPTH_MASK;
    469 
    470 	switch (depth) {
    471 		case 8:
    472 			sc->sc_dec = DEC_DEPTH_8 << DEC_DEPTH_SHIFT;
    473 			reg |= PIXEL_8;
    474 			break;
    475 		case 15:
    476 			sc->sc_dec = DEC_DEPTH_16 << DEC_DEPTH_SHIFT;
    477 			reg |= PIXEL_15;
    478 			break;
    479 		case 16:
    480 			sc->sc_dec = DEC_DEPTH_16 << DEC_DEPTH_SHIFT;
    481 			reg |= PIXEL_16;
    482 			break;
    483 		case 32:
    484 			sc->sc_dec = DEC_DEPTH_32 << DEC_DEPTH_SHIFT;
    485 			reg |= PIXEL_32;
    486 			break;
    487 		default:
    488 			return; /* not supported */
    489 	}
    490 
    491 	/* the chip wants stride in units of 8 bytes */
    492 	sc->sc_stride = sc->sc_width * (depth >> 3);
    493 	stride = sc->sc_stride >> 3;
    494 #ifdef MGX_DEBUG
    495 	sc->sc_height = 600;
    496 #endif
    497 
    498 	sc->sc_depth = depth;
    499 
    500 	switch (sc->sc_width) {
    501 		case 640:
    502 			sc->sc_dec |= DEC_WIDTH_640 << DEC_WIDTH_SHIFT;
    503 			break;
    504 		case 800:
    505 			sc->sc_dec |= DEC_WIDTH_800 << DEC_WIDTH_SHIFT;
    506 			break;
    507 		case 1024:
    508 			sc->sc_dec |= DEC_WIDTH_1024 << DEC_WIDTH_SHIFT;
    509 			break;
    510 		case 1152:
    511 			sc->sc_dec |= DEC_WIDTH_1152 << DEC_WIDTH_SHIFT;
    512 			break;
    513 		case 1280:
    514 			sc->sc_dec |= DEC_WIDTH_1280 << DEC_WIDTH_SHIFT;
    515 			break;
    516 		case 1600:
    517 			sc->sc_dec |= DEC_WIDTH_1600 << DEC_WIDTH_SHIFT;
    518 			break;
    519 		default:
    520 			return; /* not supported */
    521 	}
    522 	mgx_write_1(sc, ATR_CLIP_CONTROL, 0);
    523 	mgx_write_1(sc, ATR_BYTEMASK, 0xff);
    524 	mgx_write_1(sc, ATR_PIXEL, reg);
    525 	mgx_write_vga(sc, CRTC_INDEX, 0x13);
    526 	mgx_write_vga(sc, CRTC_DATA, stride & 0xff);
    527 	mgx_write_vga(sc, CRTC_INDEX, 0x1c);
    528 	mgx_write_vga(sc, CRTC_DATA, (stride & 0xf00) >> 4);
    529 #ifdef MGX_DEBUG
    530 	int j;
    531 	mgx_write_vga(sc, SEQ_INDEX, 0x10);
    532 	mgx_write_vga(sc, SEQ_DATA, 0x12);
    533 	for (i = 0x10; i < 0x30; i += 16) {
    534 		printf("%02x:", i);
    535 		for (j = 0; j < 16; j++) {
    536 			mgx_write_vga(sc, SEQ_INDEX, i + j);
    537 			printf(" %02x", mgx_read_vga(sc, SEQ_DATA));
    538 		}
    539 		printf("\n");
    540 	}
    541 #if 0
    542 	mgx_write_vga(sc, SEQ_INDEX, 0x1a);
    543 	mgx_write_vga(sc, SEQ_DATA, 0x0f);
    544 #endif
    545 #endif
    546 }
    547 
    548 static void
    549 mgx_bitblt(void *cookie, int xs, int ys, int xd, int yd, int wi, int he,
    550              int rop)
    551 {
    552 	struct mgx_softc *sc = cookie;
    553 	uint32_t dec = sc->sc_dec;
    554 
    555         dec |= (DEC_COMMAND_BLT << DEC_COMMAND_SHIFT) |
    556 	       (DEC_START_DIMX << DEC_START_SHIFT);
    557 	if (xs < xd) {
    558 		xs += wi - 1;
    559 		xd += wi - 1;
    560 		dec |= DEC_DIR_X_REVERSE;
    561 	}
    562 	if (ys < yd) {
    563 		ys += he - 1;
    564 		yd += he - 1;
    565 		dec |= DEC_DIR_Y_REVERSE;
    566 	}
    567 	mgx_wait_fifo(sc, 5);
    568 	mgx_write_1(sc, ATR_ROP, rop);
    569 	mgx_write_4(sc, ATR_DEC, dec);
    570 	mgx_write_4(sc, ATR_SRC_XY, (ys << 16) | xs);
    571 	mgx_write_4(sc, ATR_DST_XY, (yd << 16) | xd);
    572 	mgx_write_4(sc, ATR_WH, (he << 16) | wi);
    573 }
    574 
    575 static void
    576 mgx_rectfill(void *cookie, int x, int y, int wi, int he, long fg)
    577 {
    578 	struct mgx_softc *sc = cookie;
    579 	struct vcons_screen *scr = sc->vd.active;
    580 	uint32_t dec = sc->sc_dec;
    581 	uint32_t col;
    582 
    583 	if (scr == NULL)
    584 		return;
    585 	col = scr->scr_ri.ri_devcmap[fg];
    586 
    587 	dec = sc->sc_dec;
    588 	dec |= (DEC_COMMAND_RECT << DEC_COMMAND_SHIFT) |
    589 	       (DEC_START_DIMX << DEC_START_SHIFT);
    590 	mgx_wait_fifo(sc, 5);
    591 	mgx_write_1(sc, ATR_ROP, ROP_SRC);
    592 	mgx_write_4(sc, ATR_FG, col);
    593 	mgx_write_4(sc, ATR_DEC, dec);
    594 	mgx_write_4(sc, ATR_DST_XY, (y << 16) | x);
    595 	mgx_write_4(sc, ATR_WH, (he << 16) | wi);
    596 }
    597 
    598 static void
    599 mgx_putchar(void *cookie, int row, int col, u_int c, long attr)
    600 {
    601 	struct rasops_info *ri = cookie;
    602 	struct wsdisplay_font *font = PICK_FONT(ri, c);
    603 	struct vcons_screen *scr = ri->ri_hw;
    604 	struct mgx_softc *sc = scr->scr_cookie;
    605 	uint32_t fg, bg;
    606 	int x, y, wi, he, rv;
    607 
    608 	wi = font->fontwidth;
    609 	he = font->fontheight;
    610 
    611 	bg = (attr >> 16) & 0xf;
    612 	fg = (attr >> 24) & 0xf;
    613 
    614 	x = ri->ri_xorigin + col * wi;
    615 	y = ri->ri_yorigin + row * he;
    616 
    617 	if (c == 0x20) {
    618 		mgx_rectfill(sc, x, y, wi, he, bg);
    619 		if (attr & 1)
    620 			mgx_rectfill(sc, x, y + he - 2, wi, 1, fg);
    621 		return;
    622 	}
    623 	rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
    624 	if (rv != GC_OK) {
    625 		volatile uint32_t junk;
    626 
    627 		mgx_wait_engine(sc);
    628 		sc->sc_putchar(cookie, row, col, c, attr & ~1);
    629 		if (rv == GC_ADD) {
    630 			junk = *(uint32_t *)sc->sc_fbaddr;
    631 			__USE(junk);
    632 			glyphcache_add(&sc->sc_gc, c, x, y);
    633 		}
    634 	}
    635 	if (attr & 1)
    636 		mgx_rectfill(sc, x, y + he - 2, wi, 1, fg);
    637 }
    638 
    639 static void
    640 mgx_cursor(void *cookie, int on, int row, int col)
    641 {
    642 	struct rasops_info *ri = cookie;
    643 	struct vcons_screen *scr = ri->ri_hw;
    644 	struct mgx_softc *sc = scr->scr_cookie;
    645 	int x, y, wi,he;
    646 
    647 	wi = ri->ri_font->fontwidth;
    648 	he = ri->ri_font->fontheight;
    649 
    650 	if (ri->ri_flg & RI_CURSOR) {
    651 		x = ri->ri_ccol * wi + ri->ri_xorigin;
    652 		y = ri->ri_crow * he + ri->ri_yorigin;
    653 		mgx_bitblt(sc, x, y, x, y, wi, he, ROP_INV);
    654 		ri->ri_flg &= ~RI_CURSOR;
    655 	}
    656 
    657 	ri->ri_crow = row;
    658 	ri->ri_ccol = col;
    659 
    660 	if (on)
    661 	{
    662 		x = ri->ri_ccol * wi + ri->ri_xorigin;
    663 		y = ri->ri_crow * he + ri->ri_yorigin;
    664 		mgx_bitblt(sc, x, y, x, y, wi, he, ROP_INV);
    665 		ri->ri_flg |= RI_CURSOR;
    666 	}
    667 }
    668 
    669 static void
    670 mgx_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
    671 {
    672 	struct rasops_info *ri = cookie;
    673 	struct vcons_screen *scr = ri->ri_hw;
    674 	struct mgx_softc *sc = scr->scr_cookie;
    675 	int32_t xs, xd, y, width, height;
    676 
    677 	xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
    678 	xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
    679 	y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    680 	width = ri->ri_font->fontwidth * ncols;
    681 	height = ri->ri_font->fontheight;
    682 	mgx_bitblt(sc, xs, y, xd, y, width, height, ROP_SRC);
    683 }
    684 
    685 static void
    686 mgx_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
    687 {
    688 	struct rasops_info *ri = cookie;
    689 	struct vcons_screen *scr = ri->ri_hw;
    690 	struct mgx_softc *sc = scr->scr_cookie;
    691 	int32_t x, y, width, height, bg;
    692 
    693 	x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
    694 	y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    695 	width = ri->ri_font->fontwidth * ncols;
    696 	height = ri->ri_font->fontheight;
    697 	bg = (fillattr >> 16) & 0xff;
    698 	mgx_rectfill(sc, x, y, width, height, bg);
    699 }
    700 
    701 static void
    702 mgx_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
    703 {
    704 	struct rasops_info *ri = cookie;
    705 	struct vcons_screen *scr = ri->ri_hw;
    706 	struct mgx_softc *sc = scr->scr_cookie;
    707 	int32_t x, ys, yd, width, height;
    708 
    709 	x = ri->ri_xorigin;
    710 	ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
    711 	yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
    712 	width = ri->ri_emuwidth;
    713 	height = ri->ri_font->fontheight * nrows;
    714 	mgx_bitblt(sc, x, ys, x, yd, width, height, ROP_SRC);
    715 }
    716 
    717 static void
    718 mgx_eraserows(void *cookie, int row, int nrows, long fillattr)
    719 {
    720 	struct rasops_info *ri = cookie;
    721 	struct vcons_screen *scr = ri->ri_hw;
    722 	struct mgx_softc *sc = scr->scr_cookie;
    723 	int32_t x, y, width, height, bg;
    724 
    725 	if ((row == 0) && (nrows == ri->ri_rows)) {
    726 		x = y = 0;
    727 		width = ri->ri_width;
    728 		height = ri->ri_height;
    729 	} else {
    730 		x = ri->ri_xorigin;
    731 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    732 		width = ri->ri_emuwidth;
    733 		height = ri->ri_font->fontheight * nrows;
    734 	}
    735 	bg = (fillattr >> 16) & 0xff;
    736 	mgx_rectfill(sc, x, y, width, height, bg);
    737 }
    738 
    739 static void
    740 mgx_init_screen(void *cookie, struct vcons_screen *scr,
    741     int existing, long *defattr)
    742 {
    743 	struct mgx_softc *sc = cookie;
    744 	struct rasops_info *ri = &scr->scr_ri;
    745 
    746 	ri->ri_depth = sc->sc_depth;
    747 	ri->ri_width = sc->sc_width;
    748 	ri->ri_height = sc->sc_height;
    749 	ri->ri_stride = sc->sc_stride;
    750 	ri->ri_flg = RI_CENTER | RI_ENABLE_ALPHA;
    751 
    752 	if (ri->ri_depth == 8)
    753 		ri->ri_flg |= RI_8BIT_IS_RGB;
    754 
    755 #ifdef MGX_NOACCEL
    756 	scr->scr_flags |= VCONS_DONT_READ;
    757 #endif
    758 
    759 	ri->ri_rnum = 8;
    760 	ri->ri_rpos = 0;
    761 	ri->ri_gnum = 8;
    762 	ri->ri_gpos = 8;
    763 	ri->ri_bnum = 8;
    764 	ri->ri_bpos = 16;
    765 
    766 	ri->ri_bits = sc->sc_fbaddr;
    767 
    768 	rasops_init(ri, 0, 0);
    769 	sc->sc_putchar = ri->ri_ops.putchar;
    770 
    771 	ri->ri_caps = WSSCREEN_REVERSE | WSSCREEN_WSCOLORS;
    772 
    773 	rasops_reconfig(ri, ri->ri_height / ri->ri_font->fontheight,
    774 		    ri->ri_width / ri->ri_font->fontwidth);
    775 
    776 	ri->ri_hw = scr;
    777 
    778 #ifdef MGX_NOACCEL
    779 if (0)
    780 #endif
    781 	{
    782 		ri->ri_ops.putchar   = mgx_putchar;
    783 		ri->ri_ops.cursor    = mgx_cursor;
    784 		ri->ri_ops.copyrows  = mgx_copyrows;
    785 		ri->ri_ops.eraserows = mgx_eraserows;
    786 		ri->ri_ops.copycols  = mgx_copycols;
    787 		ri->ri_ops.erasecols = mgx_erasecols;
    788 	}
    789 }
    790 
    791 static int
    792 mgx_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    793     struct lwp *l)
    794 {
    795 	struct vcons_data *vd = v;
    796 	struct mgx_softc *sc = vd->cookie;
    797 	struct wsdisplay_fbinfo *wdf;
    798 	struct vcons_screen *ms = vd->active;
    799 
    800 	switch (cmd) {
    801 		case WSDISPLAYIO_GTYPE:
    802 			*(u_int *)data = WSDISPLAY_TYPE_MGX;
    803 			return 0;
    804 
    805 		case WSDISPLAYIO_GINFO:
    806 			wdf = (void *)data;
    807 			wdf->height = sc->sc_height;
    808 			wdf->width = sc->sc_width;
    809 			wdf->depth = 8;
    810 			wdf->cmsize = 256;
    811 			return 0;
    812 
    813 		case FBIOGVIDEO:
    814 		case WSDISPLAYIO_GVIDEO:
    815 			*(int *)data = 1;
    816 			return 0;
    817 
    818 		case WSDISPLAYIO_SVIDEO:
    819 		case FBIOSVIDEO:
    820 			return 0;
    821 
    822 		case WSDISPLAYIO_LINEBYTES:
    823 			{
    824 				int *ret = (int *)data;
    825 				*ret = sc->sc_stride;
    826 			}
    827 			return 0;
    828 
    829 		case WSDISPLAYIO_SMODE:
    830 			{
    831 				int new_mode = *(int*)data;
    832 				if (new_mode != sc->sc_mode)
    833 				{
    834 					sc->sc_mode = new_mode;
    835 					if (new_mode == WSDISPLAYIO_MODE_EMUL)
    836 					{
    837 						mgx_setup(sc, MGX_DEPTH);
    838 						glyphcache_wipe(&sc->sc_gc);
    839 						mgx_init_palette(sc);
    840 						vcons_redraw_screen(ms);
    841 					} else {
    842 						mgx_setup(sc, 32);
    843 						mgx_init_palette(sc);
    844 					}
    845 				}
    846 			}
    847 			return 0;
    848 
    849 		case WSDISPLAYIO_GETCMAP:
    850 			return mgx_getcmap(sc, (struct wsdisplay_cmap *)data);
    851 
    852 		case WSDISPLAYIO_PUTCMAP:
    853 			return mgx_putcmap(sc, (struct wsdisplay_cmap *)data);
    854 
    855 		case WSDISPLAYIO_GET_FBINFO:
    856 			{
    857 				struct wsdisplayio_fbinfo *fbi = data;
    858 
    859 				fbi->fbi_fbsize = sc->sc_fbsize;
    860 				fbi->fbi_width = sc->sc_width;
    861 				fbi->fbi_height = sc->sc_height;
    862 				fbi->fbi_bitsperpixel = sc->sc_depth;
    863 				fbi->fbi_stride = sc->sc_stride;
    864 				fbi->fbi_pixeltype = WSFB_RGB;
    865 				fbi->fbi_subtype.fbi_rgbmasks.red_offset = 8;
    866 				fbi->fbi_subtype.fbi_rgbmasks.red_size = 8;
    867 				fbi->fbi_subtype.fbi_rgbmasks.green_offset = 16;
    868 				fbi->fbi_subtype.fbi_rgbmasks.green_size = 8;
    869 				fbi->fbi_subtype.fbi_rgbmasks.blue_offset = 24;
    870 				fbi->fbi_subtype.fbi_rgbmasks.blue_size = 8;
    871 				fbi->fbi_subtype.fbi_rgbmasks.alpha_offset = 0;
    872 				fbi->fbi_subtype.fbi_rgbmasks.alpha_size = 8;
    873 				return 0;
    874 			}
    875 	}
    876 	return EPASSTHROUGH;
    877 }
    878 
    879 static paddr_t
    880 mgx_mmap(void *v, void *vs, off_t offset, int prot)
    881 {
    882 	struct vcons_data *vd = v;
    883 	struct mgx_softc *sc = vd->cookie;
    884 
    885 	/* regular fb mapping at 0 */
    886 	if ((offset >= 0) && (offset < sc->sc_fbsize)) {
    887 		return bus_space_mmap(sc->sc_tag, sc->sc_paddr,
    888 		    offset, prot, BUS_SPACE_MAP_LINEAR);
    889 	}
    890 
    891 	return -1;
    892 }
    893