Home | History | Annotate | Line # | Download | only in sbus
zx.c revision 1.5
      1 /*	$NetBSD: zx.c,v 1.5 2002/10/02 16:52:46 thorpej 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  *  3. All advertising materials mentioning features or use of this software
     19  *     must display the following acknowledgement:
     20  *         This product includes software developed by the NetBSD
     21  *         Foundation, Inc. and its contributors.
     22  *  4. Neither the name of The NetBSD Foundation nor the names of its
     23  *     contributors may be used to endorse or promote products derived
     24  *     from this software without specific prior written permission.
     25  *
     26  *  THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  *  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  *  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  *  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  *  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  *  POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Driver for the Sun ZX display adapter.  This would be called 'leo', but
     41  * NetBSD/amiga already has a driver by that name.  The XFree86 and Linux
     42  * drivers were used as "living documentation" when writing this; thanks
     43  * to the authors.
     44  *
     45  * Issues (which can be solved with wscons, happily enough):
     46  *
     47  * o There is lots of unnecessary mucking about rasops in here, primarily
     48  *   to appease the sparc fb code.
     49  *
     50  * o RASTERCONSOLE is required.  X needs the board set up correctly, and
     51  *   that's difficult to reconcile with using the PROM for output.
     52  */
     53 
     54 #include <sys/cdefs.h>
     55 __KERNEL_RCSID(0, "$NetBSD: zx.c,v 1.5 2002/10/02 16:52:46 thorpej Exp $");
     56 
     57 #include <sys/param.h>
     58 #include <sys/systm.h>
     59 #include <sys/device.h>
     60 #include <sys/ioctl.h>
     61 #include <sys/malloc.h>
     62 #include <sys/mman.h>
     63 #include <sys/tty.h>
     64 #include <sys/conf.h>
     65 #include <sys/syslog.h>
     66 #include <sys/buf.h>
     67 
     68 #include <machine/bus.h>
     69 #include <machine/autoconf.h>
     70 
     71 #include <uvm/uvm_extern.h>
     72 
     73 #include <dev/sun/fbio.h>
     74 #include <dev/sun/fbvar.h>
     75 
     76 #include <dev/sbus/zxreg.h>
     77 #include <dev/sbus/zxvar.h>
     78 #include <dev/sbus/sbusvar.h>
     79 
     80 #include <dev/wscons/wsconsio.h>
     81 
     82 #ifndef RASTERCONSOLE
     83 #error Sorry, this driver needs the RASTERCONSOLE option
     84 #endif
     85 
     86 /* Force 32-bit writes. */
     87 #define	SETREG(r, v)	(*((volatile u_int32_t *)&r) = (v))
     88 
     89 #define	ZX_STD_ROP	(ZX_ROP_NEW | ZX_ATTR_WE_ENABLE | \
     90     ZX_ATTR_OE_ENABLE | ZX_ATTR_FORCE_WID)
     91 
     92 void	zx_attach(struct device *, struct device *, void *);
     93 int	zx_match(struct device *, struct cfdata *, void *);
     94 
     95 void	zx_blank(struct device *);
     96 int	zx_cmap_put(struct zx_softc *);
     97 void	zx_copyrect(struct rasops_info *, int, int, int, int, int, int);
     98 int	zx_cross_loadwid(struct zx_softc *, u_int, u_int, u_int);
     99 int	zx_cross_wait(struct zx_softc *);
    100 void	zx_fillrect(struct rasops_info *, int, int, int, int, long, int);
    101 int	zx_intr(void *);
    102 void	zx_reset(struct zx_softc *);
    103 void	zx_unblank(struct device *);
    104 
    105 void	zx_cursor_blank(struct zx_softc *);
    106 void	zx_cursor_color(struct zx_softc *);
    107 void	zx_cursor_move(struct zx_softc *);
    108 void	zx_cursor_set(struct zx_softc *);
    109 void	zx_cursor_unblank(struct zx_softc *);
    110 
    111 void	zx_copycols(void *, int, int, int, int);
    112 void	zx_copyrows(void *, int, int, int);
    113 void	zx_cursor(void *, int, int, int);
    114 void	zx_do_cursor(struct rasops_info *);
    115 void	zx_erasecols(void *, int, int, int, long);
    116 void	zx_eraserows(void *, int, int, long);
    117 void	zx_putchar(void *, int, int, u_int, long);
    118 
    119 struct zx_mmo {
    120 	off_t	mo_va;
    121 	off_t	mo_pa;
    122 	off_t	mo_size;
    123 } static const zx_mmo[] = {
    124 	{ ZX_FB0_VOFF,		ZX_OFF_SS0,		0x00800000 },
    125 	{ ZX_LC0_VOFF,		ZX_OFF_LC_SS0_USR,	0x00001000 },
    126 	{ ZX_LD0_VOFF,		ZX_OFF_LD_SS0,		0x00001000 },
    127 	{ ZX_LX0_CURSOR_VOFF,	ZX_OFF_LX_CURSOR,	0x00001000 },
    128 	{ ZX_FB1_VOFF,		ZX_OFF_SS1,		0x00800000 },
    129 	{ ZX_LC1_VOFF,		ZX_OFF_LC_SS1_USR,	0x00001000 },
    130 	{ ZX_LD1_VOFF,		ZX_OFF_LD_SS1,		0x00001000 },
    131 	{ ZX_LX_KRN_VOFF,	ZX_OFF_LX_CROSS,	0x00001000 },
    132 	{ ZX_LC0_KRN_VOFF,	ZX_OFF_LC_SS0_KRN,	0x00001000 },
    133 	{ ZX_LC1_KRN_VOFF,	ZX_OFF_LC_SS1_KRN,	0x00001000 },
    134 	{ ZX_LD_GBL_VOFF,	ZX_OFF_LD_GBL,		0x00001000 },
    135 };
    136 
    137 CFATTACH_DECL(zx, sizeof(struct zx_softc),
    138     zx_match, zx_attach, NULL, NULL);
    139 
    140 extern struct cfdriver zx_cd;
    141 
    142 dev_type_open(zxopen);
    143 dev_type_close(zxclose);
    144 dev_type_ioctl(zxioctl);
    145 dev_type_mmap(zxmmap);
    146 
    147 static struct fbdriver zx_fbdriver = {
    148 	zx_unblank, zxopen, zxclose, zxioctl, nopoll, zxmmap
    149 };
    150 
    151 int
    152 zx_match(struct device *parent, struct cfdata *cf, void *aux)
    153 {
    154 	struct sbus_attach_args *sa;
    155 
    156 	sa = (struct sbus_attach_args *)aux;
    157 
    158 	return (strcmp(sa->sa_name, "SUNW,leo") == 0);
    159 }
    160 
    161 void
    162 zx_attach(struct device *parent, struct device *self, void *args)
    163 {
    164 	struct zx_softc *sc;
    165 	struct sbus_attach_args *sa;
    166 	bus_space_handle_t bh;
    167 	bus_space_tag_t bt;
    168 	struct fbdevice *fb;
    169 	struct rasops_info *ri;
    170 	volatile struct zx_command *zc;
    171 	int isconsole;
    172 
    173 	sc = (struct zx_softc *)self;
    174 	sa = args;
    175 	fb = &sc->sc_fb;
    176 	ri = &fb->fb_rinfo;
    177 	bt = sa->sa_bustag;
    178 	sc->sc_bt = bt;
    179 
    180 	sc->sc_paddr = sbus_bus_addr(bt, sa->sa_slot, sa->sa_offset);
    181 
    182 	if (sbus_bus_map(bt, sa->sa_slot, sa->sa_offset + ZX_OFF_SS0,
    183 	    0x800000, BUS_SPACE_MAP_LINEAR, &bh) != 0) {
    184 		printf("%s: can't map bits\n", self->dv_xname);
    185 		return;
    186 	}
    187 	fb->fb_pixels = (caddr_t)bus_space_vaddr(bt, bh);
    188 	sc->sc_pixels = (u_int32_t *)fb->fb_pixels;
    189 
    190 	if (sbus_bus_map(bt, sa->sa_slot, sa->sa_offset + ZX_OFF_LC_SS0_USR,
    191 	    PAGE_SIZE, BUS_SPACE_MAP_LINEAR, &bh) != 0) {
    192 		printf("%s: can't map zc\n", self->dv_xname);
    193 		return;
    194 	}
    195 	sc->sc_zc = (struct zx_command *)bus_space_vaddr(bt, bh);
    196 
    197 	if (sbus_bus_map(bt, sa->sa_slot, sa->sa_offset + ZX_OFF_LD_SS0,
    198 	    PAGE_SIZE, BUS_SPACE_MAP_LINEAR, &bh) != 0) {
    199 		printf("%s: can't map ld/ss0\n", self->dv_xname);
    200 		return;
    201 	}
    202 	sc->sc_zd_ss0 = (struct zx_draw *)bus_space_vaddr(bt, bh);
    203 
    204 	if (sbus_bus_map(bt, sa->sa_slot, sa->sa_offset + ZX_OFF_LD_SS1,
    205 	    PAGE_SIZE, BUS_SPACE_MAP_LINEAR, &bh) != 0) {
    206 		printf("%s: can't map ld/ss1\n", self->dv_xname);
    207 		return;
    208 	}
    209 	sc->sc_zd_ss1 =
    210 	    (struct zx_draw_ss1 *)bus_space_vaddr(bt, bh);
    211 
    212 	if (sbus_bus_map(bt, sa->sa_slot, sa->sa_offset + ZX_OFF_LX_CROSS,
    213 	    PAGE_SIZE, BUS_SPACE_MAP_LINEAR, &bh) != 0) {
    214 		printf("%s: can't map zx\n", self->dv_xname);
    215 		return;
    216 	}
    217 	sc->sc_zx = (struct zx_cross *)bus_space_vaddr(bt, bh);
    218 
    219 	if (sbus_bus_map(bt, sa->sa_slot, sa->sa_offset + ZX_OFF_LX_CURSOR,
    220 	    PAGE_SIZE, BUS_SPACE_MAP_LINEAR, &bh) != 0) {
    221 		printf("%s: can't map zcu\n", self->dv_xname);
    222 		return;
    223 	}
    224 	sc->sc_zcu = (struct zx_cursor *)bus_space_vaddr(bt, bh);
    225 
    226 	fb->fb_driver = &zx_fbdriver;
    227 	fb->fb_device = &sc->sc_dv;
    228 	fb->fb_flags = sc->sc_dv.dv_cfdata->cf_flags & FB_USERMASK;
    229 	fb->fb_pfour = NULL;
    230 	fb->fb_linebytes = 8192;
    231 
    232 	fb_setsize_obp(fb, 32, 1280, 1024, sa->sa_node);
    233 
    234 	fb->fb_type.fb_cmsize = 256;
    235 	fb->fb_type.fb_depth = 32;
    236 	fb->fb_type.fb_size = fb->fb_type.fb_height * fb->fb_linebytes;
    237 	fb->fb_type.fb_type = FBTYPE_SUNLEO;
    238 
    239 	printf(": %d x %d", fb->fb_type.fb_width, fb->fb_type.fb_height);
    240 	isconsole = fb_is_console(sa->sa_node);
    241 	if (isconsole)
    242 		printf(" (console)");
    243 	printf("\n");
    244 
    245 	sbus_establish(&sc->sc_sd, &sc->sc_dv);
    246 	if (sa->sa_nintr != 0)
    247 		bus_intr_establish(bt, sa->sa_pri, IPL_NONE, 0, zx_intr, sc);
    248 
    249 	sc->sc_cmap = malloc(768, M_DEVBUF, M_NOWAIT);
    250 	fb_attach(&sc->sc_fb, isconsole);
    251 	zx_reset(sc);
    252 
    253 	/*
    254 	 * Attach to rcons.  XXX At this point, rasops_do_cursor() will be
    255 	 * called before we get our hooks in place.  So, we mask off access
    256 	 * to the framebuffer until it's done.
    257 	 */
    258 	zc = sc->sc_zc;
    259 	SETREG(zc->zc_fontt, 1);
    260 	SETREG(zc->zc_fontmsk, 0);
    261 
    262 	fbrcons_init(&sc->sc_fb);
    263 
    264 	SETREG(zc->zc_fontt, 0);
    265 	SETREG(zc->zc_fontmsk, 0xffffffff);
    266 
    267 	ri->ri_hw = sc;
    268 	ri->ri_do_cursor = zx_do_cursor;
    269 	ri->ri_ops.copycols = zx_copycols;
    270 	ri->ri_ops.copyrows = zx_copyrows;
    271 	ri->ri_ops.erasecols = zx_erasecols;
    272 	ri->ri_ops.eraserows = zx_eraserows;
    273 	ri->ri_ops.putchar = zx_putchar;
    274 
    275 	sc->sc_fontw = ri->ri_font->fontwidth;
    276 	sc->sc_fonth = ri->ri_font->fontheight;
    277 }
    278 
    279 int
    280 zxopen(dev_t dev, int flags, int mode, struct proc *p)
    281 {
    282 
    283 	if (device_lookup(&zx_cd, minor(dev)) == NULL)
    284 		return (ENXIO);
    285 	return (0);
    286 }
    287 
    288 int
    289 zxclose(dev_t dev, int flags, int mode, struct proc *p)
    290 {
    291 	struct zx_softc *sc;
    292 
    293 	sc = (struct zx_softc *)device_lookup(&zx_cd, minor(dev));
    294 
    295 	zx_reset(sc);
    296 	zx_cursor_blank(sc);
    297 	return (0);
    298 }
    299 
    300 int
    301 zxioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
    302 {
    303 	struct zx_softc *sc;
    304 	struct fbcmap *cm;
    305 	struct fbcursor *cu;
    306 	int rv, v, count, i;
    307 
    308 	sc = zx_cd.cd_devs[minor(dev)];
    309 
    310 	switch (cmd) {
    311 	case FBIOGTYPE:
    312 		*(struct fbtype *)data = sc->sc_fb.fb_type;
    313 		break;
    314 
    315 	case FBIOGATTR:
    316 #define fba ((struct fbgattr *)data)
    317 		fba->real_type = sc->sc_fb.fb_type.fb_type;
    318 		fba->owner = 0;		/* XXX ??? */
    319 		fba->fbtype = sc->sc_fb.fb_type;
    320 		fba->sattr.flags = 0;
    321 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
    322 		fba->sattr.dev_specific[0] = -1;
    323 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
    324 		fba->emu_types[1] = -1;
    325 		fba->emu_types[2] = -1;
    326 #undef fba
    327 		break;
    328 
    329 	case FBIOGVIDEO:
    330 		*(int *)data = ((sc->sc_flags & ZX_BLANKED) != 0);
    331 		break;
    332 
    333 	case FBIOSVIDEO:
    334 		if (*(int *)data)
    335 			zx_unblank(&sc->sc_dv);
    336 		else
    337 			zx_blank(&sc->sc_dv);
    338 		break;
    339 
    340 	case FBIOGETCMAP:
    341 		cm = (struct fbcmap *)data;
    342 		if (cm->index > 256 || cm->count > 256 - cm->index)
    343 			return (EINVAL);
    344 		rv = copyout(sc->sc_cmap + cm->index, cm->red, cm->count);
    345 		if (rv == 0)
    346 			rv = copyout(sc->sc_cmap + 256 + cm->index, cm->green,
    347 			    cm->count);
    348 		if (rv == 0)
    349 			rv = copyout(sc->sc_cmap + 512 + cm->index, cm->blue,
    350 			    cm->count);
    351 		return (rv);
    352 
    353 	case FBIOPUTCMAP:
    354 		cm = (struct fbcmap *)data;
    355 		if (cm->index > 256 || cm->count > 256 - cm->index)
    356 			return (EINVAL);
    357 		rv = copyin(cm->red, sc->sc_cmap + cm->index, cm->count);
    358 		if (rv == 0)
    359 			rv = copyin(cm->green, sc->sc_cmap + 256 + cm->index,
    360 			    cm->count);
    361 		if (rv == 0)
    362 			rv = copyin(cm->blue, sc->sc_cmap + 512 + cm->index,
    363 			    cm->count);
    364 		zx_cmap_put(sc);
    365 		return (rv);
    366 
    367 	case FBIOGCURPOS:
    368 		*(struct fbcurpos *)data = sc->sc_curpos;
    369 		break;
    370 
    371 	case FBIOSCURPOS:
    372 		sc->sc_curpos = *(struct fbcurpos *)data;
    373 		zx_cursor_move(sc);
    374 		break;
    375 
    376 	case FBIOGCURMAX:
    377 		((struct fbcurpos *)data)->x = 32;
    378 		((struct fbcurpos *)data)->y = 32;
    379 		break;
    380 
    381 	case FBIOSCURSOR:
    382 		cu = (struct fbcursor *)data;
    383 		v = cu->set;
    384 
    385 		if ((v & FB_CUR_SETSHAPE) != 0) {
    386 			if ((u_int)cu->size.x > 32 || (u_int)cu->size.y > 32)
    387 				return (EINVAL);
    388 			count = cu->size.y * 4;
    389 			if (!uvm_useracc(cu->image, count, B_READ) ||
    390 			    !uvm_useracc(cu->mask, count, B_READ))
    391 				return (EFAULT);
    392 		}
    393 		if ((v & FB_CUR_SETCUR) != 0) {
    394 			if (cu->enable)
    395 				zx_cursor_unblank(sc);
    396 			else
    397 				zx_cursor_blank(sc);
    398 		}
    399 		if ((v & (FB_CUR_SETPOS | FB_CUR_SETHOT)) != 0) {
    400 			if ((v & FB_CUR_SETPOS) != 0)
    401 				sc->sc_curpos = cu->pos;
    402 			if ((v & FB_CUR_SETHOT) != 0)
    403 				sc->sc_curhot = cu->hot;
    404 			zx_cursor_move(sc);
    405 		}
    406 		if ((v & FB_CUR_SETCMAP) != 0) {
    407 			if (cu->cmap.index > 2 ||
    408 			    cu->cmap.count > 2 - cu->cmap.index)
    409 				return (EINVAL);
    410 			for (i = 0; i < cu->cmap.count; i++) {
    411 				if ((v = fubyte(&cu->cmap.red[i])) < 0)
    412 					return (EFAULT);
    413 				sc->sc_curcmap[i + cu->cmap.index + 0] = v;
    414 				if ((v = fubyte(&cu->cmap.green[i])) < 0)
    415 					return (EFAULT);
    416 				sc->sc_curcmap[i + cu->cmap.index + 2] = v;
    417 				if ((v = fubyte(&cu->cmap.blue[i])) < 0)
    418 					return (EFAULT);
    419 				sc->sc_curcmap[i + cu->cmap.index + 4] = v;
    420 			}
    421 			zx_cursor_color(sc);
    422 		}
    423 		if ((v & FB_CUR_SETSHAPE) != 0) {
    424 			sc->sc_cursize = cu->size;
    425 			count = cu->size.y * 4;
    426 			memset(sc->sc_curbits, 0, sizeof(sc->sc_curbits));
    427 			copyin(cu->mask, (caddr_t)sc->sc_curbits[0], count);
    428 			copyin(cu->image, (caddr_t)sc->sc_curbits[1], count);
    429 			zx_cursor_set(sc);
    430 		}
    431 		break;
    432 
    433 	case FBIOGCURSOR:
    434 		cu = (struct fbcursor *)data;
    435 
    436 		cu->set = FB_CUR_SETALL;
    437 		cu->enable = ((sc->sc_flags & ZX_CURSOR) != 0);
    438 		cu->pos = sc->sc_curpos;
    439 		cu->hot = sc->sc_curhot;
    440 		cu->size = sc->sc_cursize;
    441 
    442 		if (cu->image != NULL) {
    443 			count = sc->sc_cursize.y * 4;
    444 			rv = copyout((caddr_t)sc->sc_curbits[1],
    445 			    (caddr_t)cu->image, count);
    446 			if (rv)
    447 				return (rv);
    448 			rv = copyout((caddr_t)sc->sc_curbits[0],
    449 			    (caddr_t)cu->mask, count);
    450 			if (rv)
    451 				return (rv);
    452 		}
    453 		if (cu->cmap.red != NULL) {
    454 			if (cu->cmap.index > 2 ||
    455 			    cu->cmap.count > 2 - cu->cmap.index)
    456 				return (EINVAL);
    457 			for (i = 0; i < cu->cmap.count; i++) {
    458 				v = sc->sc_curcmap[i + cu->cmap.index + 0];
    459 				if (subyte(&cu->cmap.red[i], v))
    460 					return (EFAULT);
    461 				v = sc->sc_curcmap[i + cu->cmap.index + 2];
    462 				if (subyte(&cu->cmap.green[i], v))
    463 					return (EFAULT);
    464 				v = sc->sc_curcmap[i + cu->cmap.index + 4];
    465 				if (subyte(&cu->cmap.blue[i], v))
    466 					return (EFAULT);
    467 			}
    468 		} else {
    469 			cu->cmap.index = 0;
    470 			cu->cmap.count = 2;
    471 		}
    472 		break;
    473 
    474 	default:
    475 #ifdef DEBUG
    476 		log(LOG_NOTICE, "zxioctl(0x%lx) (%s[%d])\n", cmd,
    477 		    p->p_comm, p->p_pid);
    478 #endif
    479 		return (ENOTTY);
    480 	}
    481 
    482 	return (0);
    483 }
    484 
    485 int
    486 zx_intr(void *cookie)
    487 {
    488 
    489 	return (1);
    490 }
    491 
    492 void
    493 zx_reset(struct zx_softc *sc)
    494 {
    495 	volatile struct zx_draw *zd;
    496 	volatile struct zx_command *zc;
    497 	struct fbtype *fbt;
    498 	u_int i;
    499 
    500 	zd = sc->sc_zd_ss0;
    501 	zc = sc->sc_zc;
    502 	fbt = &sc->sc_fb.fb_type;
    503 
    504 	zx_cross_loadwid(sc, ZX_WID_DBL_8, 0, 0x2c0);
    505 	zx_cross_loadwid(sc, ZX_WID_DBL_8, 1, 0x30);
    506 	zx_cross_loadwid(sc, ZX_WID_DBL_8, 2, 0x20);
    507 	zx_cross_loadwid(sc, ZX_WID_DBL_24, 1, 0x30);
    508 
    509 	i = sc->sc_zd_ss1->zd_misc;
    510 	i |= ZX_SS1_MISC_ENABLE;
    511 	SETREG(sc->sc_zd_ss1->zd_misc, i);
    512 
    513 	SETREG(zd->zd_wid, 0xffffffff);
    514 	SETREG(zd->zd_widclip, 0);
    515 	SETREG(zd->zd_wmask, 0xffff);
    516 	SETREG(zd->zd_vclipmin, 0);
    517 	SETREG(zd->zd_vclipmax,
    518 	    (fbt->fb_width - 1) | ((fbt->fb_height - 1) << 16));
    519 	SETREG(zd->zd_fg, 0);
    520 	SETREG(zd->zd_planemask, 0xff000000);
    521 	SETREG(zd->zd_rop, ZX_STD_ROP);
    522 
    523 	SETREG(zc->zc_extent,
    524 	    (fbt->fb_width - 1) | ((fbt->fb_height - 1) << 11));
    525 	SETREG(zc->zc_addrspace, ZX_ADDRSPC_FONT_OBGR);
    526 	SETREG(zc->zc_fontt, 0);
    527 
    528 	for (i = 0; i < 256; i++) {
    529 		sc->sc_cmap[i] = rasops_cmap[i * 3];
    530 		sc->sc_cmap[i + 256] = rasops_cmap[i * 3 + 1];
    531 		sc->sc_cmap[i + 512] = rasops_cmap[i * 3 + 2];
    532 	}
    533 
    534 	zx_cmap_put(sc);
    535 }
    536 
    537 int
    538 zx_cross_wait(struct zx_softc *sc)
    539 {
    540 	volatile struct zx_cross *zx;
    541 	int i;
    542 
    543 	zx = sc->sc_zx;
    544 
    545 	for (i = 300000; i != 0; i--) {
    546 		if ((zx->zx_csr & ZX_CROSS_CSR_PROGRESS) == 0)
    547 			break;
    548 		DELAY(1);
    549 	}
    550 
    551 	if (i == 0)
    552 		printf("zx_cross_wait: timed out\n");
    553 
    554 	return (i);
    555 }
    556 
    557 int
    558 zx_cross_loadwid(struct zx_softc *sc, u_int type, u_int index, u_int value)
    559 {
    560 	volatile struct zx_cross *zx;
    561 	u_int tmp;
    562 
    563 	zx = sc->sc_zx;
    564 	SETREG(zx->zx_type, ZX_CROSS_TYPE_WID);
    565 
    566 	if (zx_cross_wait(sc))
    567 		return (1);
    568 
    569 	if (type == ZX_WID_DBL_8)
    570 		tmp = (index & 0x0f) + 0x40;
    571 	else if (type == ZX_WID_DBL_24)
    572 		tmp = index & 0x3f;
    573 
    574 	SETREG(zx->zx_type, 0x5800 + tmp);
    575 	SETREG(zx->zx_value, value);
    576 	SETREG(zx->zx_type, ZX_CROSS_TYPE_WID);
    577 	SETREG(zx->zx_csr, ZX_CROSS_CSR_UNK | ZX_CROSS_CSR_UNK2);
    578 
    579 	return (0);
    580 }
    581 
    582 int
    583 zx_cmap_put(struct zx_softc *sc)
    584 {
    585 	volatile struct zx_cross *zx;
    586 	const u_char *b;
    587 	u_int i, t;
    588 
    589 	zx = sc->sc_zx;
    590 
    591 	SETREG(zx->zx_type, ZX_CROSS_TYPE_CLUT0);
    592 	if (zx_cross_wait(sc))
    593 		return (1);
    594 
    595 	SETREG(zx->zx_type, ZX_CROSS_TYPE_CLUTDATA);
    596 
    597 	for (i = 0, b = sc->sc_cmap; i < 256; i++) {
    598 		t = b[i];
    599 		t |= b[i + 256] << 8;
    600 		t |= b[i + 512] << 16;
    601 		SETREG(zx->zx_value, t);
    602 	}
    603 
    604 	SETREG(zx->zx_type, ZX_CROSS_TYPE_CLUT0);
    605 	i = zx->zx_csr;
    606 	i = i | ZX_CROSS_CSR_UNK | ZX_CROSS_CSR_UNK2;
    607 	SETREG(zx->zx_csr, i);
    608 	return (0);
    609 }
    610 
    611 void
    612 zx_cursor_move(struct zx_softc *sc)
    613 {
    614 	volatile struct zx_cursor *zcu;
    615 	int sx, sy, x, y;
    616 
    617 	x = sc->sc_curpos.x - sc->sc_curhot.x;
    618 	y = sc->sc_curpos.y - sc->sc_curhot.y;
    619 	zcu = sc->sc_zcu;
    620 
    621 	if (x < 0) {
    622 		sx = min(-x, 32);
    623 		x = 0;
    624 	} else
    625 		sx = 0;
    626 
    627 	if (y < 0) {
    628 		sy = min(-y, 32);
    629 		y = 0;
    630 	} else
    631 		sy = 0;
    632 
    633 	if (sx != sc->sc_shiftx || sy != sc->sc_shifty) {
    634 		sc->sc_shiftx = sx;
    635 		sc->sc_shifty = sy;
    636 		zx_cursor_set(sc);
    637 	}
    638 
    639 	SETREG(zcu->zcu_sxy, ((y & 0x7ff) << 11) | (x & 0x7ff));
    640 	SETREG(zcu->zcu_misc, zcu->zcu_misc | 0x30);
    641 
    642 	/* XXX Necessary? */
    643 	SETREG(zcu->zcu_misc, zcu->zcu_misc | 0x80);
    644 }
    645 
    646 void
    647 zx_cursor_set(struct zx_softc *sc)
    648 {
    649 	volatile struct zx_cursor *zcu;
    650 	int i, j, data;
    651 
    652 	zcu = sc->sc_zcu;
    653 
    654 	if ((sc->sc_flags & ZX_CURSOR) != 0)
    655 		SETREG(zcu->zcu_misc, zcu->zcu_misc & ~0x80);
    656 
    657 	for (j = 0; j < 2; j++) {
    658 		SETREG(zcu->zcu_type, 0x20 << i);
    659 
    660 		for (i = sc->sc_shifty; i < 32; i++) {
    661 			data = sc->sc_curbits[j][i];
    662 			SETREG(zcu->zcu_data, data >> sc->sc_shiftx);
    663 		}
    664 		for (i = sc->sc_shifty; i != 0; i--)
    665 			SETREG(zcu->zcu_data, 0);
    666 	}
    667 
    668 	if ((sc->sc_flags & ZX_CURSOR) != 0)
    669 		SETREG(zcu->zcu_misc, zcu->zcu_misc | 0x80);
    670 }
    671 
    672 void
    673 zx_cursor_blank(struct zx_softc *sc)
    674 {
    675 	volatile struct zx_cursor *zcu;
    676 
    677 	sc->sc_flags &= ~ZX_CURSOR;
    678 	zcu = sc->sc_zcu;
    679 	SETREG(zcu->zcu_misc, zcu->zcu_misc & ~0x80);
    680 }
    681 
    682 void
    683 zx_cursor_unblank(struct zx_softc *sc)
    684 {
    685 	volatile struct zx_cursor *zcu;
    686 
    687 	sc->sc_flags |= ZX_CURSOR;
    688 	zcu = sc->sc_zcu;
    689 	SETREG(zcu->zcu_misc, zcu->zcu_misc | 0x80);
    690 }
    691 
    692 void
    693 zx_cursor_color(struct zx_softc *sc)
    694 {
    695 	volatile struct zx_cursor *zcu;
    696 	u_int8_t tmp;
    697 
    698 	zcu = sc->sc_zcu;
    699 
    700 	SETREG(zcu->zcu_type, 0x50);
    701 
    702 	tmp = sc->sc_curcmap[0] | (sc->sc_curcmap[2] << 8) |
    703 	    (sc->sc_curcmap[4] << 16);
    704 	SETREG(zcu->zcu_data, tmp);
    705 
    706 	tmp = sc->sc_curcmap[1] | (sc->sc_curcmap[3] << 8) |
    707 	    (sc->sc_curcmap[5] << 16);
    708 	SETREG(zcu->zcu_data, sc->sc_curcmap[1]);
    709 
    710 	SETREG(zcu->zcu_misc, zcu->zcu_misc | 0x03);
    711 }
    712 
    713 void
    714 zx_blank(struct device *dv)
    715 {
    716 	struct zx_softc *sc;
    717 	volatile struct zx_cross *zx;
    718 
    719 	sc = (struct zx_softc *)dv;
    720 
    721 	if ((sc->sc_flags & ZX_BLANKED) != 0)
    722 		return;
    723 	sc->sc_flags |= ZX_BLANKED;
    724 
    725 	zx = sc->sc_zx;
    726 	SETREG(zx->zx_type, ZX_CROSS_TYPE_VIDEO);
    727 	SETREG(zx->zx_csr, zx->zx_csr & ~ZX_CROSS_CSR_ENABLE);
    728 }
    729 
    730 void
    731 zx_unblank(struct device *dv)
    732 {
    733 	struct zx_softc *sc;
    734 	volatile struct zx_cross *zx;
    735 
    736 	sc = (struct zx_softc *)dv;
    737 
    738 	if ((sc->sc_flags & ZX_BLANKED) == 0)
    739 		return;
    740 	sc->sc_flags &= ~ZX_BLANKED;
    741 
    742 	zx = sc->sc_zx;
    743 	SETREG(zx->zx_type, ZX_CROSS_TYPE_VIDEO);
    744 	SETREG(zx->zx_csr, zx->zx_csr | ZX_CROSS_CSR_ENABLE);
    745 }
    746 
    747 paddr_t
    748 zxmmap(dev_t dev, off_t off, int prot)
    749 {
    750 	struct zx_softc *sc;
    751 	const struct zx_mmo *mm, *max;
    752 
    753 	sc = device_lookup(&zx_cd, minor(dev));
    754 	off = trunc_page(off);
    755 	mm = zx_mmo;
    756 	max = mm + sizeof(zx_mmo) / sizeof(zx_mmo[0]);
    757 
    758 	for (; mm < max; mm++)
    759 		if (off >= mm->mo_va && off < mm->mo_va + mm->mo_size) {
    760 			off = off - mm->mo_va + mm->mo_pa;
    761 			return (bus_space_mmap(sc->sc_bt, sc->sc_paddr,
    762 			    off, prot, BUS_SPACE_MAP_LINEAR));
    763 		}
    764 
    765 	return (-1);
    766 }
    767 
    768 void
    769 zx_fillrect(struct rasops_info *ri, int x, int y, int w, int h, long attr,
    770 	    int rop)
    771 {
    772 	struct zx_softc *sc;
    773 	volatile struct zx_command *zc;
    774 	volatile struct zx_draw *zd;
    775 	int fg, bg;
    776 
    777 	sc = ri->ri_hw;
    778 	zc = sc->sc_zc;
    779 	zd = sc->sc_zd_ss0;
    780 
    781 	rasops_unpack_attr(attr, &fg, &bg, NULL);
    782 	x = x * sc->sc_fontw + ri->ri_xorigin;
    783 	y = y * sc->sc_fonth + ri->ri_yorigin;
    784 	w = sc->sc_fontw * w - 1;
    785 	h = sc->sc_fonth * h - 1;
    786 
    787 	while ((zc->zc_csr & ZX_CSR_BLT_BUSY) != 0)
    788 		;
    789 
    790 	SETREG(zd->zd_rop, rop);
    791 	SETREG(zd->zd_fg, (bg & 7) ? 0x00000000 : 0xff000000);
    792 	SETREG(zc->zc_extent, w | (h << 11));
    793 	SETREG(zc->zc_fill, x | (y << 11) | 0x80000000);
    794 }
    795 
    796 void
    797 zx_copyrect(struct rasops_info *ri, int sx, int sy, int dx, int dy, int w,
    798 	    int h)
    799 {
    800 	struct zx_softc *sc;
    801 	volatile struct zx_command *zc;
    802 	volatile struct zx_draw *zd;
    803 	int dir;
    804 
    805 	sc = ri->ri_hw;
    806 	zc = sc->sc_zc;
    807 	zd = sc->sc_zd_ss0;
    808 
    809 	sx = sx * sc->sc_fontw + ri->ri_xorigin;
    810 	sy = sy * sc->sc_fonth + ri->ri_yorigin;
    811 	dx = dx * sc->sc_fontw + ri->ri_xorigin;
    812 	dy = dy * sc->sc_fonth + ri->ri_yorigin;
    813 	w = w * sc->sc_fontw - 1;
    814 	h = h * sc->sc_fonth - 1;
    815 
    816 	if (sy < dy || sx < dx) {
    817 		dir = 0x80000000;
    818 		sx += w;
    819 		sy += h;
    820 		dx += w;
    821 		dy += h;
    822 	} else
    823 		dir = 0;
    824 
    825 	while ((zc->zc_csr & ZX_CSR_BLT_BUSY) != 0)
    826 		;
    827 
    828 	SETREG(zd->zd_rop, ZX_STD_ROP);
    829 	SETREG(zc->zc_extent, w | (h << 11) | dir);
    830 	SETREG(zc->zc_src, sx | (sy << 11));
    831 	SETREG(zc->zc_copy, dx | (dy << 11));
    832 }
    833 
    834 void
    835 zx_do_cursor(struct rasops_info *ri)
    836 {
    837 
    838 	zx_fillrect(ri, ri->ri_ccol, ri->ri_crow, 1, 1, 0,
    839 	    ZX_ROP_NEW_XOR_OLD | ZX_ATTR_WE_ENABLE | ZX_ATTR_OE_ENABLE |
    840 	    ZX_ATTR_FORCE_WID);
    841 }
    842 
    843 void
    844 zx_erasecols(void *cookie, int row, int col, int num, long attr)
    845 {
    846 	struct rasops_info *ri;
    847 
    848 	ri = (struct rasops_info *)cookie;
    849 
    850 	zx_fillrect(ri, col, row, num, 1, attr, ZX_STD_ROP);
    851 }
    852 
    853 void
    854 zx_eraserows(void *cookie, int row, int num, long attr)
    855 {
    856 	struct rasops_info *ri;
    857 
    858 	ri = (struct rasops_info *)cookie;
    859 
    860 	zx_fillrect(ri, 0, row, ri->ri_cols, num, attr, ZX_STD_ROP);
    861 }
    862 
    863 void
    864 zx_copyrows(void *cookie, int src, int dst, int num)
    865 {
    866 	struct rasops_info *ri;
    867 
    868 	ri = (struct rasops_info *)cookie;
    869 
    870 	zx_copyrect(ri, 0, src, 0, dst, ri->ri_cols, num);
    871 }
    872 
    873 void
    874 zx_copycols(void *cookie, int row, int src, int dst, int num)
    875 {
    876 	struct rasops_info *ri;
    877 
    878 	ri = (struct rasops_info *)cookie;
    879 
    880 	zx_copyrect(ri, src, row, dst, row, num, 1);
    881 }
    882 
    883 void
    884 zx_putchar(void *cookie, int row, int col, u_int uc, long attr)
    885 {
    886 	struct rasops_info *ri;
    887 	struct zx_softc *sc;
    888 	struct wsdisplay_font *font;
    889 	volatile struct zx_command *zc;
    890 	volatile struct zx_draw *zd;
    891 	volatile u_int32_t *dp;
    892 	u_int8_t *fb;
    893 	int fs, i, fg, bg, ul;
    894 
    895 	ri = (struct rasops_info *)cookie;
    896 
    897 	if (uc == ' ') {
    898 		zx_fillrect(ri, col, row, 1, 1, attr, ZX_STD_ROP);
    899 		return;
    900 	}
    901 
    902 	sc = (struct zx_softc *)ri->ri_hw;
    903 	font = ri->ri_font;
    904 	zc = sc->sc_zc;
    905 	zd = sc->sc_zd_ss0;
    906 
    907 	dp = (volatile u_int32_t *)sc->sc_pixels +
    908 	    ((row * sc->sc_fonth + ri->ri_yorigin) << 11) +
    909 	    (col * sc->sc_fontw + ri->ri_xorigin);
    910 	fb = (u_int8_t *)font->data + (uc - font->firstchar) *
    911 	    ri->ri_fontscale;
    912 	fs = font->stride;
    913 	rasops_unpack_attr(attr, &fg, &bg, &ul);
    914 
    915 	while ((zc->zc_csr & ZX_CSR_BLT_BUSY) != 0)
    916 		;
    917 
    918 	SETREG(zd->zd_rop, ZX_STD_ROP);
    919 	SETREG(zd->zd_fg, (fg & 7) ? 0x00000000 : 0xff000000);
    920 	SETREG(zd->zd_bg, (bg & 7) ? 0x00000000 : 0xff000000);
    921 	SETREG(zc->zc_fontmsk, 0xffffffff << (32 - sc->sc_fontw));
    922 
    923 	if (sc->sc_fontw <= 8) {
    924 		for (i = sc->sc_fonth; i != 0; i--, dp += 2048) {
    925 			*dp = *fb << 24;
    926 			fb += fs;
    927 		}
    928 	} else {
    929 		for (i = sc->sc_fonth; i != 0; i--, dp += 2048) {
    930 			*dp = *((u_int16_t *)fb) << 16;
    931 			fb += fs;
    932 		}
    933 	}
    934 
    935 	if (ul) {
    936 		dp -= 4096;
    937 		*dp = 0xffffffff;
    938 	}
    939 }
    940