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