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