Home | History | Annotate | Line # | Download | only in sbus
zx.c revision 1.1
      1 /*	$NetBSD: zx.c,v 1.1 2002/09/13 14:03:53 ad 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.1 2002/09/13 14:03:53 ad 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 struct cfattach zx_ca = {
    138 	sizeof(struct zx_softc), zx_match, zx_attach
    139 };
    140 
    141 extern struct cfdriver zx_cd;
    142 
    143 dev_type_open(zxopen);
    144 dev_type_close(zxclose);
    145 dev_type_ioctl(zxioctl);
    146 dev_type_mmap(zxmmap);
    147 
    148 static struct fbdriver zx_fbdriver = {
    149 	zx_unblank, zxopen, zxclose, zxioctl, nopoll, zxmmap
    150 };
    151 
    152 int
    153 zx_match(struct device *parent, struct cfdata *cf, void *aux)
    154 {
    155 	struct sbus_attach_args *sa;
    156 
    157 	sa = (struct sbus_attach_args *)aux;
    158 
    159 	return (strcmp(sa->sa_name, "SUNW,leo") == 0);
    160 }
    161 
    162 void
    163 zx_attach(struct device *parent, struct device *self, void *args)
    164 {
    165 	struct zx_softc *sc;
    166 	struct sbus_attach_args *sa;
    167 	bus_space_handle_t bh;
    168 	bus_space_tag_t bt;
    169 	struct fbdevice *fb;
    170 	struct rasops_info *ri;
    171 	volatile struct zx_command *zc;
    172 	int isconsole;
    173 
    174 	sc = (struct zx_softc *)self;
    175 	sa = args;
    176 	fb = &sc->sc_fb;
    177 	ri = &fb->fb_rinfo;
    178 	bt = sa->sa_bustag;
    179 	sc->sc_bt = bt;
    180 
    181 	sc->sc_paddr = sbus_bus_addr(bt, sa->sa_slot, sa->sa_offset);
    182 
    183 	if (sbus_bus_map(bt, sa->sa_slot, sa->sa_offset + ZX_OFF_SS0,
    184 	    0x800000, BUS_SPACE_MAP_LINEAR, &bh) != 0) {
    185 		printf("%s: can't map bits\n", self->dv_xname);
    186 		return;
    187 	}
    188 	fb->fb_pixels = (caddr_t)bus_space_vaddr(bt, bh);
    189 	sc->sc_pixels = (u_int32_t *)fb->fb_pixels;
    190 
    191 	if (sbus_bus_map(bt, sa->sa_slot, sa->sa_offset + ZX_OFF_LC_SS0_USR,
    192 	    PAGE_SIZE, BUS_SPACE_MAP_LINEAR, &bh) != 0) {
    193 		printf("%s: can't map zc\n", self->dv_xname);
    194 		return;
    195 	}
    196 	sc->sc_zc = (struct zx_command *)bus_space_vaddr(bt, bh);
    197 
    198 	if (sbus_bus_map(bt, sa->sa_slot, sa->sa_offset + ZX_OFF_LD_SS0,
    199 	    PAGE_SIZE, BUS_SPACE_MAP_LINEAR, &bh) != 0) {
    200 		printf("%s: can't map ld/ss0\n", self->dv_xname);
    201 		return;
    202 	}
    203 	sc->sc_zd_ss0 = (struct zx_draw *)bus_space_vaddr(bt, bh);
    204 
    205 	if (sbus_bus_map(bt, sa->sa_slot, sa->sa_offset + ZX_OFF_LD_SS1,
    206 	    PAGE_SIZE, BUS_SPACE_MAP_LINEAR, &bh) != 0) {
    207 		printf("%s: can't map ld/ss1\n", self->dv_xname);
    208 		return;
    209 	}
    210 	sc->sc_zd_ss1 =
    211 	    (struct zx_draw_ss1 *)bus_space_vaddr(bt, bh);
    212 
    213 	if (sbus_bus_map(bt, sa->sa_slot, sa->sa_offset + ZX_OFF_LX_CROSS,
    214 	    PAGE_SIZE, BUS_SPACE_MAP_LINEAR, &bh) != 0) {
    215 		printf("%s: can't map zx\n", self->dv_xname);
    216 		return;
    217 	}
    218 	sc->sc_zx = (struct zx_cross *)bus_space_vaddr(bt, bh);
    219 
    220 	if (sbus_bus_map(bt, sa->sa_slot, sa->sa_offset + ZX_OFF_LX_CURSOR,
    221 	    PAGE_SIZE, BUS_SPACE_MAP_LINEAR, &bh) != 0) {
    222 		printf("%s: can't map zcu\n", self->dv_xname);
    223 		return;
    224 	}
    225 	sc->sc_zcu = (struct zx_cursor *)bus_space_vaddr(bt, bh);
    226 
    227 	fb->fb_driver = &zx_fbdriver;
    228 	fb->fb_device = &sc->sc_dv;
    229 	fb->fb_flags = sc->sc_dv.dv_cfdata->cf_flags & FB_USERMASK;
    230 	fb->fb_pfour = NULL;
    231 	fb->fb_linebytes = 8192;
    232 
    233 	fb_setsize_obp(fb, 32, 1280, 1024, sa->sa_node);
    234 
    235 	fb->fb_type.fb_cmsize = 256;
    236 	fb->fb_type.fb_depth = 32;
    237 	fb->fb_type.fb_size = fb->fb_type.fb_height * fb->fb_linebytes;
    238 	fb->fb_type.fb_type = FBTYPE_SUNLEO;
    239 
    240 	printf(": %d x %d", fb->fb_type.fb_width, fb->fb_type.fb_height);
    241 	isconsole = fb_is_console(sa->sa_node);
    242 	if (isconsole)
    243 		printf(" (console)");
    244 	printf("\n");
    245 
    246 	sbus_establish(&sc->sc_sd, &sc->sc_dv);
    247 	if (sa->sa_nintr != 0)
    248 		bus_intr_establish(bt, sa->sa_pri, IPL_NONE, 0, zx_intr, sc);
    249 
    250 	sc->sc_cmap = malloc(768, M_DEVBUF, M_NOWAIT);
    251 	fb_attach(&sc->sc_fb, isconsole);
    252 	zx_reset(sc);
    253 
    254 	/*
    255 	 * Attach to rcons.  XXX At this point, rasops_do_cursor() will be
    256 	 * called before we get our hooks in place.  So, we mask off access
    257 	 * to the framebuffer until it's done.
    258 	 */
    259 	zc = sc->sc_zc;
    260 	SETREG(zc->zc_fontt, 1);
    261 	SETREG(zc->zc_fontmsk, 0);
    262 
    263 	fbrcons_init(&sc->sc_fb);
    264 
    265 	SETREG(zc->zc_fontt, 0);
    266 	SETREG(zc->zc_fontmsk, 0xffffffff);
    267 
    268 	ri->ri_hw = sc;
    269 	ri->ri_do_cursor = zx_do_cursor;
    270 	ri->ri_ops.copycols = zx_copycols;
    271 	ri->ri_ops.copyrows = zx_copyrows;
    272 	ri->ri_ops.erasecols = zx_erasecols;
    273 	ri->ri_ops.eraserows = zx_eraserows;
    274 	ri->ri_ops.putchar = zx_putchar;
    275 
    276 	sc->sc_fontw = ri->ri_font->fontwidth;
    277 	sc->sc_fonth = ri->ri_font->fontheight;
    278 }
    279 
    280 int
    281 zxopen(dev_t dev, int flags, int mode, struct proc *p)
    282 {
    283 
    284 	if (device_lookup(&zx_cd, minor(dev)) == NULL)
    285 		return (ENXIO);
    286 	return (0);
    287 }
    288 
    289 int
    290 zxclose(dev_t dev, int flags, int mode, struct proc *p)
    291 {
    292 	struct zx_softc *sc;
    293 
    294 	sc = (struct zx_softc *)device_lookup(&zx_cd, minor(dev));
    295 
    296 	zx_reset(sc);
    297 	zx_cursor_blank(sc);
    298 	return (0);
    299 }
    300 
    301 int
    302 zxioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
    303 {
    304 	struct zx_softc *sc;
    305 	struct fbcmap *cm;
    306 	struct fbcursor *cu;
    307 	int rv, v, count, i;
    308 
    309 	sc = zx_cd.cd_devs[minor(dev)];
    310 
    311 	switch (cmd) {
    312 	case FBIOGTYPE:
    313 		*(struct fbtype *)data = sc->sc_fb.fb_type;
    314 		break;
    315 
    316 	case FBIOGATTR:
    317 #define fba ((struct fbgattr *)data)
    318 		fba->real_type = sc->sc_fb.fb_type.fb_type;
    319 		fba->owner = 0;		/* XXX ??? */
    320 		fba->fbtype = sc->sc_fb.fb_type;
    321 		fba->sattr.flags = 0;
    322 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
    323 		fba->sattr.dev_specific[0] = -1;
    324 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
    325 		fba->emu_types[1] = -1;
    326 		fba->emu_types[2] = -1;
    327 #undef fba
    328 		break;
    329 
    330 	case FBIOGVIDEO:
    331 		*(int *)data = ((sc->sc_flags & ZX_BLANKED) != 0);
    332 		break;
    333 
    334 	case FBIOSVIDEO:
    335 		if (*(int *)data)
    336 			zx_unblank(&sc->sc_dv);
    337 		else
    338 			zx_blank(&sc->sc_dv);
    339 		break;
    340 
    341 	case FBIOGETCMAP:
    342 		cm = (struct fbcmap *)data;
    343 		if (cm->index > 256 || cm->count > 256 - cm->index)
    344 			return (EINVAL);
    345 		rv = copyout(sc->sc_cmap + cm->index, cm->red, cm->count);
    346 		if (rv != 0)
    347 			rv = copyout(sc->sc_cmap + 256 + cm->index, cm->green,
    348 			    cm->count);
    349 		if (rv != 0)
    350 			rv = copyout(sc->sc_cmap + 512 + cm->index, cm->blue,
    351 			    cm->count);
    352 		return (rv);
    353 
    354 	case FBIOPUTCMAP:
    355 		cm = (struct fbcmap *)data;
    356 		if (cm->index > 256 || cm->count > 256 - cm->index)
    357 			return (EINVAL);
    358 		rv = copyin(cm->red, sc->sc_cmap + cm->index, cm->count);
    359 		if (rv != 0)
    360 			rv = copyin(cm->green, sc->sc_cmap + 256 + cm->index,
    361 			    cm->count);
    362 		if (rv != 0)
    363 			rv = copyin(cm->blue, sc->sc_cmap + 512 + cm->index,
    364 			    cm->count);
    365 
    366 		zx_cmap_put(sc);
    367 		return (rv);
    368 
    369 	case FBIOGCURPOS:
    370 		*(struct fbcurpos *)data = sc->sc_curpos;
    371 		break;
    372 
    373 	case FBIOSCURPOS:
    374 		sc->sc_curpos = *(struct fbcurpos *)data;
    375 		zx_cursor_move(sc);
    376 		break;
    377 
    378 	case FBIOGCURMAX:
    379 		((struct fbcurpos *)data)->x = 32;
    380 		((struct fbcurpos *)data)->y = 32;
    381 		break;
    382 
    383 	case FBIOSCURSOR:
    384 		cu = (struct fbcursor *)data;
    385 		v = cu->set;
    386 
    387 		if ((v & FB_CUR_SETSHAPE) != 0) {
    388 			if ((u_int)cu->size.x > 32 || (u_int)cu->size.y > 32)
    389 				return (EINVAL);
    390 			count = cu->size.y * 4;
    391 			if (!uvm_useracc(cu->image, count, B_READ) ||
    392 			    !uvm_useracc(cu->mask, count, B_READ))
    393 				return (EFAULT);
    394 		}
    395 		if ((v & FB_CUR_SETCUR) != 0) {
    396 			if (cu->enable)
    397 				zx_cursor_unblank(sc);
    398 			else
    399 				zx_cursor_blank(sc);
    400 		}
    401 		if ((v & (FB_CUR_SETPOS | FB_CUR_SETHOT)) != 0) {
    402 			if ((v & FB_CUR_SETPOS) != 0)
    403 				sc->sc_curpos = cu->pos;
    404 			if ((v & FB_CUR_SETHOT) != 0)
    405 				sc->sc_curhot = cu->hot;
    406 			zx_cursor_move(sc);
    407 		}
    408 		if ((v & FB_CUR_SETCMAP) != 0) {
    409 			if (cu->cmap.index > 2 ||
    410 			    cu->cmap.count > 2 - cu->cmap.index)
    411 				return (EINVAL);
    412 			for (i = 0; i < cu->cmap.count; i++) {
    413 				if ((v = fubyte(&cu->cmap.red[i])) < 0)
    414 					return (EFAULT);
    415 				sc->sc_curcmap[i + cu->cmap.index + 0] = v;
    416 				if ((v = fubyte(&cu->cmap.green[i])) < 0)
    417 					return (EFAULT);
    418 				sc->sc_curcmap[i + cu->cmap.index + 2] = v;
    419 				if ((v = fubyte(&cu->cmap.blue[i])) < 0)
    420 					return (EFAULT);
    421 				sc->sc_curcmap[i + cu->cmap.index + 4] = v;
    422 			}
    423 			zx_cursor_color(sc);
    424 		}
    425 		if ((v & FB_CUR_SETSHAPE) != 0) {
    426 			sc->sc_cursize = cu->size;
    427 			count = cu->size.y * 4;
    428 			memset(sc->sc_curbits, 0, sizeof(sc->sc_curbits));
    429 			copyin(cu->mask, (caddr_t)sc->sc_curbits[0], count);
    430 			copyin(cu->image, (caddr_t)sc->sc_curbits[1], count);
    431 			zx_cursor_set(sc);
    432 		}
    433 		break;
    434 
    435 	case FBIOGCURSOR:
    436 		cu = (struct fbcursor *)data;
    437 
    438 		cu->set = FB_CUR_SETALL;
    439 		cu->enable = ((sc->sc_flags & ZX_CURSOR) != 0);
    440 		cu->pos = sc->sc_curpos;
    441 		cu->hot = sc->sc_curhot;
    442 		cu->size = sc->sc_cursize;
    443 
    444 		if (cu->image != NULL) {
    445 			count = sc->sc_cursize.y * 4;
    446 			rv = copyout((caddr_t)sc->sc_curbits[1],
    447 			    (caddr_t)cu->image, count);
    448 			if (rv)
    449 				return (rv);
    450 			rv = copyout((caddr_t)sc->sc_curbits[0],
    451 			    (caddr_t)cu->mask, count);
    452 			if (rv)
    453 				return (rv);
    454 		}
    455 		if (cu->cmap.red != NULL) {
    456 			if (cu->cmap.index > 2 ||
    457 			    cu->cmap.count > 2 - cu->cmap.index)
    458 				return (EINVAL);
    459 			for (i = 0; i < cu->cmap.count; i++) {
    460 				v = sc->sc_curcmap[i + cu->cmap.index + 0];
    461 				if (subyte(&cu->cmap.red[i], v))
    462 					return (EFAULT);
    463 				v = sc->sc_curcmap[i + cu->cmap.index + 2];
    464 				if (subyte(&cu->cmap.green[i], v))
    465 					return (EFAULT);
    466 				v = sc->sc_curcmap[i + cu->cmap.index + 4];
    467 				if (subyte(&cu->cmap.blue[i], v))
    468 					return (EFAULT);
    469 			}
    470 		} else {
    471 			cu->cmap.index = 0;
    472 			cu->cmap.count = 2;
    473 		}
    474 		break;
    475 
    476 	default:
    477 #ifdef DEBUG
    478 		log(LOG_NOTICE, "zxioctl(0x%lx) (%s[%d])\n", cmd,
    479 		    p->p_comm, p->p_pid);
    480 #endif
    481 		return (ENOTTY);
    482 	}
    483 
    484 	return (0);
    485 }
    486 
    487 int
    488 zx_intr(void *cookie)
    489 {
    490 
    491 	return (1);
    492 }
    493 
    494 void
    495 zx_reset(struct zx_softc *sc)
    496 {
    497 	volatile struct zx_draw *zd;
    498 	volatile struct zx_command *zc;
    499 	struct fbtype *fbt;
    500 	u_int i;
    501 
    502 	zd = sc->sc_zd_ss0;
    503 	zc = sc->sc_zc;
    504 	fbt = &sc->sc_fb.fb_type;
    505 
    506 	zx_cross_loadwid(sc, ZX_WID_DBL_8, 0, 0x2c0);
    507 	zx_cross_loadwid(sc, ZX_WID_DBL_8, 1, 0x30);
    508 	zx_cross_loadwid(sc, ZX_WID_DBL_8, 2, 0x20);
    509 	zx_cross_loadwid(sc, ZX_WID_DBL_24, 1, 0x30);
    510 
    511 	i = sc->sc_zd_ss1->zd_misc;
    512 	i |= ZX_SS1_MISC_ENABLE;
    513 	SETREG(sc->sc_zd_ss1->zd_misc, i);
    514 
    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 	SETREG(zd->zd_widclip, 0);
    523 
    524 	SETREG(zc->zc_extent,
    525 	    (fbt->fb_width - 1) | ((fbt->fb_height - 1) << 11));
    526 	SETREG(zc->zc_addrspace, ZX_ADDRSPC_FONT_OBGR);
    527 	SETREG(zc->zc_fontt, 0);
    528 
    529 	for (i = 0; i < 256; i++) {
    530 		sc->sc_cmap[i] = rasops_cmap[i * 3];
    531 		sc->sc_cmap[i + 256] = rasops_cmap[i * 3 + 1];
    532 		sc->sc_cmap[i + 512] = rasops_cmap[i * 3 + 2];
    533 	}
    534 
    535 	zx_cmap_put(sc);
    536 }
    537 
    538 int
    539 zx_cross_wait(struct zx_softc *sc)
    540 {
    541 	volatile struct zx_cross *zx;
    542 	int i;
    543 
    544 	zx = sc->sc_zx;
    545 
    546 	for (i = 300000; i != 0; i--) {
    547 		if ((zx->zx_csr & ZX_CROSS_CSR_PROGRESS) == 0)
    548 			break;
    549 		DELAY(1);
    550 	}
    551 
    552 	if (i == 0)
    553 		printf("zx_cross_wait: timed out\n");
    554 
    555 	return (i);
    556 }
    557 
    558 int
    559 zx_cross_loadwid(struct zx_softc *sc, u_int type, u_int index, u_int value)
    560 {
    561 	volatile struct zx_cross *zx;
    562 	u_int tmp;
    563 
    564 	zx = sc->sc_zx;
    565 	SETREG(zx->zx_type, ZX_CROSS_TYPE_WID);
    566 
    567 	if (zx_cross_wait(sc))
    568 		return (1);
    569 
    570 	if (type == ZX_WID_DBL_8)
    571 		tmp = (index & 0x0f) + 0x40;
    572 	else if (type == ZX_WID_DBL_24)
    573 		tmp = index & 0x3f;
    574 
    575 	SETREG(zx->zx_type, 0x5800 + tmp);
    576 	SETREG(zx->zx_value, value);
    577 	SETREG(zx->zx_type, ZX_CROSS_TYPE_WID);
    578 	SETREG(zx->zx_csr, ZX_CROSS_CSR_UNK | ZX_CROSS_CSR_UNK2);
    579 
    580 	return (0);
    581 }
    582 
    583 int
    584 zx_cmap_put(struct zx_softc *sc)
    585 {
    586 	volatile struct zx_cross *zx;
    587 	const u_char *b;
    588 	u_int i, t;
    589 
    590 	zx = sc->sc_zx;
    591 
    592 	SETREG(zx->zx_type, ZX_CROSS_TYPE_CLUT0);
    593 	if (zx_cross_wait(sc))
    594 		return (1);
    595 
    596 	SETREG(zx->zx_type, ZX_CROSS_TYPE_CLUTDATA);
    597 
    598 	for (i = 0, b = sc->sc_cmap; i < 256; i++) {
    599 		t = b[i];
    600 		t |= b[i + 256] << 8;
    601 		t |= b[i + 512] << 16;
    602 		SETREG(zx->zx_value, t);
    603 	}
    604 
    605 	SETREG(zx->zx_type, ZX_CROSS_TYPE_CLUT0);
    606 	i = zx->zx_csr;
    607 	i = i | ZX_CROSS_CSR_UNK | ZX_CROSS_CSR_UNK2;
    608 	SETREG(zx->zx_csr, i);
    609 	return (0);
    610 }
    611 
    612 void
    613 zx_cursor_move(struct zx_softc *sc)
    614 {
    615 	volatile struct zx_cursor *zcu;
    616 	int sx, sy, x, y;
    617 
    618 	x = sc->sc_curpos.x - sc->sc_curhot.x;
    619 	y = sc->sc_curpos.y - sc->sc_curhot.y;
    620 	zcu = sc->sc_zcu;
    621 
    622 	if (x < 0) {
    623 		sx = min(-x, 32);
    624 		x = 0;
    625 	} else
    626 		sx = 0;
    627 
    628 	if (y < 0) {
    629 		sy = min(-y, 32);
    630 		y = 0;
    631 	} else
    632 		sy = 0;
    633 
    634 	if (sx != sc->sc_shiftx || sy != sc->sc_shifty) {
    635 		sc->sc_shiftx = sx;
    636 		sc->sc_shifty = sy;
    637 		zx_cursor_set(sc);
    638 	}
    639 
    640 	SETREG(zcu->zcu_sxy, ((y & 0x7ff) << 11) | (x & 0x7ff));
    641 	SETREG(zcu->zcu_misc, zcu->zcu_misc | 0x30);
    642 
    643 	/* XXX Necessary? */
    644 	SETREG(zcu->zcu_misc, zcu->zcu_misc | 0x80);
    645 }
    646 
    647 void
    648 zx_cursor_set(struct zx_softc *sc)
    649 {
    650 	volatile struct zx_cursor *zcu;
    651 	int i, j, data;
    652 
    653 	zcu = sc->sc_zcu;
    654 	SETREG(zcu->zcu_type, 0);
    655 
    656 	for (j = 0; j < 2; j++) {
    657 		for (i = sc->sc_shifty; i < 32; i++) {
    658 			data = sc->sc_curbits[j][i];
    659 			SETREG(zcu->zcu_data, data >> sc->sc_shiftx);
    660 		}
    661 		for (i = sc->sc_shifty; i != 0; i--)
    662 			SETREG(zcu->zcu_data, 0);
    663 	}
    664 }
    665 
    666 void
    667 zx_cursor_blank(struct zx_softc *sc)
    668 {
    669 	volatile struct zx_cursor *zcu;
    670 
    671 	sc->sc_flags &= ~ZX_CURSOR;
    672 	zcu = sc->sc_zcu;
    673 	SETREG(zcu->zcu_misc, zcu->zcu_misc & ~0x80);
    674 }
    675 
    676 void
    677 zx_cursor_unblank(struct zx_softc *sc)
    678 {
    679 	volatile struct zx_cursor *zcu;
    680 
    681 	sc->sc_flags |= ZX_CURSOR;
    682 	zcu = sc->sc_zcu;
    683 	SETREG(zcu->zcu_misc, zcu->zcu_misc | 0x80);
    684 }
    685 
    686 void
    687 zx_cursor_color(struct zx_softc *sc)
    688 {
    689 	volatile struct zx_cursor *zcu;
    690 	u_int8_t tmp;
    691 
    692 	zcu = sc->sc_zcu;
    693 
    694 	SETREG(zcu->zcu_type, 0x50);
    695 
    696 	tmp = sc->sc_curcmap[0] | (sc->sc_curcmap[2] << 8) |
    697 	    (sc->sc_curcmap[4] << 16);
    698 	SETREG(zcu->zcu_data, tmp);
    699 
    700 	tmp = sc->sc_curcmap[1] | (sc->sc_curcmap[3] << 8) |
    701 	    (sc->sc_curcmap[5] << 16);
    702 	SETREG(zcu->zcu_data, sc->sc_curcmap[1]);
    703 
    704 	SETREG(zcu->zcu_misc, zcu->zcu_misc | 0x03);
    705 }
    706 
    707 void
    708 zx_blank(struct device *dv)
    709 {
    710 	struct zx_softc *sc;
    711 	volatile struct zx_cross *zx;
    712 
    713 	sc = (struct zx_softc *)dv;
    714 
    715 	if ((sc->sc_flags & ZX_BLANKED) != 0)
    716 		return;
    717 	sc->sc_flags |= ZX_BLANKED;
    718 
    719 	zx = sc->sc_zx;
    720 	SETREG(zx->zx_type, ZX_CROSS_TYPE_VIDEO);
    721 	SETREG(zx->zx_csr, zx->zx_csr & ~ZX_CROSS_CSR_ENABLE);
    722 }
    723 
    724 void
    725 zx_unblank(struct device *dv)
    726 {
    727 	struct zx_softc *sc;
    728 	volatile struct zx_cross *zx;
    729 
    730 	sc = (struct zx_softc *)dv;
    731 
    732 	if ((sc->sc_flags & ZX_BLANKED) == 0)
    733 		return;
    734 	sc->sc_flags &= ~ZX_BLANKED;
    735 
    736 	zx = sc->sc_zx;
    737 	SETREG(zx->zx_type, ZX_CROSS_TYPE_VIDEO);
    738 	SETREG(zx->zx_csr, zx->zx_csr | ZX_CROSS_CSR_ENABLE);
    739 }
    740 
    741 paddr_t
    742 zxmmap(dev_t dev, off_t off, int prot)
    743 {
    744 	struct zx_softc *sc;
    745 	const struct zx_mmo *mm, *max;
    746 
    747 	sc = device_lookup(&zx_cd, minor(dev));
    748 	off = round_page(off);
    749 	mm = zx_mmo;
    750 	max = mm + sizeof(zx_mmo) / sizeof(zx_mmo[0]);
    751 
    752 	for (; mm < max; mm++)
    753 		if (off >= mm->mo_va && off < mm->mo_va + mm->mo_size) {
    754 			off = off - mm->mo_va + mm->mo_pa;
    755 			return (bus_space_mmap(sc->sc_bt, sc->sc_paddr,
    756 			    off, prot, BUS_SPACE_MAP_LINEAR));
    757 		}
    758 
    759 	return (-1);
    760 }
    761 
    762 void
    763 zx_fillrect(struct rasops_info *ri, int x, int y, int w, int h, long attr,
    764 	    int rop)
    765 {
    766 	struct zx_softc *sc;
    767 	volatile struct zx_command *zc;
    768 	volatile struct zx_draw *zd;
    769 	int fg, bg;
    770 
    771 	sc = ri->ri_hw;
    772 	zc = sc->sc_zc;
    773 	zd = sc->sc_zd_ss0;
    774 
    775 	rasops_unpack_attr(attr, &fg, &bg, NULL);
    776 	x = x * sc->sc_fontw + ri->ri_xorigin;
    777 	y = y * sc->sc_fonth + ri->ri_yorigin;
    778 	w = sc->sc_fontw * w - 1;
    779 	h = sc->sc_fonth * h - 1;
    780 
    781 	while ((zc->zc_csr & ZX_CSR_BLT_BUSY) != 0)
    782 		;
    783 
    784 	SETREG(zd->zd_rop, rop);
    785 	SETREG(zd->zd_fg, (bg & 7) ? 0x00000000 : 0xff000000);
    786 	SETREG(zc->zc_extent, w | (h << 11));
    787 	SETREG(zc->zc_fill, x | (y << 11) | 0x80000000);
    788 }
    789 
    790 void
    791 zx_copyrect(struct rasops_info *ri, int sx, int sy, int dx, int dy, int w,
    792 	    int h)
    793 {
    794 	struct zx_softc *sc;
    795 	volatile struct zx_command *zc;
    796 	volatile struct zx_draw *zd;
    797 	int dir;
    798 
    799 	sc = ri->ri_hw;
    800 	zc = sc->sc_zc;
    801 	zd = sc->sc_zd_ss0;
    802 
    803 	sx = sx * sc->sc_fontw + ri->ri_xorigin;
    804 	sy = sy * sc->sc_fonth + ri->ri_yorigin;
    805 	dx = dx * sc->sc_fontw + ri->ri_xorigin;
    806 	dy = dy * sc->sc_fonth + ri->ri_yorigin;
    807 	w = w * sc->sc_fontw - 1;
    808 	h = h * sc->sc_fonth - 1;
    809 
    810 	if (sy < dy || sx < dx) {
    811 		dir = 0x80000000;
    812 		sx += w;
    813 		sy += h;
    814 		dx += w;
    815 		dy += h;
    816 	} else
    817 		dir = 0;
    818 
    819 	while ((zc->zc_csr & ZX_CSR_BLT_BUSY) != 0)
    820 		;
    821 
    822 	SETREG(zd->zd_rop, ZX_STD_ROP);
    823 	SETREG(zc->zc_extent, w | (h << 11) | dir);
    824 	SETREG(zc->zc_src, sx | (sy << 11));
    825 	SETREG(zc->zc_copy, dx | (dy << 11));
    826 }
    827 
    828 void
    829 zx_do_cursor(struct rasops_info *ri)
    830 {
    831 
    832 	zx_fillrect(ri, ri->ri_ccol, ri->ri_crow, 1, 1, 0,
    833 	    ZX_ROP_NEW_XOR_OLD | ZX_ATTR_WE_ENABLE | ZX_ATTR_OE_ENABLE |
    834 	    ZX_ATTR_FORCE_WID);
    835 }
    836 
    837 void
    838 zx_erasecols(void *cookie, int row, int col, int num, long attr)
    839 {
    840 	struct rasops_info *ri;
    841 
    842 	ri = (struct rasops_info *)cookie;
    843 
    844 	zx_fillrect(ri, col, row, num, 1, attr, ZX_STD_ROP);
    845 }
    846 
    847 void
    848 zx_eraserows(void *cookie, int row, int num, long attr)
    849 {
    850 	struct rasops_info *ri;
    851 
    852 	ri = (struct rasops_info *)cookie;
    853 
    854 	zx_fillrect(ri, 0, row, ri->ri_cols, num, attr, ZX_STD_ROP);
    855 }
    856 
    857 void
    858 zx_copyrows(void *cookie, int src, int dst, int num)
    859 {
    860 	struct rasops_info *ri;
    861 
    862 	ri = (struct rasops_info *)cookie;
    863 
    864 	zx_copyrect(ri, 0, src, 0, dst, ri->ri_cols, num);
    865 }
    866 
    867 void
    868 zx_copycols(void *cookie, int row, int src, int dst, int num)
    869 {
    870 	struct rasops_info *ri;
    871 
    872 	ri = (struct rasops_info *)cookie;
    873 
    874 	zx_copyrect(ri, src, row, dst, row, num, 1);
    875 }
    876 
    877 void
    878 zx_putchar(void *cookie, int row, int col, u_int uc, long attr)
    879 {
    880 	struct rasops_info *ri;
    881 	struct zx_softc *sc;
    882 	struct wsdisplay_font *font;
    883 	volatile struct zx_command *zc;
    884 	volatile struct zx_draw *zd;
    885 	volatile u_int32_t *dp;
    886 	u_int8_t *fb;
    887 	int fs, i, fg, bg, ul;
    888 
    889 	ri = (struct rasops_info *)cookie;
    890 
    891 	if (uc == ' ') {
    892 		zx_fillrect(ri, col, row, 1, 1, attr, ZX_STD_ROP);
    893 		return;
    894 	}
    895 
    896 	sc = (struct zx_softc *)ri->ri_hw;
    897 	font = ri->ri_font;
    898 	zc = sc->sc_zc;
    899 	zd = sc->sc_zd_ss0;
    900 
    901 	dp = (volatile u_int32_t *)sc->sc_pixels +
    902 	    ((row * sc->sc_fonth + ri->ri_yorigin) << 11) +
    903 	    (col * sc->sc_fontw + ri->ri_xorigin);
    904 	fb = (u_int8_t *)font->data + (uc - font->firstchar) *
    905 	    ri->ri_fontscale;
    906 	fs = font->stride;
    907 	rasops_unpack_attr(attr, &fg, &bg, &ul);
    908 
    909 	while ((zc->zc_csr & ZX_CSR_BLT_BUSY) != 0)
    910 		;
    911 
    912 	SETREG(zd->zd_rop, ZX_STD_ROP);
    913 	SETREG(zd->zd_fg, (fg & 7) ? 0x00000000 : 0xff000000);
    914 	SETREG(zd->zd_bg, (bg & 7) ? 0x00000000 : 0xff000000);
    915 	SETREG(zc->zc_fontmsk, 0xffffffff << (32 - sc->sc_fontw));
    916 
    917 	if (sc->sc_fontw <= 8) {
    918 		for (i = sc->sc_fonth; i != 0; i--, dp += 2048) {
    919 			*dp = *fb << 24;
    920 			fb += fs;
    921 		}
    922 	} else {
    923 		for (i = sc->sc_fonth; i != 0; i--, dp += 2048) {
    924 			*dp = *((u_int16_t *)fb) << 16;
    925 			fb += fs;
    926 		}
    927 	}
    928 
    929 	if (ul) {
    930 		dp -= 4096;
    931 		*dp = 0xffffffff;
    932 	}
    933 }
    934