Home | History | Annotate | Line # | Download | only in sbus
zx.c revision 1.28
      1 /*	$NetBSD: zx.c,v 1.28 2009/05/05 23:55:04 macallan Exp $	*/
      2 
      3 /*
      4  *  Copyright (c) 2002 The NetBSD Foundation, Inc.
      5  *  All rights reserved.
      6  *
      7  *  This code is derived from software contributed to The NetBSD Foundation
      8  *  by Andrew Doran.
      9  *
     10  *  Redistribution and use in source and binary forms, with or without
     11  *  modification, are permitted provided that the following conditions
     12  *  are met:
     13  *  1. Redistributions of source code must retain the above copyright
     14  *     notice, this list of conditions and the following disclaimer.
     15  *  2. Redistributions in binary form must reproduce the above copyright
     16  *     notice, this list of conditions and the following disclaimer in the
     17  *     documentation and/or other materials provided with the distribution.
     18  *
     19  *  THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  *  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  *  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  *  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  *  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  *  POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Driver for the Sun ZX display adapter.  This would be called 'leo', but
     34  * NetBSD/amiga already has a driver by that name.  The XFree86 and Linux
     35  * drivers were used as "living documentation" when writing this; thanks
     36  * to the authors.
     37  *
     38  * Issues (which can be solved with wscons, happily enough):
     39  *
     40  * o There is lots of unnecessary mucking about rasops in here, primarily
     41  *   to appease the sparc fb code.
     42  *
     43  * o RASTERCONSOLE is required.  X needs the board set up correctly, and
     44  *   that's difficult to reconcile with using the PROM for output.
     45  */
     46 
     47 #include <sys/cdefs.h>
     48 __KERNEL_RCSID(0, "$NetBSD: zx.c,v 1.28 2009/05/05 23:55:04 macallan Exp $");
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/device.h>
     53 #include <sys/ioctl.h>
     54 #include <sys/malloc.h>
     55 #include <sys/mman.h>
     56 #include <sys/tty.h>
     57 #include <sys/conf.h>
     58 #include <sys/syslog.h>
     59 #include <sys/buf.h>
     60 
     61 #include <sys/bus.h>
     62 #include <machine/autoconf.h>
     63 
     64 #include <uvm/uvm_extern.h>
     65 
     66 #include <dev/sun/fbio.h>
     67 #include <dev/sun/fbvar.h>
     68 
     69 #include "wsdisplay.h"
     70 #if NWSDISPLAY > 0
     71 #include <dev/wscons/wsconsio.h>
     72 #include <dev/wsfont/wsfont.h>
     73 #include <dev/rasops/rasops.h>
     74 #include <dev/wscons/wsdisplay_vconsvar.h>
     75 
     76 #include "opt_wsemul.h"
     77 #endif
     78 
     79 #include <dev/sbus/zxreg.h>
     80 #include <dev/sbus/zxvar.h>
     81 #include <dev/sbus/sbusvar.h>
     82 
     83 #include <dev/wscons/wsconsio.h>
     84 
     85 #if (NWSDISPLAY == 0) && !defined(RASTERCONSOLE)
     86 #error Sorry, this driver needs WSCONS or RASTERCONSOLE
     87 #endif
     88 
     89 #if (NWSDISPLAY > 0) && defined(RASTERCONSOLE)
     90 #error Sorry, RASTERCONSOLE and WSCONS are mutually exclusive
     91 #endif
     92 
     93 #define	ZX_STD_ROP	(ZX_ROP_NEW | ZX_ATTR_WE_ENABLE | \
     94     ZX_ATTR_OE_ENABLE | ZX_ATTR_FORCE_WID)
     95 
     96 static void	zx_attach(device_t, device_t, void *);
     97 static int	zx_match(device_t, struct cfdata *, void *);
     98 
     99 static void	zx_blank(device_t);
    100 static int	zx_cmap_put(struct zx_softc *);
    101 static void	zx_copyrect(struct zx_softc *, int, int, int, int, int, int);
    102 static int	zx_cross_loadwid(struct zx_softc *, u_int, u_int, u_int);
    103 static int	zx_cross_wait(struct zx_softc *);
    104 static void	zx_fillrect(struct zx_softc *, int, int, int, int, uint32_t, int);
    105 static int	zx_intr(void *);
    106 static void	zx_reset(struct zx_softc *);
    107 static void	zx_unblank(device_t);
    108 
    109 static void	zx_cursor_blank(struct zx_softc *);
    110 static void	zx_cursor_color(struct zx_softc *);
    111 static void	zx_cursor_move(struct zx_softc *);
    112 static void	zx_cursor_set(struct zx_softc *);
    113 static void	zx_cursor_unblank(struct zx_softc *);
    114 
    115 static void	zx_copycols(void *, int, int, int, int);
    116 static void	zx_copyrows(void *, int, int, int);
    117 static void	zx_do_cursor(void *, int, int, int);
    118 static void	zx_erasecols(void *, int, int, int, long);
    119 static void	zx_eraserows(void *, int, int, long);
    120 static void	zx_putchar(void *, int, int, u_int, long);
    121 
    122 struct zx_mmo {
    123 	off_t	mo_va;
    124 	off_t	mo_pa;
    125 	off_t	mo_size;
    126 } static const zx_mmo[] = {
    127 	{ ZX_FB0_VOFF,		ZX_OFF_SS0,		0x00800000 },
    128 	{ ZX_LC0_VOFF,		ZX_OFF_LC_SS0_USR,	0x00001000 },
    129 	{ ZX_LD0_VOFF,		ZX_OFF_LD_SS0,		0x00001000 },
    130 	{ ZX_LX0_CURSOR_VOFF,	ZX_OFF_LX_CURSOR,	0x00001000 },
    131 	{ ZX_FB1_VOFF,		ZX_OFF_SS1,		0x00800000 },
    132 	{ ZX_LC1_VOFF,		ZX_OFF_LC_SS1_USR,	0x00001000 },
    133 	{ ZX_LD1_VOFF,		ZX_OFF_LD_SS1,		0x00001000 },
    134 	{ ZX_LX_KRN_VOFF,	ZX_OFF_LX_CROSS,	0x00001000 },
    135 	{ ZX_LC0_KRN_VOFF,	ZX_OFF_LC_SS0_KRN,	0x00001000 },
    136 	{ ZX_LC1_KRN_VOFF,	ZX_OFF_LC_SS1_KRN,	0x00001000 },
    137 	{ ZX_LD_GBL_VOFF,	ZX_OFF_LD_GBL,		0x00001000 },
    138 };
    139 
    140 CFATTACH_DECL_NEW(zx, sizeof(struct zx_softc),
    141     zx_match, zx_attach, NULL, NULL);
    142 
    143 extern struct cfdriver zx_cd;
    144 
    145 static dev_type_open(zxopen);
    146 static dev_type_close(zxclose);
    147 static dev_type_ioctl(zxioctl);
    148 static dev_type_mmap(zxmmap);
    149 
    150 static struct fbdriver zx_fbdriver = {
    151 	zx_unblank, zxopen, zxclose, zxioctl, nopoll, zxmmap
    152 };
    153 
    154 #if NWSDISPLAY > 0
    155 struct wsscreen_descr zx_defaultscreen = {
    156 	"std",
    157 	0, 0,	/* will be filled in -- XXX shouldn't, it's global */
    158 		/* doesn't matter - you can't really have more than one leo */
    159 	NULL,		/* textops */
    160 	8, 16,	/* font width/height */
    161 	WSSCREEN_WSCOLORS,	/* capabilities */
    162 	NULL	/* modecookie */
    163 };
    164 
    165 static int 	zx_ioctl(void *, void *, u_long, void *, int, struct lwp *);
    166 static paddr_t	zx_mmap(void *, void *, off_t, int);
    167 static void	zx_init_screen(void *, struct vcons_screen *, int, long *);
    168 
    169 static int	zx_putcmap(struct zx_softc *, struct wsdisplay_cmap *);
    170 static int	zx_getcmap(struct zx_softc *, struct wsdisplay_cmap *);
    171 
    172 struct wsdisplay_accessops zx_accessops = {
    173 	zx_ioctl,
    174 	zx_mmap,
    175 	NULL,	/* alloc_screen */
    176 	NULL,	/* free_screen */
    177 	NULL,	/* show_screen */
    178 	NULL, 	/* load_font */
    179 	NULL,	/* pollc */
    180 	NULL	/* scroll */
    181 };
    182 
    183 const struct wsscreen_descr *_zx_scrlist[] = {
    184 	&zx_defaultscreen
    185 };
    186 
    187 struct wsscreen_list zx_screenlist = {
    188 	sizeof(_zx_scrlist) / sizeof(struct wsscreen_descr *),
    189 	_zx_scrlist
    190 };
    191 
    192 
    193 extern const u_char rasops_cmap[768];
    194 
    195 static struct vcons_screen zx_console_screen;
    196 #endif /* NWSDISPLAY > 0 */
    197 
    198 static int
    199 zx_match(device_t parent, struct cfdata *cf, void *aux)
    200 {
    201 	struct sbus_attach_args *sa;
    202 
    203 	sa = (struct sbus_attach_args *)aux;
    204 
    205 	return (strcmp(sa->sa_name, "SUNW,leo") == 0);
    206 }
    207 
    208 static void
    209 zx_attach(device_t parent, device_t self, void *args)
    210 {
    211 	struct zx_softc *sc;
    212 	struct sbus_attach_args *sa;
    213 	bus_space_handle_t bh;
    214 	bus_space_tag_t bt;
    215 	struct fbdevice *fb;
    216 #if NWSDISPLAY > 0
    217 	struct wsemuldisplaydev_attach_args aa;
    218 	struct rasops_info *ri = &zx_console_screen.scr_ri;
    219 	unsigned long defattr;
    220 #endif
    221 	int isconsole, width, height;
    222 
    223 	sc = device_private(self);
    224 	sc->sc_dv = self;
    225 
    226 	sa = args;
    227 	fb = &sc->sc_fb;
    228 	bt = sa->sa_bustag;
    229 	sc->sc_bt = bt;
    230 	sc->sc_paddr = sbus_bus_addr(bt, sa->sa_slot, sa->sa_offset);
    231 
    232 	if (sparc_bus_map_large(bt, sa->sa_slot, sa->sa_offset + ZX_OFF_SS0,
    233 	    0x800000, BUS_SPACE_MAP_LINEAR, &bh) != 0) {
    234 		aprint_error_dev(self, "can't map bits\n");
    235 		return;
    236 	}
    237 	fb->fb_pixels = (void *)bus_space_vaddr(bt, bh);
    238 	sc->sc_pixels = (u_int32_t *)fb->fb_pixels;
    239 
    240 	if (sbus_bus_map(bt, sa->sa_slot, sa->sa_offset + ZX_OFF_LC_SS0_USR,
    241 	    PAGE_SIZE, BUS_SPACE_MAP_LINEAR, &bh) != 0) {
    242 		aprint_error_dev(self, "can't map zc\n");
    243 		return;
    244 	}
    245 
    246 	sc->sc_bhzc = bh;
    247 
    248 	if (sbus_bus_map(bt, sa->sa_slot, sa->sa_offset + ZX_OFF_LD_SS0,
    249 	    PAGE_SIZE, BUS_SPACE_MAP_LINEAR, &bh) != 0) {
    250 		aprint_error_dev(self, "can't map ld/ss0\n");
    251 		return;
    252 	}
    253 	sc->sc_bhzdss0 = bh;
    254 
    255 	if (sbus_bus_map(bt, sa->sa_slot, sa->sa_offset + ZX_OFF_LD_SS1,
    256 	    PAGE_SIZE, BUS_SPACE_MAP_LINEAR, &bh) != 0) {
    257 		aprint_error_dev(self, "can't map ld/ss1\n");
    258 		return;
    259 	}
    260 	sc->sc_bhzdss1 = bh;
    261 
    262 	if (sbus_bus_map(bt, sa->sa_slot, sa->sa_offset + ZX_OFF_LX_CROSS,
    263 	    PAGE_SIZE, BUS_SPACE_MAP_LINEAR, &bh) != 0) {
    264 		aprint_error_dev(self, "can't map zx\n");
    265 		return;
    266 	}
    267 	sc->sc_bhzx = bh;
    268 
    269 	if (sbus_bus_map(bt, sa->sa_slot, sa->sa_offset + ZX_OFF_LX_CURSOR,
    270 	    PAGE_SIZE, BUS_SPACE_MAP_LINEAR, &bh) != 0) {
    271 		aprint_error_dev(self, "can't map zcu\n");
    272 		return;
    273 	}
    274 	sc->sc_bhzcu = bh;
    275 
    276 	fb->fb_driver = &zx_fbdriver;
    277 	fb->fb_device = sc->sc_dv;
    278 	fb->fb_flags = device_cfdata(sc->sc_dv)->cf_flags & FB_USERMASK;
    279 	fb->fb_pfour = NULL;
    280 	fb->fb_linebytes = prom_getpropint(sa->sa_node, "linebytes", 8192);
    281 
    282 	width = prom_getpropint(sa->sa_node, "width", 1280);
    283 	height = prom_getpropint(sa->sa_node, "height", 1024);
    284 	fb_setsize_obp(fb, 32, width, height, sa->sa_node);
    285 
    286 	fb->fb_type.fb_cmsize = 256;
    287 	fb->fb_type.fb_depth = 32;
    288 	fb->fb_type.fb_size = fb->fb_type.fb_height * fb->fb_linebytes;
    289 	fb->fb_type.fb_type = FBTYPE_SUNLEO;
    290 
    291 	printf(": %d x %d", fb->fb_type.fb_width, fb->fb_type.fb_height);
    292 	isconsole = fb_is_console(sa->sa_node);
    293 	if (isconsole)
    294 		printf(" (console)");
    295 	printf("\n");
    296 
    297 	sbus_establish(&sc->sc_sd, sc->sc_dv);
    298 	if (sa->sa_nintr != 0)
    299 		bus_intr_establish(bt, sa->sa_pri, IPL_NONE, zx_intr, sc);
    300 
    301 	sc->sc_cmap = malloc(768, M_DEVBUF, M_NOWAIT);
    302 	zx_reset(sc);
    303 
    304 #if NWSDISPLAY > 0
    305 	sc->sc_width = fb->fb_type.fb_width;
    306 	sc->sc_stride = 8192; /* 32 bit */
    307 	sc->sc_height = fb->fb_type.fb_height;
    308 
    309 	/* setup rasops and so on for wsdisplay */
    310 	wsfont_init();
    311 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    312 	sc->sc_bg = WS_DEFAULT_BG;
    313 
    314 	vcons_init(&sc->vd, sc, &zx_defaultscreen, &zx_accessops);
    315 	sc->vd.init_screen = zx_init_screen;
    316 
    317 	if (isconsole) {
    318 		/* we mess with zx_console_screen only once */
    319 		vcons_init_screen(&sc->vd, &zx_console_screen, 1,
    320 		    &defattr);
    321 		zx_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    322 
    323 		zx_defaultscreen.textops = &ri->ri_ops;
    324 		zx_defaultscreen.capabilities = WSSCREEN_WSCOLORS;
    325 		zx_defaultscreen.nrows = ri->ri_rows;
    326 		zx_defaultscreen.ncols = ri->ri_cols;
    327 		zx_fillrect(sc, 0, 0, width, height,
    328 		     ri->ri_devcmap[defattr >> 16], ZX_STD_ROP);
    329 		wsdisplay_cnattach(&zx_defaultscreen, ri, 0, 0, defattr);
    330 	} else {
    331 		/*
    332 		 * we're not the console so we just clear the screen and don't
    333 		 * set up any sort of text display
    334 		 */
    335 		if (zx_defaultscreen.textops == NULL) {
    336 			/*
    337 			 * ugly, but...
    338 			 * we want the console settings to win, so we only
    339 			 * touch anything when we find an untouched screen
    340 			 * definition. In this case we fill it from fb to
    341 			 * avoid problems in case no zx is the console
    342 			 */
    343 			ri = &sc->sc_fb.fb_rinfo;
    344 			zx_defaultscreen.textops = &ri->ri_ops;
    345 			zx_defaultscreen.capabilities = ri->ri_caps;
    346 			zx_defaultscreen.nrows = ri->ri_rows;
    347 			zx_defaultscreen.ncols = ri->ri_cols;
    348 		}
    349 	}
    350 
    351 	aa.scrdata = &zx_screenlist;
    352 	aa.console = isconsole;
    353 	aa.accessops = &zx_accessops;
    354 	aa.accesscookie = &sc->vd;
    355 	config_found(sc->sc_dv, &aa, wsemuldisplaydevprint);
    356 #endif
    357 	fb_attach(&sc->sc_fb, isconsole);
    358 }
    359 
    360 static int
    361 zxopen(dev_t dev, int flags, int mode, struct lwp *l)
    362 {
    363 
    364 	if (device_lookup(&zx_cd, minor(dev)) == NULL)
    365 		return (ENXIO);
    366 	return (0);
    367 }
    368 
    369 static int
    370 zxclose(dev_t dev, int flags, int mode, struct lwp *l)
    371 {
    372 	struct zx_softc *sc;
    373 
    374 	sc = device_lookup_private(&zx_cd, minor(dev));
    375 
    376 	zx_reset(sc);
    377 	zx_cursor_blank(sc);
    378 	return (0);
    379 }
    380 
    381 static int
    382 zxioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
    383 {
    384 	struct zx_softc *sc;
    385 	struct fbcmap *cm;
    386 	struct fbcursor *cu;
    387 	uint32_t curbits[2][32];
    388 	int rv, v, count, i;
    389 
    390 	sc = device_lookup_private(&zx_cd, minor(dev));
    391 
    392 	switch (cmd) {
    393 	case FBIOGTYPE:
    394 		*(struct fbtype *)data = sc->sc_fb.fb_type;
    395 		break;
    396 
    397 	case FBIOGATTR:
    398 #define fba ((struct fbgattr *)data)
    399 		fba->real_type = sc->sc_fb.fb_type.fb_type;
    400 		fba->owner = 0;		/* XXX ??? */
    401 		fba->fbtype = sc->sc_fb.fb_type;
    402 		fba->sattr.flags = 0;
    403 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
    404 		fba->sattr.dev_specific[0] = -1;
    405 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
    406 		fba->emu_types[1] = -1;
    407 		fba->emu_types[2] = -1;
    408 #undef fba
    409 		break;
    410 
    411 	case FBIOGVIDEO:
    412 		*(int *)data = ((sc->sc_flags & ZX_BLANKED) != 0);
    413 		break;
    414 
    415 	case FBIOSVIDEO:
    416 		if (*(int *)data)
    417 			zx_unblank(sc->sc_dv);
    418 		else
    419 			zx_blank(sc->sc_dv);
    420 		break;
    421 
    422 	case FBIOGETCMAP:
    423 		cm = (struct fbcmap *)data;
    424 		if (cm->index > 256 || cm->count > 256 - cm->index)
    425 			return (EINVAL);
    426 		rv = copyout(sc->sc_cmap + cm->index, cm->red, cm->count);
    427 		if (rv == 0)
    428 			rv = copyout(sc->sc_cmap + 256 + cm->index, cm->green,
    429 			    cm->count);
    430 		if (rv == 0)
    431 			rv = copyout(sc->sc_cmap + 512 + cm->index, cm->blue,
    432 			    cm->count);
    433 		return (rv);
    434 
    435 	case FBIOPUTCMAP:
    436 		cm = (struct fbcmap *)data;
    437 		if (cm->index > 256 || cm->count > 256 - cm->index)
    438 			return (EINVAL);
    439 		rv = copyin(cm->red, sc->sc_cmap + cm->index, cm->count);
    440 		if (rv == 0)
    441 			rv = copyin(cm->green, sc->sc_cmap + 256 + cm->index,
    442 			    cm->count);
    443 		if (rv == 0)
    444 			rv = copyin(cm->blue, sc->sc_cmap + 512 + cm->index,
    445 			    cm->count);
    446 		zx_cmap_put(sc);
    447 		return (rv);
    448 
    449 	case FBIOGCURPOS:
    450 		*(struct fbcurpos *)data = sc->sc_curpos;
    451 		break;
    452 
    453 	case FBIOSCURPOS:
    454 		sc->sc_curpos = *(struct fbcurpos *)data;
    455 		zx_cursor_move(sc);
    456 		break;
    457 
    458 	case FBIOGCURMAX:
    459 		((struct fbcurpos *)data)->x = 32;
    460 		((struct fbcurpos *)data)->y = 32;
    461 		break;
    462 
    463 	case FBIOSCURSOR:
    464 		cu = (struct fbcursor *)data;
    465 		v = cu->set;
    466 
    467 		if ((v & FB_CUR_SETSHAPE) != 0) {
    468 			if ((u_int)cu->size.x > 32 || (u_int)cu->size.y > 32)
    469 				return (EINVAL);
    470 			count = cu->size.y * 4;
    471 			rv = copyin(cu->mask, curbits[0], count);
    472 			if (rv)
    473 				return rv;
    474 			rv = copyin(cu->image, curbits[1], count);
    475 			if (rv)
    476 				return rv;
    477 		}
    478 		if ((v & FB_CUR_SETCUR) != 0) {
    479 			if (cu->enable)
    480 				zx_cursor_unblank(sc);
    481 			else
    482 				zx_cursor_blank(sc);
    483 		}
    484 		if ((v & (FB_CUR_SETPOS | FB_CUR_SETHOT)) != 0) {
    485 			if ((v & FB_CUR_SETPOS) != 0)
    486 				sc->sc_curpos = cu->pos;
    487 			if ((v & FB_CUR_SETHOT) != 0)
    488 				sc->sc_curhot = cu->hot;
    489 			zx_cursor_move(sc);
    490 		}
    491 		if ((v & FB_CUR_SETCMAP) != 0) {
    492 			if (cu->cmap.index > 2 ||
    493 			    cu->cmap.count > 2 - cu->cmap.index)
    494 				return (EINVAL);
    495 			for (i = 0; i < cu->cmap.count; i++) {
    496 				if ((v = fubyte(&cu->cmap.red[i])) < 0)
    497 					return (EFAULT);
    498 				sc->sc_curcmap[i + cu->cmap.index + 0] = v;
    499 				if ((v = fubyte(&cu->cmap.green[i])) < 0)
    500 					return (EFAULT);
    501 				sc->sc_curcmap[i + cu->cmap.index + 2] = v;
    502 				if ((v = fubyte(&cu->cmap.blue[i])) < 0)
    503 					return (EFAULT);
    504 				sc->sc_curcmap[i + cu->cmap.index + 4] = v;
    505 			}
    506 			zx_cursor_color(sc);
    507 		}
    508 		if ((v & FB_CUR_SETSHAPE) != 0) {
    509 			sc->sc_cursize = cu->size;
    510 			count = cu->size.y * 4;
    511 			memset(sc->sc_curbits, 0, sizeof(sc->sc_curbits));
    512 			memcpy(sc->sc_curbits[0], curbits[0], count);
    513 			memcpy(sc->sc_curbits[1], curbits[1], count);
    514 			zx_cursor_set(sc);
    515 		}
    516 		break;
    517 
    518 	case FBIOGCURSOR:
    519 		cu = (struct fbcursor *)data;
    520 
    521 		cu->set = FB_CUR_SETALL;
    522 		cu->enable = ((sc->sc_flags & ZX_CURSOR) != 0);
    523 		cu->pos = sc->sc_curpos;
    524 		cu->hot = sc->sc_curhot;
    525 		cu->size = sc->sc_cursize;
    526 
    527 		if (cu->image != NULL) {
    528 			count = sc->sc_cursize.y * 4;
    529 			rv = copyout(sc->sc_curbits[1], cu->image, count);
    530 			if (rv)
    531 				return (rv);
    532 			rv = copyout(sc->sc_curbits[0], cu->mask, count);
    533 			if (rv)
    534 				return (rv);
    535 		}
    536 		if (cu->cmap.red != NULL) {
    537 			if (cu->cmap.index > 2 ||
    538 			    cu->cmap.count > 2 - cu->cmap.index)
    539 				return (EINVAL);
    540 			for (i = 0; i < cu->cmap.count; i++) {
    541 				v = sc->sc_curcmap[i + cu->cmap.index + 0];
    542 				if (subyte(&cu->cmap.red[i], v))
    543 					return (EFAULT);
    544 				v = sc->sc_curcmap[i + cu->cmap.index + 2];
    545 				if (subyte(&cu->cmap.green[i], v))
    546 					return (EFAULT);
    547 				v = sc->sc_curcmap[i + cu->cmap.index + 4];
    548 				if (subyte(&cu->cmap.blue[i], v))
    549 					return (EFAULT);
    550 			}
    551 		} else {
    552 			cu->cmap.index = 0;
    553 			cu->cmap.count = 2;
    554 		}
    555 		break;
    556 
    557 	default:
    558 #ifdef DEBUG
    559 		log(LOG_NOTICE, "zxioctl(0x%lx) (%s[%d])\n", cmd,
    560 		    l->l_proc->p_comm, l->l_proc->p_pid);
    561 #endif
    562 		return (ENOTTY);
    563 	}
    564 
    565 	return (0);
    566 }
    567 
    568 static int
    569 zx_intr(void *cookie)
    570 {
    571 
    572 	return (1);
    573 }
    574 
    575 static void
    576 zx_reset(struct zx_softc *sc)
    577 {
    578 	struct fbtype *fbt;
    579 	u_int i;
    580 
    581 	fbt = &sc->sc_fb.fb_type;
    582 
    583 	zx_cross_loadwid(sc, ZX_WID_DBL_8, 0, 0x2c0);
    584 	zx_cross_loadwid(sc, ZX_WID_DBL_8, 1, 0x30);
    585 	zx_cross_loadwid(sc, ZX_WID_DBL_8, 2, 0x20);
    586 	zx_cross_loadwid(sc, ZX_WID_DBL_24, 1, 0x30);
    587 
    588 	i = bus_space_read_4(sc->sc_bt, sc->sc_bhzdss1, zd_misc);
    589 	i |= ZX_SS1_MISC_ENABLE;
    590 	bus_space_write_4(sc->sc_bt, sc->sc_bhzdss1, zd_misc, i);
    591 
    592 	bus_space_write_4(sc->sc_bt, sc->sc_bhzdss0, zd_wid, 1);
    593 	bus_space_write_4(sc->sc_bt, sc->sc_bhzdss0, zd_widclip, 0);
    594 	bus_space_write_4(sc->sc_bt, sc->sc_bhzdss0, zd_wmask, 0xffff);
    595 	bus_space_write_4(sc->sc_bt, sc->sc_bhzdss0, zd_vclipmin, 0);
    596 	bus_space_write_4(sc->sc_bt, sc->sc_bhzdss0, zd_vclipmax,
    597 	    (fbt->fb_width - 1) | ((fbt->fb_height - 1) << 16));
    598 	bus_space_write_4(sc->sc_bt, sc->sc_bhzdss0, zd_fg, 0);
    599 	bus_space_write_4(sc->sc_bt, sc->sc_bhzdss0, zd_planemask, 0xffffffff);
    600 	bus_space_write_4(sc->sc_bt, sc->sc_bhzdss0, zd_rop, ZX_STD_ROP);
    601 
    602 	bus_space_write_4(sc->sc_bt, sc->sc_bhzc, zc_extent,
    603 	    (fbt->fb_width - 1) | ((fbt->fb_height - 1) << 11));
    604 	bus_space_write_4(sc->sc_bt, sc->sc_bhzc, zc_addrspace,
    605 	    ZX_ADDRSPC_FONT_OBGR);
    606 	bus_space_write_4(sc->sc_bt, sc->sc_bhzc, zc_fontt, 0);
    607 
    608 	for (i = 0; i < 256; i++) {
    609 		sc->sc_cmap[i] = rasops_cmap[i * 3];
    610 		sc->sc_cmap[i + 256] = rasops_cmap[i * 3 + 1];
    611 		sc->sc_cmap[i + 512] = rasops_cmap[i * 3 + 2];
    612 	}
    613 
    614 	zx_cmap_put(sc);
    615 }
    616 
    617 static int
    618 zx_cross_wait(struct zx_softc *sc)
    619 {
    620 	int i;
    621 
    622 	for (i = 300000; i != 0; i--) {
    623 		if ((bus_space_read_4(sc->sc_bt, sc->sc_bhzx, zx_csr) &
    624 		    ZX_CROSS_CSR_PROGRESS) == 0)
    625 			break;
    626 		DELAY(1);
    627 	}
    628 
    629 	if (i == 0)
    630 		printf("zx_cross_wait: timed out\n");
    631 
    632 	return (i);
    633 }
    634 
    635 static int
    636 zx_cross_loadwid(struct zx_softc *sc, u_int type, u_int index, u_int value)
    637 {
    638 	u_int tmp = 0;
    639 
    640 	bus_space_write_4(sc->sc_bt, sc->sc_bhzx, zx_type, ZX_CROSS_TYPE_WID);
    641 
    642 	if (zx_cross_wait(sc))
    643 		return (1);
    644 
    645 	if (type == ZX_WID_DBL_8)
    646 		tmp = (index & 0x0f) + 0x40;
    647 	else if (type == ZX_WID_DBL_24)
    648 		tmp = index & 0x3f;
    649 
    650 	bus_space_write_4(sc->sc_bt, sc->sc_bhzx, zx_type, 0x5800 + tmp);
    651 	bus_space_write_4(sc->sc_bt, sc->sc_bhzx, zx_value, value);
    652 	bus_space_write_4(sc->sc_bt, sc->sc_bhzx, zx_type, ZX_CROSS_TYPE_WID);
    653 	bus_space_write_4(sc->sc_bt, sc->sc_bhzx, zx_csr,
    654 	    ZX_CROSS_CSR_UNK | ZX_CROSS_CSR_UNK2);
    655 
    656 	return (0);
    657 }
    658 
    659 static int
    660 zx_cmap_put(struct zx_softc *sc)
    661 {
    662 	const u_char *b;
    663 	u_int i, t;
    664 
    665 	bus_space_write_4(sc->sc_bt, sc->sc_bhzx, zx_type, ZX_CROSS_TYPE_CLUT0);
    666 
    667 	zx_cross_wait(sc);
    668 
    669 	bus_space_write_4(sc->sc_bt, sc->sc_bhzx, zx_type,
    670 	    ZX_CROSS_TYPE_CLUTDATA);
    671 
    672 	for (i = 0, b = sc->sc_cmap; i < 256; i++) {
    673 		t = b[i];
    674 		t |= b[i + 256] << 8;
    675 		t |= b[i + 512] << 16;
    676 		bus_space_write_4(sc->sc_bt, sc->sc_bhzx, zx_value, t);
    677 	}
    678 
    679 	bus_space_write_4(sc->sc_bt, sc->sc_bhzx, zx_type, ZX_CROSS_TYPE_CLUT0);
    680 	i = bus_space_read_4(sc->sc_bt, sc->sc_bhzx, zx_csr);
    681 	i = i | ZX_CROSS_CSR_UNK | ZX_CROSS_CSR_UNK2;
    682 	bus_space_write_4(sc->sc_bt, sc->sc_bhzx, zx_csr, i);
    683 	return (0);
    684 }
    685 
    686 static void
    687 zx_cursor_move(struct zx_softc *sc)
    688 {
    689 	int sx, sy, x, y;
    690 
    691 	x = sc->sc_curpos.x - sc->sc_curhot.x;
    692 	y = sc->sc_curpos.y - sc->sc_curhot.y;
    693 
    694 	if (x < 0) {
    695 		sx = min(-x, 32);
    696 		x = 0;
    697 	} else
    698 		sx = 0;
    699 
    700 	if (y < 0) {
    701 		sy = min(-y, 32);
    702 		y = 0;
    703 	} else
    704 		sy = 0;
    705 
    706 	if (sx != sc->sc_shiftx || sy != sc->sc_shifty) {
    707 		sc->sc_shiftx = sx;
    708 		sc->sc_shifty = sy;
    709 		zx_cursor_set(sc);
    710 	}
    711 
    712 	bus_space_write_4(sc->sc_bt, sc->sc_bhzcu, zcu_sxy,
    713 	    ((y & 0x7ff) << 11) | (x & 0x7ff));
    714 	bus_space_write_4(sc->sc_bt, sc->sc_bhzcu, zcu_misc,
    715 	    bus_space_read_4(sc->sc_bt, sc->sc_bhzcu, zcu_misc) | 0x30);
    716 
    717 	/* XXX Necessary? */
    718 	bus_space_write_4(sc->sc_bt, sc->sc_bhzcu, zcu_misc,
    719 	    bus_space_read_4(sc->sc_bt, sc->sc_bhzcu, zcu_misc) | 0x80);
    720 }
    721 
    722 static void
    723 zx_cursor_set(struct zx_softc *sc)
    724 {
    725 	int i, j, data;
    726 
    727 	if ((sc->sc_flags & ZX_CURSOR) != 0)
    728 		bus_space_write_4(sc->sc_bt, sc->sc_bhzcu, zcu_misc,
    729 		    bus_space_read_4(sc->sc_bt, sc->sc_bhzcu, zcu_misc) &
    730 		    ~0x80);
    731 
    732 	for (j = 0; j < 2; j++) {
    733 		bus_space_write_4(sc->sc_bt, sc->sc_bhzcu, zcu_type, 0x20 << j);
    734 
    735 		for (i = sc->sc_shifty; i < 32; i++) {
    736 			data = sc->sc_curbits[j][i];
    737 			bus_space_write_4(sc->sc_bt, sc->sc_bhzcu, zcu_data,
    738 			    data >> sc->sc_shiftx);
    739 		}
    740 		for (i = sc->sc_shifty; i != 0; i--)
    741 			bus_space_write_4(sc->sc_bt, sc->sc_bhzcu, zcu_data, 0);
    742 	}
    743 
    744 	if ((sc->sc_flags & ZX_CURSOR) != 0)
    745 		bus_space_write_4(sc->sc_bt, sc->sc_bhzcu, zcu_misc,
    746 		    bus_space_read_4(sc->sc_bt, sc->sc_bhzcu, zcu_misc) | 0x80);
    747 }
    748 
    749 static void
    750 zx_cursor_blank(struct zx_softc *sc)
    751 {
    752 
    753 	sc->sc_flags &= ~ZX_CURSOR;
    754 	bus_space_write_4(sc->sc_bt, sc->sc_bhzcu, zcu_misc,
    755 	    bus_space_read_4(sc->sc_bt, sc->sc_bhzcu, zcu_misc) & ~0x80);
    756 }
    757 
    758 static void
    759 zx_cursor_unblank(struct zx_softc *sc)
    760 {
    761 
    762 	sc->sc_flags |= ZX_CURSOR;
    763 	bus_space_write_4(sc->sc_bt, sc->sc_bhzcu, zcu_misc,
    764 	    bus_space_read_4(sc->sc_bt, sc->sc_bhzcu, zcu_misc) | 0x80);
    765 }
    766 
    767 static void
    768 zx_cursor_color(struct zx_softc *sc)
    769 {
    770 	u_int8_t tmp;
    771 
    772 	bus_space_write_4(sc->sc_bt, sc->sc_bhzcu, zcu_type, 0x50);
    773 
    774 	tmp = sc->sc_curcmap[0] | (sc->sc_curcmap[2] << 8) |
    775 	    (sc->sc_curcmap[4] << 16);
    776 	bus_space_write_4(sc->sc_bt, sc->sc_bhzcu, zcu_data, tmp);
    777 
    778 	tmp = sc->sc_curcmap[1] | (sc->sc_curcmap[3] << 8) |
    779 	    (sc->sc_curcmap[5] << 16);
    780 	bus_space_write_4(sc->sc_bt, sc->sc_bhzcu, zcu_data, sc->sc_curcmap[1]);
    781 
    782 	bus_space_write_4(sc->sc_bt, sc->sc_bhzcu, zcu_misc,
    783 	    bus_space_read_4(sc->sc_bt, sc->sc_bhzcu, zcu_misc) | 0x03);
    784 }
    785 
    786 static void
    787 zx_blank(device_t dv)
    788 {
    789 	struct zx_softc *sc;
    790 
    791 	sc = device_private(dv);
    792 
    793 	if ((sc->sc_flags & ZX_BLANKED) != 0)
    794 		return;
    795 	sc->sc_flags |= ZX_BLANKED;
    796 
    797 	bus_space_write_4(sc->sc_bt, sc->sc_bhzx, zx_type, ZX_CROSS_TYPE_VIDEO);
    798 	bus_space_write_4(sc->sc_bt, sc->sc_bhzx, zx_csr,
    799 	    bus_space_read_4(sc->sc_bt, sc->sc_bhzx, zx_csr) &
    800 	    ~ZX_CROSS_CSR_ENABLE);
    801 }
    802 
    803 static void
    804 zx_unblank(device_t dv)
    805 {
    806 	struct zx_softc *sc;
    807 
    808 	sc = device_private(dv);
    809 
    810 	if ((sc->sc_flags & ZX_BLANKED) == 0)
    811 		return;
    812 	sc->sc_flags &= ~ZX_BLANKED;
    813 
    814 	bus_space_write_4(sc->sc_bt, sc->sc_bhzx, zx_type, ZX_CROSS_TYPE_VIDEO);
    815 	bus_space_write_4(sc->sc_bt, sc->sc_bhzx, zx_csr,
    816 	    bus_space_read_4(sc->sc_bt, sc->sc_bhzx, zx_csr) |
    817 	    ZX_CROSS_CSR_ENABLE);
    818 }
    819 
    820 static paddr_t
    821 zxmmap(dev_t dev, off_t off, int prot)
    822 {
    823 	struct zx_softc *sc;
    824 	const struct zx_mmo *mm, *mmmax;
    825 
    826 	sc = device_lookup_private(&zx_cd, minor(dev));
    827 	off = trunc_page(off);
    828 	mm = zx_mmo;
    829 	mmmax = mm + sizeof(zx_mmo) / sizeof(zx_mmo[0]);
    830 
    831 	for (; mm < mmmax; mm++)
    832 		if (off >= mm->mo_va && off < mm->mo_va + mm->mo_size) {
    833 			off = off - mm->mo_va + mm->mo_pa;
    834 			return (bus_space_mmap(sc->sc_bt, sc->sc_paddr,
    835 			    off, prot, BUS_SPACE_MAP_LINEAR));
    836 		}
    837 
    838 	return (-1);
    839 }
    840 
    841 static void
    842 zx_fillrect(struct zx_softc *sc, int x, int y, int w, int h, uint32_t bg,
    843 	    int rop)
    844 {
    845 
    846 
    847 	while ((bus_space_read_4(sc->sc_bt, sc->sc_bhzc, zc_csr) &
    848 	    ZX_CSR_BLT_BUSY) != 0)
    849 		;
    850 
    851 	bus_space_write_4(sc->sc_bt, sc->sc_bhzdss0, zd_rop, rop);
    852 	bus_space_write_4(sc->sc_bt, sc->sc_bhzdss0, zd_fg, bg);
    853 	bus_space_write_4(sc->sc_bt, sc->sc_bhzc, zc_extent, w | (h << 11));
    854 	bus_space_write_4(sc->sc_bt, sc->sc_bhzc, zc_fill,
    855 	    x | (y << 11) | 0x80000000);
    856 }
    857 
    858 static void
    859 zx_copyrect(struct zx_softc *sc, int sx, int sy, int dx, int dy, int w,
    860 	    int h)
    861 {
    862 	uint32_t dir;
    863 
    864 	if (sy < dy || sx < dx) {
    865 		dir = 0x80000000;
    866 		sx += w;
    867 		sy += h;
    868 		dx += w;
    869 		dy += h;
    870 	} else
    871 		dir = 0;
    872 
    873 	while ((bus_space_read_4(sc->sc_bt, sc->sc_bhzc, zc_csr) &
    874 	    ZX_CSR_BLT_BUSY) != 0)
    875 		;
    876 
    877 	bus_space_write_4(sc->sc_bt, sc->sc_bhzdss0, zd_rop, ZX_STD_ROP);
    878 	bus_space_write_4(sc->sc_bt, sc->sc_bhzc, zc_extent,
    879 	    w | (h << 11) | dir);
    880 	bus_space_write_4(sc->sc_bt, sc->sc_bhzc, zc_src, sx | (sy << 11));
    881 	bus_space_write_4(sc->sc_bt, sc->sc_bhzc, zc_copy, dx | (dy << 11));
    882 }
    883 
    884 static void
    885 zx_do_cursor(void *cookie, int on, int row, int col)
    886 {
    887 	struct rasops_info *ri = cookie;
    888 	struct vcons_screen *scr = ri->ri_hw;
    889 	struct zx_softc *sc = scr->scr_cookie;
    890 	int x, y, wi, he;
    891 
    892 	wi = ri->ri_font->fontwidth;
    893 	he = ri->ri_font->fontheight;
    894 
    895 	if (ri->ri_flg & RI_CURSOR) {
    896 		x = ri->ri_ccol * wi + ri->ri_xorigin;
    897 		y = ri->ri_crow * he + ri->ri_yorigin;
    898 		zx_fillrect(sc, x, y, wi, he, 0xff000000,
    899 		  ZX_ROP_NEW_XOR_OLD | ZX_ATTR_WE_ENABLE | ZX_ATTR_OE_ENABLE |
    900 		  ZX_ATTR_FORCE_WID);
    901 		ri->ri_flg &= ~RI_CURSOR;
    902 	}
    903 
    904 	ri->ri_crow = row;
    905 	ri->ri_ccol = col;
    906 
    907 	if (on)
    908 	{
    909 		x = ri->ri_ccol * wi + ri->ri_xorigin;
    910 		y = ri->ri_crow * he + ri->ri_yorigin;
    911 		zx_fillrect(sc, x, y, wi, he, 0xff000000,
    912 		  ZX_ROP_NEW_XOR_OLD | ZX_ATTR_WE_ENABLE | ZX_ATTR_OE_ENABLE |
    913 		  ZX_ATTR_FORCE_WID);
    914 		ri->ri_flg |= RI_CURSOR;
    915 	}
    916 }
    917 
    918 static void
    919 zx_erasecols(void *cookie, int row, int startcol, int ncols, long attr)
    920 {
    921 	struct rasops_info *ri = cookie;
    922 	struct vcons_screen *scr = ri->ri_hw;
    923 	struct zx_softc *sc = scr->scr_cookie;
    924 	int32_t x, y, width, height, bg;
    925 
    926 	x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
    927 	y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    928 	width = ri->ri_font->fontwidth * ncols;
    929 	height = ri->ri_font->fontheight;
    930 	bg = ((uint32_t)ri->ri_devcmap[(attr >> 16) & 0xff]) << 24;
    931 	zx_fillrect(sc, x, y, width, height, bg, ZX_STD_ROP);
    932 }
    933 
    934 static void
    935 zx_eraserows(void *cookie, int row, int nrows, long attr)
    936 {
    937 	struct rasops_info *ri = cookie;
    938 	struct vcons_screen *scr = ri->ri_hw;
    939 	struct zx_softc *sc = scr->scr_cookie;
    940 	int32_t x, y, width, height, bg;
    941 
    942 	if ((row == 0) && (nrows == ri->ri_rows)) {
    943 		x = y = 0;
    944 		width = ri->ri_width;
    945 		height = ri->ri_height;
    946 	} else {
    947 		x = ri->ri_xorigin;
    948 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    949 		width = ri->ri_emuwidth;
    950 		height = ri->ri_font->fontheight * nrows;
    951 	}
    952 	bg = ((uint32_t)ri->ri_devcmap[(attr >> 16) & 0xff]) << 24;
    953 	zx_fillrect(sc, x, y, width, height, bg, ZX_STD_ROP);
    954 }
    955 
    956 static void
    957 zx_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
    958 {
    959 	struct rasops_info *ri = cookie;
    960 	struct vcons_screen *scr = ri->ri_hw;
    961 	struct zx_softc *sc = scr->scr_cookie;
    962 	int32_t x, ys, yd, width, height;
    963 
    964 	x = ri->ri_xorigin;
    965 	ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
    966 	yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
    967 	width = ri->ri_emuwidth;
    968 	height = ri->ri_font->fontheight * nrows;
    969 	zx_copyrect(sc, x, ys, x, yd, width, height);
    970 }
    971 
    972 static void
    973 zx_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
    974 {
    975 	struct rasops_info *ri = cookie;
    976 	struct vcons_screen *scr = ri->ri_hw;
    977 	struct zx_softc *sc = scr->scr_cookie;
    978 	int32_t xs, xd, y, width, height;
    979 
    980 	xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
    981 	xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
    982 	y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    983 	width = ri->ri_font->fontwidth * ncols;
    984 	height = ri->ri_font->fontheight;
    985 	zx_copyrect(sc, xs, y, xd, y, width, height);
    986 }
    987 
    988 static void
    989 zx_putchar(void *cookie, int row, int col, u_int uc, long attr)
    990 {
    991 	struct rasops_info *ri = cookie;
    992 	struct vcons_screen *scr = ri->ri_hw;
    993 	struct zx_softc *sc = scr->scr_cookie;
    994 	struct wsdisplay_font *font;
    995 	volatile u_int32_t *dp;
    996 	u_int8_t *fb;
    997 	int fs, i, ul;
    998 	uint32_t fg, bg;
    999 
   1000 	rasops_unpack_attr(attr, &fg, &bg, &ul);
   1001 	bg = ((uint32_t)ri->ri_devcmap[bg]) << 24;
   1002 	fg = ((uint32_t)ri->ri_devcmap[fg]) << 24;
   1003 	if (uc == ' ') {
   1004 		int x, y;
   1005 
   1006 		x = ri->ri_xorigin + ri->ri_font->fontwidth * col;
   1007 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1008 		zx_fillrect(sc, x, y, ri->ri_font->fontwidth,
   1009 			    ri->ri_font->fontheight, bg, ZX_STD_ROP);
   1010 		return;
   1011 	}
   1012 
   1013 	font = ri->ri_font;
   1014 
   1015 	dp = (volatile u_int32_t *)sc->sc_pixels +
   1016 	    ((row * font->fontheight + ri->ri_yorigin) << 11) +
   1017 	    (col * font->fontwidth + ri->ri_xorigin);
   1018 	fb = (u_int8_t *)font->data + (uc - font->firstchar) *
   1019 	    ri->ri_fontscale;
   1020 	fs = font->stride;
   1021 
   1022 	while ((bus_space_read_4(sc->sc_bt, sc->sc_bhzc, zc_csr) &
   1023 	    ZX_CSR_BLT_BUSY) != 0)
   1024 		;
   1025 
   1026 	bus_space_write_4(sc->sc_bt, sc->sc_bhzdss0, zd_rop, ZX_STD_ROP);
   1027 	bus_space_write_4(sc->sc_bt, sc->sc_bhzdss0, zd_fg, fg);
   1028 	bus_space_write_4(sc->sc_bt, sc->sc_bhzdss0, zd_bg, bg);
   1029 	bus_space_write_4(sc->sc_bt, sc->sc_bhzc, zc_fontmsk,
   1030 	    0xffffffff << (32 - font->fontwidth));
   1031 
   1032 	if (font->fontwidth <= 8) {
   1033 		for (i = font->fontheight; i != 0; i--, dp += 2048) {
   1034 			*dp = *fb << 24;
   1035 			fb += fs;
   1036 		}
   1037 	} else {
   1038 		for (i = font->fontheight; i != 0; i--, dp += 2048) {
   1039 			*dp = *((u_int16_t *)fb) << 16;
   1040 			fb += fs;
   1041 		}
   1042 	}
   1043 
   1044 	if (ul) {
   1045 		dp -= 4096;
   1046 		*dp = 0xffffffff;
   1047 	}
   1048 }
   1049 
   1050 #if NWSDISPLAY > 0
   1051 static int
   1052 zx_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
   1053 	struct lwp *l)
   1054 {
   1055 	/* we'll probably need to add more stuff here */
   1056 	struct vcons_data *vd = v;
   1057 	struct zx_softc *sc = vd->cookie;
   1058 	struct wsdisplay_fbinfo *wdf;
   1059 	struct rasops_info *ri = &sc->sc_fb.fb_rinfo;
   1060 	struct vcons_screen *ms = sc->vd.active;
   1061 	switch (cmd) {
   1062 		case WSDISPLAYIO_GTYPE:
   1063 			*(u_int *)data = WSDISPLAY_TYPE_SUNTCX;
   1064 			return 0;
   1065 		case WSDISPLAYIO_GINFO:
   1066 			wdf = (void *)data;
   1067 			wdf->height = ri->ri_height;
   1068 			wdf->width = ri->ri_width;
   1069 			wdf->depth = ri->ri_depth;
   1070 			wdf->cmsize = 256;
   1071 			return 0;
   1072 
   1073 		case WSDISPLAYIO_GETCMAP:
   1074 			return zx_getcmap(sc,
   1075 			    (struct wsdisplay_cmap *)data);
   1076 		case WSDISPLAYIO_PUTCMAP:
   1077 			return zx_putcmap(sc,
   1078 			    (struct wsdisplay_cmap *)data);
   1079 
   1080 		case WSDISPLAYIO_SMODE:
   1081 			{
   1082 				int new_mode = *(int*)data;
   1083 				if (new_mode != sc->sc_mode)
   1084 				{
   1085 					sc->sc_mode = new_mode;
   1086 					if(new_mode == WSDISPLAYIO_MODE_EMUL)
   1087 					{
   1088 						vcons_redraw_screen(ms);
   1089 					}
   1090 				}
   1091 			}
   1092 	}
   1093 	return EPASSTHROUGH;
   1094 }
   1095 
   1096 static paddr_t
   1097 zx_mmap(void *v, void *vs, off_t offset, int prot)
   1098 {
   1099 	/* I'm not at all sure this is the right thing to do */
   1100 	return zxmmap(0, offset, prot); /* assume minor dev 0 for now */
   1101 }
   1102 
   1103 static int
   1104 zx_putcmap(struct zx_softc *sc, struct wsdisplay_cmap *cm)
   1105 {
   1106 	u_int index = cm->index;
   1107 	u_int count = cm->count;
   1108 	int error,i;
   1109 	if (index >= 256 || count > 256 || index + count > 256)
   1110 		return EINVAL;
   1111 
   1112 	for (i = 0; i < count; i++)
   1113 	{
   1114 		error = copyin(&cm->red[i],
   1115 		    &sc->sc_cmap[index + i], 1);
   1116 		if (error)
   1117 			return error;
   1118 		error = copyin(&cm->green[i],
   1119 		    &sc->sc_cmap[index + i + 256], 1);
   1120 		if (error)
   1121 			return error;
   1122 		error = copyin(&cm->blue[i],
   1123 		    &sc->sc_cmap[index + i + 512], 1);
   1124 		if (error)
   1125 			return error;
   1126 	}
   1127 	zx_cmap_put(sc);
   1128 
   1129 	return 0;
   1130 }
   1131 
   1132 static int
   1133 zx_getcmap(struct zx_softc *sc, struct wsdisplay_cmap *cm)
   1134 {
   1135 	u_int index = cm->index;
   1136 	u_int count = cm->count;
   1137 	int error,i;
   1138 
   1139 	if (index >= 256 || count > 256 || index + count > 256)
   1140 		return EINVAL;
   1141 
   1142 	for (i = 0; i < count; i++)
   1143 	{
   1144 		error = copyout(&sc->sc_cmap[index + i],
   1145 		    &cm->red[i], 1);
   1146 		if (error)
   1147 			return error;
   1148 		error = copyout(&sc->sc_cmap[index + i + 256],
   1149 		    &cm->green[i], 1);
   1150 		if (error)
   1151 			return error;
   1152 		error = copyout(&sc->sc_cmap[index + i + 256],
   1153 		    &cm->blue[i], 1);
   1154 		if (error)
   1155 			return error;
   1156 	}
   1157 
   1158 	return 0;
   1159 }
   1160 
   1161 static void
   1162 zx_init_screen(void *cookie, struct vcons_screen *scr,
   1163     int existing, long *defattr)
   1164 {
   1165 	struct zx_softc *sc = cookie;
   1166 	struct rasops_info *ri = &scr->scr_ri;
   1167 
   1168 	ri->ri_depth = 8; /*sc->sc_fb.fb_type.fb_depth = 32;*/
   1169 	ri->ri_width = sc->sc_width;
   1170 	ri->ri_height = sc->sc_height;
   1171 	ri->ri_stride = sc->sc_stride;
   1172 	ri->ri_flg = RI_CENTER;
   1173 
   1174 	ri->ri_bits = (void *)sc->sc_pixels;
   1175 
   1176 	rasops_init(ri, sc->sc_height/8, sc->sc_width/8);
   1177 	ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_REVERSE;
   1178 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
   1179 		    sc->sc_width / ri->ri_font->fontwidth);
   1180 
   1181 	ri->ri_hw = scr;
   1182 
   1183 	ri->ri_ops.cursor = zx_do_cursor;
   1184 	ri->ri_ops.copycols = zx_copycols;
   1185 	ri->ri_ops.copyrows = zx_copyrows;
   1186 	ri->ri_ops.erasecols = zx_erasecols;
   1187 	ri->ri_ops.eraserows = zx_eraserows;
   1188 	ri->ri_ops.putchar = zx_putchar;
   1189 }
   1190 
   1191 #endif /* NWSDISPLAY > 0 */
   1192