Home | History | Annotate | Line # | Download | only in sbus
tcx.c revision 1.34
      1 /*	$NetBSD: tcx.c,v 1.34 2009/08/19 03:35:32 macallan Exp $ */
      2 
      3 /*
      4  *  Copyright (c) 1996,1998 The NetBSD Foundation, Inc.
      5  *  All rights reserved.
      6  *
      7  *  This code is derived from software contributed to The NetBSD Foundation
      8  *  by Paul Kranenburg and Michael Lorenz.
      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  * color display (TCX) driver.
     34  *
     35  * Does not handle interrupts, even though they can occur.
     36  *
     37  * XXX should defer colormap updates to vertical retrace interrupts
     38  */
     39 
     40 #include <sys/cdefs.h>
     41 __KERNEL_RCSID(0, "$NetBSD: tcx.c,v 1.34 2009/08/19 03:35:32 macallan Exp $");
     42 
     43 /*
     44  * define for cg8 emulation on S24 (24-bit version of tcx) for the SS5;
     45  * it is bypassed on the 8-bit version (onboard framebuffer for SS4)
     46  */
     47 #undef TCX_CG8
     48 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/buf.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 
     59 #ifdef DEBUG
     60 #include <sys/proc.h>
     61 #include <sys/syslog.h>
     62 #endif
     63 
     64 #include <sys/bus.h>
     65 #include <machine/autoconf.h>
     66 
     67 #include <dev/sun/fbio.h>
     68 #include <dev/sun/fbvar.h>
     69 #include <dev/sun/btreg.h>
     70 #include <dev/sun/btvar.h>
     71 
     72 #include <dev/sbus/sbusvar.h>
     73 #include <dev/sbus/tcxreg.h>
     74 
     75 #include <dev/wscons/wsdisplayvar.h>
     76 #include <dev/wscons/wsconsio.h>
     77 #include <dev/wsfont/wsfont.h>
     78 #include <dev/rasops/rasops.h>
     79 
     80 #include <dev/wscons/wsdisplay_vconsvar.h>
     81 
     82 #include "opt_wsemul.h"
     83 
     84 /* per-display variables */
     85 struct tcx_softc {
     86 	device_t	sc_dev;		/* base device */
     87 	struct sbusdev	sc_sd;		/* sbus device */
     88 	struct fbdevice	sc_fb;		/* frame buffer device */
     89 	bus_space_tag_t	sc_bustag;
     90 	struct openprom_addr sc_physaddr[TCX_NREG];/* phys addr of h/w */
     91 
     92 	bus_space_handle_t sc_bt;	/* Brooktree registers */
     93 	bus_space_handle_t sc_thc;	/* THC registers */
     94 	uint8_t *sc_fbaddr;		/* framebuffer */
     95 	uint64_t *sc_rblit;		/* blitspace */
     96 	uint64_t *sc_rstip;		/* stipple space */
     97 
     98 	short	sc_8bit;		/* true if 8-bit hardware */
     99 	short	sc_blanked;		/* true if blanked */
    100 	u_char	sc_cmap_red[256];
    101 	u_char	sc_cmap_green[256];
    102 	u_char	sc_cmap_blue[256];
    103 	int 	sc_mode, sc_bg;
    104 	struct vcons_data vd;
    105 };
    106 
    107 static struct vcons_screen tcx_console_screen;
    108 
    109 extern const u_char rasops_cmap[768];
    110 
    111 struct wsscreen_descr tcx_defscreendesc = {
    112 	"default",
    113 	0, 0,
    114 	NULL,
    115 	8, 16,
    116 	WSSCREEN_WSCOLORS,
    117 };
    118 
    119 const struct wsscreen_descr *_tcx_scrlist[] = {
    120 	&tcx_defscreendesc,
    121 	/* XXX other formats, graphics screen? */
    122 };
    123 
    124 struct wsscreen_list tcx_screenlist = {
    125 	sizeof(_tcx_scrlist) / sizeof(struct wsscreen_descr *),
    126 	_tcx_scrlist
    127 };
    128 
    129 /*
    130  * The S24 provides the framebuffer RAM mapped in three ways:
    131  * 26 bits per pixel, in 32-bit words; the low-order 24 bits are
    132  * blue, green, and red values, and the other two bits select the
    133  * display modes, per pixel);
    134  * 24 bits per pixel, in 32-bit words; the high-order byte reads as
    135  * zero, and is ignored on writes (so the mode bits cannot be altered);
    136  * 8 bits per pixel, unpadded; writes to this space do not modify the
    137  * other 18 bits.
    138  */
    139 #define TCX_CTL_8_MAPPED	0x00000000	/* 8 bits, uses color map */
    140 #define TCX_CTL_24_MAPPED	0x01000000	/* 24 bits, uses color map */
    141 #define TCX_CTL_24_LEVEL	0x03000000	/* 24 bits, ignores color map */
    142 #define TCX_CTL_PIXELMASK	0x00FFFFFF	/* mask for index/level */
    143 
    144 /* autoconfiguration driver */
    145 static void	tcxattach(device_t, device_t, void *);
    146 static int	tcxmatch(device_t, cfdata_t, void *);
    147 static void	tcx_unblank(device_t);
    148 
    149 CFATTACH_DECL_NEW(tcx, sizeof(struct tcx_softc),
    150     tcxmatch, tcxattach, NULL, NULL);
    151 
    152 extern struct cfdriver tcx_cd;
    153 
    154 dev_type_open(tcxopen);
    155 dev_type_close(tcxclose);
    156 dev_type_ioctl(tcxioctl);
    157 dev_type_mmap(tcxmmap);
    158 
    159 const struct cdevsw tcx_cdevsw = {
    160 	tcxopen, tcxclose, noread, nowrite, tcxioctl,
    161 	nostop, notty, nopoll, tcxmmap, nokqfilter,
    162 };
    163 
    164 /* frame buffer generic driver */
    165 static struct fbdriver tcx_fbdriver = {
    166 	tcx_unblank, tcxopen, tcxclose, tcxioctl, nopoll, tcxmmap,
    167 	nokqfilter
    168 };
    169 
    170 static void tcx_reset(struct tcx_softc *);
    171 static void tcx_loadcmap(struct tcx_softc *, int, int);
    172 
    173 static int	tcx_ioctl(void *, void *, u_long, void *, int, struct lwp *);
    174 static paddr_t	tcx_mmap(void *, void *, off_t, int);
    175 
    176 static void	tcx_init_screen(void *, struct vcons_screen *, int, long *);
    177 static void	tcx_clearscreen(struct tcx_softc *);
    178 static void	tcx_copyrows(void *, int, int, int);
    179 static void	tcx_eraserows(void *, int, int, long);
    180 static void	tcx_putchar(void *, int, int, u_int, long);
    181 
    182 struct wsdisplay_accessops tcx_accessops = {
    183 	tcx_ioctl,
    184 	tcx_mmap,
    185 	NULL,	/* vcons_alloc_screen */
    186 	NULL,	/* vcons_free_screen */
    187 	NULL,	/* vcons_show_screen */
    188 	NULL,	/* load_font */
    189 	NULL,	/* polls */
    190 	NULL,	/* scroll */
    191 };
    192 
    193 #define OBPNAME	"SUNW,tcx"
    194 
    195 #ifdef TCX_CG8
    196 /*
    197  * For CG8 emulation, we map the 32-bit-deep framebuffer at an offset of
    198  * 256K; the cg8 space begins with a mono overlay plane and an overlay
    199  * enable plane (128K bytes each, 1 bit per pixel), immediately followed
    200  * by the color planes, 32 bits per pixel.  We also map just the 32-bit
    201  * framebuffer at 0x04000000 (TCX_USER_RAM_COMPAT), for compatibility
    202  * with the cg8 driver.
    203  */
    204 #define	TCX_CG8OVERLAY	(256 * 1024)
    205 #define	TCX_SIZE_DFB32	(1152 * 900 * 4) /* max size of the framebuffer */
    206 #endif
    207 
    208 /*
    209  * Match a tcx.
    210  */
    211 int
    212 tcxmatch(device_t parent, cfdata_t cf, void *aux)
    213 {
    214 	struct sbus_attach_args *sa = aux;
    215 
    216 	return (strcmp(sa->sa_name, OBPNAME) == 0);
    217 }
    218 
    219 /*
    220  * Attach a display.
    221  */
    222 void
    223 tcxattach(device_t parent, device_t self, void *args)
    224 {
    225 	struct tcx_softc *sc = device_private(self);
    226 	struct sbus_attach_args *sa = args;
    227 	struct wsemuldisplaydev_attach_args aa;
    228 	struct rasops_info *ri;
    229 	unsigned long defattr;
    230 	int node, ramsize;
    231 	struct fbdevice *fb = &sc->sc_fb;
    232 	bus_space_handle_t bh;
    233 	int isconsole, i, j;
    234 	uint32_t confreg;
    235 
    236 	sc->sc_dev = self;
    237 	sc->sc_bustag = sa->sa_bustag;
    238 	node = sa->sa_node;
    239 
    240 	fb->fb_driver = &tcx_fbdriver;
    241 	fb->fb_device = sc->sc_dev;
    242 	/* Mask out invalid flags from the user. */
    243 	fb->fb_flags = device_cfdata(sc->sc_dev)->cf_flags & FB_USERMASK;
    244 	/*
    245 	 * The onboard framebuffer on the SS4 supports only 8-bit mode;
    246 	 * it can be distinguished from the S24 card for the SS5 by the
    247 	 * presence of the "tcx-8-bit" attribute on the SS4 version.
    248 	 */
    249 	sc->sc_8bit = node_has_property(node, "tcx-8-bit");
    250 	fb->fb_type.fb_depth = 8;
    251 	fb_setsize_obp(fb, fb->fb_type.fb_depth, 1152, 900, node);
    252 
    253 	if (sc->sc_8bit) {
    254 		printf(" (8bit only TCX)");
    255 		ramsize = 1024 * 1024;
    256 		/* XXX - fix THC and TEC offsets */
    257 		sc->sc_physaddr[TCX_REG_TEC].oa_base += 0x1000;
    258 		sc->sc_physaddr[TCX_REG_THC].oa_base += 0x1000;
    259 	} else {
    260 		printf(" (S24)\n");
    261 		ramsize = 4 * 1024 * 1024;
    262 	}
    263 
    264 	fb->fb_type.fb_cmsize = 256;
    265 	fb->fb_type.fb_size = ramsize;
    266 	printf(": %s, %d x %d", OBPNAME,
    267 		fb->fb_type.fb_width,
    268 		fb->fb_type.fb_height);
    269 
    270 	fb->fb_type.fb_type = FBTYPE_SUNTCX;
    271 
    272 
    273 	if (sa->sa_nreg != TCX_NREG) {
    274 		printf("%s: only %d register sets\n",
    275 			device_xname(self), sa->sa_nreg);
    276 		return;
    277 	}
    278 	memcpy(sc->sc_physaddr, sa->sa_reg,
    279 	      sa->sa_nreg * sizeof(struct openprom_addr));
    280 
    281 	/* Map the register banks we care about */
    282 	if (sbus_bus_map(sa->sa_bustag,
    283 			 sc->sc_physaddr[TCX_REG_THC].oa_space,
    284 			 sc->sc_physaddr[TCX_REG_THC].oa_base,
    285 			 0x1000,
    286 			 BUS_SPACE_MAP_LINEAR, &sc->sc_thc) != 0) {
    287 		printf("tcxattach: cannot map thc registers\n");
    288 		return;
    289 	}
    290 
    291 	if (sbus_bus_map(sa->sa_bustag,
    292 			 sc->sc_physaddr[TCX_REG_CMAP].oa_space,
    293 			 sc->sc_physaddr[TCX_REG_CMAP].oa_base,
    294 			 0x1000,
    295 			 BUS_SPACE_MAP_LINEAR, &sc->sc_bt) != 0) {
    296 		printf("tcxattach: cannot map bt registers\n");
    297 		return;
    298 	}
    299 
    300 	/* map the 8bit dumb FB for the console */
    301 	if (sbus_bus_map(sa->sa_bustag,
    302 		 sc->sc_physaddr[TCX_REG_DFB8].oa_space,
    303 		 sc->sc_physaddr[TCX_REG_DFB8].oa_base,
    304 			 1024 * 1024,
    305 			 BUS_SPACE_MAP_LINEAR,
    306 			 &bh) != 0) {
    307 		printf("tcxattach: cannot map framebuffer\n");
    308 		return;
    309 	}
    310 	sc->sc_fbaddr = bus_space_vaddr(sa->sa_bustag, bh);
    311 
    312 	/* RBLIT space */
    313 	if (sbus_bus_map(sa->sa_bustag,
    314 		 sc->sc_physaddr[TCX_REG_RBLIT].oa_space,
    315 		 sc->sc_physaddr[TCX_REG_RBLIT].oa_base,
    316 			 8 * 1024 * 1024,
    317 			 BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_LARGE,
    318 			 &bh) != 0) {
    319 		printf("tcxattach: cannot map RBLIT space\n");
    320 		return;
    321 	}
    322 	sc->sc_rblit = bus_space_vaddr(sa->sa_bustag, bh);
    323 
    324 	/* RSTIP space */
    325 	if (sbus_bus_map(sa->sa_bustag,
    326 		 sc->sc_physaddr[TCX_REG_RSTIP].oa_space,
    327 		 sc->sc_physaddr[TCX_REG_RSTIP].oa_base,
    328 			 8 * 1024 * 1024,
    329 			 BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_LARGE,
    330 			 &bh) != 0) {
    331 		printf("tcxattach: cannot map RSTIP space\n");
    332 		return;
    333 	}
    334 	sc->sc_rstip = bus_space_vaddr(sa->sa_bustag, bh);
    335 
    336 	isconsole = fb_is_console(node);
    337 
    338 	confreg = bus_space_read_4(sa->sa_bustag, sc->sc_thc, THC_CONFIG);
    339 	printf(", id %d, rev %d, sense %d",
    340 		(confreg & THC_CFG_FBID) >> THC_CFG_FBID_SHIFT,
    341 		(confreg & THC_CFG_REV) >> THC_CFG_REV_SHIFT,
    342 		(confreg & THC_CFG_SENSE) >> THC_CFG_SENSE_SHIFT
    343 	);
    344 
    345 	/* reset cursor & frame buffer controls */
    346 	tcx_reset(sc);
    347 
    348 	/* Initialize the default color map. */
    349 	j = 0;
    350 	for (i = 0; i < 256; i++) {
    351 
    352 		sc->sc_cmap_red[i] = rasops_cmap[j];
    353 		sc->sc_cmap_green[i] = rasops_cmap[j + 1];
    354 		sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
    355 		j += 3;
    356 	}
    357 	tcx_loadcmap(sc, 0, 256);
    358 
    359 	/* enable video */
    360 	confreg = bus_space_read_4(sa->sa_bustag, sc->sc_thc, THC_MISC);
    361 	confreg |= THC_MISC_VIDEN;
    362 	bus_space_write_4(sa->sa_bustag, sc->sc_thc, THC_MISC, confreg);
    363 
    364 	if (isconsole) {
    365 		printf(" (console)\n");
    366 	} else
    367 		printf("\n");
    368 
    369 	sbus_establish(&sc->sc_sd, sc->sc_dev);
    370 	fb_attach(&sc->sc_fb, isconsole);
    371 
    372 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    373 	wsfont_init();
    374 
    375 	vcons_init(&sc->vd, sc, &tcx_defscreendesc, &tcx_accessops);
    376 	sc->vd.init_screen = tcx_init_screen;
    377 
    378 	vcons_init_screen(&sc->vd, &tcx_console_screen, 1, &defattr);
    379 	tcx_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    380 
    381 	sc->sc_bg = (defattr >> 16) & 0xff;
    382 	tcx_clearscreen(sc);
    383 
    384 	ri = &tcx_console_screen.scr_ri;
    385 
    386 	tcx_defscreendesc.nrows = ri->ri_rows;
    387 	tcx_defscreendesc.ncols = ri->ri_cols;
    388 	tcx_defscreendesc.textops = &ri->ri_ops;
    389 	tcx_defscreendesc.capabilities = ri->ri_caps;
    390 
    391 	if(isconsole) {
    392 		wsdisplay_cnattach(&tcx_defscreendesc, ri, 0, 0, defattr);
    393 	}
    394 
    395 	aa.console = isconsole;
    396 	aa.scrdata = &tcx_screenlist;
    397 	aa.accessops = &tcx_accessops;
    398 	aa.accesscookie = &sc->vd;
    399 
    400 	config_found(self, &aa, wsemuldisplaydevprint);
    401 	/*
    402 	 * we need to do this again - something overwrites a handful
    403 	 * palette registers and we end up with white in reg. 0
    404 	 */
    405 	tcx_loadcmap(sc, 0, 256);
    406 }
    407 
    408 int
    409 tcxopen(dev_t dev, int flags, int mode, struct lwp *l)
    410 {
    411 	return (0);
    412 }
    413 
    414 int
    415 tcxclose(dev_t dev, int flags, int mode, struct lwp *l)
    416 {
    417 	struct tcx_softc *sc = device_lookup_private(&tcx_cd, minor(dev));
    418 
    419 	tcx_reset(sc);
    420 	/* we may want to clear and redraw the console here */
    421 	return (0);
    422 }
    423 
    424 int
    425 tcxioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
    426 {
    427 	struct tcx_softc *sc = device_lookup_private(&tcx_cd, minor(dev));
    428 	//int error;
    429 
    430 	switch (cmd) {
    431 
    432 	case FBIOGTYPE:
    433 		*(struct fbtype *)data = sc->sc_fb.fb_type;
    434 		break;
    435 
    436 	case FBIOGATTR:
    437 #define fba ((struct fbgattr *)data)
    438 		fba->real_type = sc->sc_fb.fb_type.fb_type;
    439 		fba->owner = 0;		/* XXX ??? */
    440 		fba->fbtype = sc->sc_fb.fb_type;
    441 		fba->sattr.flags = 0;
    442 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
    443 		fba->sattr.dev_specific[0] = -1;
    444 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
    445 		fba->emu_types[1] = FBTYPE_SUN3COLOR;
    446 		fba->emu_types[2] = -1;
    447 #undef fba
    448 		break;
    449 
    450 	case FBIOGETCMAP:
    451 #define	p ((struct fbcmap *)data)
    452 		if (copyout(&sc->sc_cmap_red[p->index], p->red, p->count) != 0)
    453 			return EINVAL;
    454 		if (copyout(&sc->sc_cmap_green[p->index], p->green, p->count)
    455 		    != 0)
    456 			return EINVAL;
    457 		if (copyout(&sc->sc_cmap_blue[p->index], p->blue, p->count)
    458 		    != 0)
    459 			return EINVAL;
    460 		return 0;
    461 
    462 	case FBIOPUTCMAP:
    463 		/* copy to software map */
    464 		if (copyin(p->red, &sc->sc_cmap_red[p->index], p->count) != 0)
    465 			return EINVAL;
    466 		if (copyin(p->green, &sc->sc_cmap_green[p->index], p->count)
    467 		    != 0)
    468 			return EINVAL;
    469 		if (copyin(p->blue, &sc->sc_cmap_blue[p->index], p->count) != 0)
    470 			return EINVAL;
    471 		tcx_loadcmap(sc, p->index, p->count);
    472 #undef p
    473 		break;
    474 	case FBIOGVIDEO:
    475 		*(int *)data = sc->sc_blanked;
    476 		break;
    477 
    478 	case FBIOSVIDEO:
    479 		if (*(int *)data)
    480 			tcx_unblank(sc->sc_dev);
    481 		else if (!sc->sc_blanked) {
    482 			uint32_t reg;
    483 			sc->sc_blanked = 1;
    484 			reg = bus_space_read_4(sc->sc_bustag, sc->sc_thc,
    485 			    THC_MISC);
    486 			reg &= ~THC_MISC_VIDEN;
    487 			/* Put monitor in `power-saving mode' */
    488 			reg |= THC_MISC_VSYNC_DISABLE;
    489 			reg |= THC_MISC_HSYNC_DISABLE;
    490 			bus_space_write_4(sc->sc_bustag, sc->sc_thc, THC_MISC,
    491 			    reg);
    492 		}
    493 		break;
    494 
    495 	default:
    496 #ifdef DEBUG
    497 		log(LOG_NOTICE, "tcxioctl(0x%lx) (%s[%d])\n", cmd,
    498 		    l->l_proc->p_comm, l->l_proc->p_pid);
    499 #endif
    500 		return (ENOTTY);
    501 	}
    502 	return (0);
    503 }
    504 
    505 /*
    506  * Clean up hardware state (e.g., after bootup or after X crashes).
    507  */
    508 static void
    509 tcx_reset(struct tcx_softc *sc)
    510 {
    511 
    512 	/* Disable cursor in Brooktree DAC. */
    513 	bus_space_write_4(sc->sc_bustag, sc->sc_bt, DAC_ADDRESS,
    514 	    DAC_C1_CONTROL_0);
    515 	bus_space_write_4(sc->sc_bustag, sc->sc_bt, DAC_CONTROL_1, 0);
    516 }
    517 
    518 /*
    519  * Load a subset of the current (new) colormap into the color DAC.
    520  */
    521 static void
    522 tcx_loadcmap(struct tcx_softc *sc, int start, int ncolors)
    523 {
    524 	int i;
    525 
    526 	for (i = 0; i < ncolors; i++) {
    527 		bus_space_write_4(sc->sc_bustag, sc->sc_bt, DAC_ADDRESS,
    528 		    (start + i) << 24);
    529 		bus_space_write_4(sc->sc_bustag, sc->sc_bt, DAC_FB_LUT,
    530 		    sc->sc_cmap_red[i + start] << 24);
    531 		bus_space_write_4(sc->sc_bustag, sc->sc_bt, DAC_FB_LUT,
    532 		    sc->sc_cmap_green[i + start] << 24);
    533 		bus_space_write_4(sc->sc_bustag, sc->sc_bt, DAC_FB_LUT,
    534 		    sc->sc_cmap_blue[i + start] << 24);
    535 	}
    536 	bus_space_write_4(sc->sc_bustag, sc->sc_bt, DAC_ADDRESS, 0);
    537 }
    538 
    539 static void
    540 tcx_unblank(device_t dev)
    541 {
    542 	struct tcx_softc *sc = device_private(dev);
    543 
    544 	if (sc->sc_blanked) {
    545 		uint32_t reg;
    546 		sc->sc_blanked = 0;
    547 		reg = bus_space_read_4(sc->sc_bustag, sc->sc_thc, THC_MISC);
    548 		reg &= ~THC_MISC_VSYNC_DISABLE;
    549 		reg &= ~THC_MISC_HSYNC_DISABLE;
    550 		reg |= THC_MISC_VIDEN;
    551 		bus_space_write_4(sc->sc_bustag, sc->sc_thc, THC_MISC, reg);
    552 	}
    553 }
    554 
    555 /*
    556  * Base addresses at which users can mmap() the various pieces of a tcx.
    557  */
    558 #define	TCX_USER_RAM	0x00000000
    559 #define	TCX_USER_RAM24	0x01000000
    560 #define	TCX_USER_RAM_COMPAT	0x04000000	/* cg3 emulation */
    561 #define	TCX_USER_STIP	0x10000000
    562 #define	TCX_USER_BLIT	0x20000000
    563 #define	TCX_USER_RDFB32	0x28000000
    564 #define	TCX_USER_RSTIP	0x30000000
    565 #define	TCX_USER_RBLIT	0x38000000
    566 #define	TCX_USER_TEC	0x70001000
    567 #define	TCX_USER_BTREGS	0x70002000
    568 #define	TCX_USER_THC	0x70004000
    569 #define	TCX_USER_DHC	0x70008000
    570 #define	TCX_USER_ALT	0x7000a000
    571 #define	TCX_USER_UART	0x7000c000
    572 #define	TCX_USER_VRT	0x7000e000
    573 #define	TCX_USER_ROM	0x70010000
    574 
    575 struct mmo {
    576 	u_int	mo_uaddr;	/* user (virtual) address */
    577 	u_int	mo_size;	/* size, or 0 for video ram size */
    578 	u_int	mo_bank;	/* register bank number */
    579 };
    580 
    581 /*
    582  * Return the address that would map the given device at the given
    583  * offset, allowing for the given protection, or return -1 for error.
    584  *
    585  * XXX	needs testing against `demanding' applications (e.g., aviator)
    586  */
    587 paddr_t
    588 tcxmmap(dev_t dev, off_t off, int prot)
    589 {
    590 	struct tcx_softc *sc = device_lookup_private(&tcx_cd, minor(dev));
    591 	struct openprom_addr *rr = sc->sc_physaddr;
    592 	struct mmo *mo, *mo_end;
    593 	u_int u, sz;
    594 	static struct mmo mmo[] = {
    595 		{ TCX_USER_RAM, 0, TCX_REG_DFB8 },
    596 		{ TCX_USER_RAM24, 0, TCX_REG_DFB24 },
    597 		{ TCX_USER_RAM_COMPAT, 0, TCX_REG_DFB8 },
    598 
    599 		{ TCX_USER_STIP, 1, TCX_REG_STIP },
    600 		{ TCX_USER_BLIT, 1, TCX_REG_BLIT },
    601 		{ TCX_USER_RDFB32, 0, TCX_REG_RDFB32 },
    602 		{ TCX_USER_RSTIP, 1, TCX_REG_RSTIP },
    603 		{ TCX_USER_RBLIT, 1, TCX_REG_RBLIT },
    604 		{ TCX_USER_TEC, 1, TCX_REG_TEC },
    605 		{ TCX_USER_BTREGS, 8192 /* XXX */, TCX_REG_CMAP },
    606 		{ TCX_USER_THC, 0x1000, TCX_REG_THC },
    607 		{ TCX_USER_DHC, 1, TCX_REG_DHC },
    608 		{ TCX_USER_ALT, 1, TCX_REG_ALT },
    609 		{ TCX_USER_ROM, 65536, TCX_REG_ROM },
    610 	};
    611 #define NMMO (sizeof mmo / sizeof *mmo)
    612 
    613 	if (off & PGOFSET)
    614 		panic("tcxmmap");
    615 
    616 	/*
    617 	 * Entries with size 0 map video RAM (i.e., the size in fb data).
    618 	 * Entries that map 32-bit deep regions are adjusted for their
    619 	 * depth (fb_size gives the size of the 8-bit-deep region).
    620 	 *
    621 	 * Since we work in pages, the fact that the map offset table's
    622 	 * sizes are sometimes bizarre (e.g., 1) is effectively ignored:
    623 	 * one byte is as good as one page.
    624 	 */
    625 
    626 	mo = mmo;
    627 	mo_end = &mmo[NMMO];
    628 
    629 	for (; mo < mo_end; mo++) {
    630 		if ((u_int)off < mo->mo_uaddr)
    631 			continue;
    632 		u = off - mo->mo_uaddr;
    633 		sz = mo->mo_size;
    634 		if (sz == 0) {
    635 			sz = sc->sc_fb.fb_type.fb_size;
    636 			/*
    637 			 * check for the 32-bit-deep regions and adjust
    638 			 * accordingly
    639 			 */
    640 			if (mo->mo_uaddr == TCX_USER_RAM24 ||
    641 			    mo->mo_uaddr == TCX_USER_RDFB32) {
    642 				if (sc->sc_8bit) {
    643 					/*
    644 					 * not present on 8-bit hardware
    645 					 */
    646 					continue;
    647 				}
    648 				sz *= 4;
    649 			}
    650 		}
    651 		if (u < sz) {
    652 			return (bus_space_mmap(sc->sc_bustag,
    653 				BUS_ADDR(rr[mo->mo_bank].oa_space,
    654 					 rr[mo->mo_bank].oa_base),
    655 				u,
    656 				prot,
    657 				BUS_SPACE_MAP_LINEAR));
    658 		}
    659 	}
    660 	return (-1);
    661 }
    662 
    663 int
    664 tcx_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    665 	struct lwp *l)
    666 {
    667 	struct vcons_data *vd = v;
    668 	struct tcx_softc *sc = vd->cookie;
    669 	struct wsdisplay_fbinfo *wdf;
    670 	struct vcons_screen *ms = vd->active;
    671 
    672 	switch (cmd) {
    673 		case WSDISPLAYIO_GTYPE:
    674 			*(u_int *)data = WSDISPLAY_TYPE_SUNTCX;
    675 			return 0;
    676 
    677 #if 0
    678 		case FBIOGVIDEO:
    679 		case WSDISPLAYIO_GVIDEO:
    680 			*(int *)data = tcx_get_video(sc);
    681 			return 0;
    682 
    683 		case WSDISPLAYIO_SVIDEO:
    684 		case FBIOSVIDEO:
    685 			tcx_set_video(sc, *(int *)data);
    686 			return 0;
    687 #endif
    688 		case WSDISPLAYIO_GINFO:
    689 			wdf = (void *)data;
    690 			wdf->height = ms->scr_ri.ri_height;
    691 			wdf->width = ms->scr_ri.ri_width;
    692 			if (sc->sc_8bit) {
    693 				wdf->depth = 8;
    694 			} else {
    695 				wdf->depth = 32;
    696 			}
    697 			wdf->cmsize = 256;
    698 			return 0;
    699 		case WSDISPLAYIO_LINEBYTES:
    700 			{
    701 				int *ret = (int *)data;
    702 				*ret = sc->sc_8bit ? ms->scr_ri.ri_width :
    703 				    ms->scr_ri.ri_width << 2;
    704 			}
    705 			return 0;
    706 #if 0
    707 		case WSDISPLAYIO_GETCMAP:
    708 			return tcx_getcmap(sc, (struct wsdisplay_cmap *)data);
    709 
    710 		case WSDISPLAYIO_PUTCMAP:
    711 			return tcx_putcmap(sc, (struct wsdisplay_cmap *)data);
    712 #endif
    713 		case WSDISPLAYIO_SMODE:
    714 			{
    715 				int new_mode = *(int*)data;
    716 				if (new_mode != sc->sc_mode)
    717 				{
    718 					sc->sc_mode = new_mode;
    719 					if (new_mode == WSDISPLAYIO_MODE_EMUL)
    720 					{
    721 						tcx_loadcmap(sc, 0, 256);
    722 						tcx_clearscreen(sc);
    723 						vcons_redraw_screen(ms);
    724 					}
    725 				}
    726 			}
    727 	}
    728 	return EPASSTHROUGH;
    729 }
    730 
    731 static paddr_t
    732 tcx_mmap(void *v, void *vs, off_t offset, int prot)
    733 {
    734 	struct vcons_data *vd = v;
    735 	struct tcx_softc *sc = vd->cookie;
    736 
    737 	/* 'regular' framebuffer mmap()ing */
    738 	if (sc->sc_8bit) {
    739 		/* on 8Bit boards hand over the 8 bit aperture */
    740 		if (offset > 1024 * 1024)
    741 			return -1;
    742 		return bus_space_mmap(sc->sc_bustag,
    743 		    sc->sc_physaddr[TCX_REG_DFB8].oa_base + offset, 0, prot,
    744 		    BUS_SPACE_MAP_LINEAR);
    745 	} else {
    746 		/* ... but if we have a 24bit aperture we use it */
    747 		if (offset > 1024 * 1024 * 4)
    748 			return -1;
    749 		return bus_space_mmap(sc->sc_bustag,
    750 		    sc->sc_physaddr[TCX_REG_DFB24].oa_base + offset, 0, prot,
    751 		    BUS_SPACE_MAP_LINEAR);
    752 	}
    753 	return -1;
    754 }
    755 
    756 static void
    757 tcx_init_screen(void *cookie, struct vcons_screen *scr,
    758     int existing, long *defattr)
    759 {
    760 	struct tcx_softc *sc = cookie;
    761 	struct rasops_info *ri = &scr->scr_ri;
    762 
    763 	ri->ri_depth = 8;
    764 	ri->ri_width = sc->sc_fb.fb_type.fb_width;
    765 	ri->ri_height = sc->sc_fb.fb_type.fb_height;
    766 	ri->ri_stride = sc->sc_fb.fb_linebytes;
    767 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
    768 
    769 	ri->ri_bits = sc->sc_fbaddr;
    770 
    771 	rasops_init(ri, ri->ri_height/8, ri->ri_width/8);
    772 	ri->ri_caps = WSSCREEN_WSCOLORS;
    773 	rasops_reconfig(ri, ri->ri_height / ri->ri_font->fontheight,
    774 		    ri->ri_width / ri->ri_font->fontwidth);
    775 
    776 	/* enable acceleration */
    777 	ri->ri_ops.copyrows  = tcx_copyrows;
    778 	ri->ri_ops.eraserows = tcx_eraserows;
    779 	ri->ri_ops.putchar   = tcx_putchar;
    780 #if 0
    781 	ri->ri_ops.cursor    = tcx_cursor;
    782 	ri->ri_ops.copycols  = tcx_copycols;
    783 	ri->ri_ops.erasecols = tcx_erasecols;
    784 #endif
    785 }
    786 
    787 static void
    788 tcx_clearscreen(struct tcx_softc *sc)
    789 {
    790 	uint64_t bg = ((uint64_t)sc->sc_bg << 32) | 0xffffffffLL;
    791 	int i;
    792 
    793 	for (i = 0; i < 1024 * 1024; i += 32)
    794 		sc->sc_rstip[i] = bg;
    795 }
    796 
    797 static void
    798 tcx_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
    799 {
    800 	struct rasops_info *ri = cookie;
    801 	struct vcons_screen *scr = ri->ri_hw;
    802 	struct tcx_softc *sc = scr->scr_cookie;
    803 	int i, last, first, len, dest, leftover;
    804 
    805 	i = ri->ri_width * ri->ri_font->fontheight * nrows;
    806 	len = i & 0xffffe0;
    807 	leftover = i & 0x1f;
    808 	if (srcrow < dstrow) {
    809 		/* we must go bottom to top */
    810 		first = ri->ri_width *
    811 		    (ri->ri_font->fontheight * srcrow + ri->ri_yorigin);
    812 		last = first + len;
    813 		dest = ri->ri_width *
    814 		    (ri->ri_font->fontheight * dstrow + ri->ri_yorigin) + len;
    815 		if (leftover > 0) {
    816 			sc->sc_rblit[dest + 32] =
    817 			    (uint64_t)((leftover - 1) << 24) |
    818 			    (uint64_t)(i + 32);
    819 		}
    820 		for (i = last; i >= first; i -= 32) {
    821 			sc->sc_rblit[dest] = 0x300000001f000000LL | (uint64_t)i;
    822 			dest -= 32;
    823 		}
    824 	} else {
    825 		/* top to bottom */
    826 		first = ri->ri_width *
    827 		    (ri->ri_font->fontheight * srcrow + ri->ri_yorigin);
    828 		dest = ri->ri_width *
    829 		    (ri->ri_font->fontheight * dstrow + ri->ri_yorigin);
    830 		last = first + len;
    831 		for (i = first; i <= last; i+= 32) {
    832 			sc->sc_rblit[dest] = 0x300000001f000000LL | (uint64_t)i;
    833 			dest += 32;
    834 		}
    835 		if (leftover > 0) {
    836 			sc->sc_rblit[dest] =
    837 			    (uint64_t)((leftover - 1) << 24) | (uint64_t)i;
    838 		}
    839 	}
    840 }
    841 
    842 static void
    843 tcx_eraserows(void *cookie, int start, int nrows, long attr)
    844 {
    845 	struct rasops_info *ri = cookie;
    846 	struct vcons_screen *scr = ri->ri_hw;
    847 	struct tcx_softc *sc = scr->scr_cookie;
    848 	uint64_t temp;
    849 	int i, last, first, len, leftover;
    850 
    851 	i = ri->ri_width * ri->ri_font->fontheight * nrows;
    852 	len = i & 0xffffe0;
    853 	leftover = i & 0x1f;
    854 	first = ri->ri_width *
    855 	    (ri->ri_font->fontheight * start + ri->ri_yorigin);
    856 	last = first + len;
    857 	temp = 0x30000000ffffffffLL |
    858 	    ((uint64_t)ri->ri_devcmap[(attr >> 16) & 0xff] << 32);
    859 
    860 	for (i = first; i <= last; i+= 32)
    861 		sc->sc_rblit[i] = temp;
    862 
    863 	if (leftover > 0) {
    864 		temp &= 0xffffffffffffffffLL << (32 - leftover);
    865 		sc->sc_rblit[i] = temp;
    866 	}
    867 }
    868 /*
    869  * The stipple engine is 100% retarded. All drawing operations have to start
    870  * at 32 pixel boundaries so we'll have to deal with characters being split.
    871  */
    872 
    873 static void
    874 tcx_putchar(void *cookie, int row, int col, u_int c, long attr)
    875 {
    876 	struct rasops_info *ri = cookie;
    877 	struct vcons_screen *scr = ri->ri_hw;
    878 	struct tcx_softc *sc = scr->scr_cookie;
    879 	uint64_t bg, fg, temp, mask;
    880 	int addr, i, uc, shift;
    881 	uint32_t fmask;
    882 	uint8_t *cdata;
    883 	uint16_t *wdata;
    884 
    885 	addr = ri->ri_xorigin +
    886 	    col * ri->ri_font->fontwidth +
    887 	    (ri->ri_yorigin + row * ri->ri_font->fontheight) * ri->ri_width;
    888 
    889 	/* check if the character is crossing a 32 pixel boundary */
    890 	if ((addr & 0xffffe0) ==
    891 	    ((addr + ri->ri_font->fontwidth - 1) & 0xffffe0)) {
    892 		/* phew, not split */
    893 		shift = addr & 0x1f;
    894 		addr &= 0xffffe0;
    895 		fmask = 0xffffffff >> (32 - ri->ri_font->fontwidth);
    896 		fmask = fmask << (32 - ri->ri_font->fontwidth - shift);
    897 		mask = fmask;
    898 		bg = 0x3000000000000000LL |
    899 		    ((uint64_t)ri->ri_devcmap[(attr >> 16) & 0xff] &
    900 		      0xff) << 32;
    901 		bg |= mask;
    902 		temp = 0x3000000000000000LL |
    903 		    ((uint64_t)ri->ri_devcmap[(attr >> 24) & 0xff] & 0xff) <<
    904 		    	32;
    905 		uc = c - ri->ri_font->firstchar;
    906 		cdata = (uint8_t *)ri->ri_font->data + uc * ri->ri_fontscale;
    907 
    908 		if (ri->ri_font->fontwidth < 9) {
    909 			/* byte by byte */
    910 			for (i = 0; i < ri->ri_font->fontheight; i++) {
    911 				sc->sc_rstip[addr] = bg;
    912 				if (*cdata != 0) {
    913 					if (shift > 24) {
    914 						fg = (uint64_t)*cdata >>
    915 					  	  (shift - 24);
    916 					} else {
    917 						fg = (uint64_t)*cdata <<
    918 					  	  (24 - shift);
    919 					}
    920 					sc->sc_rstip[addr] = fg | temp;
    921 				}
    922 				cdata++;
    923 				addr += ri->ri_width;
    924 			}
    925 		} else if (ri->ri_font->fontwidth < 17) {
    926 			/* short by short */
    927 			wdata = (uint16_t *)cdata;
    928 			for (i = 0; i < ri->ri_font->fontheight; i++) {
    929 				sc->sc_rstip[addr] = bg;
    930 				if (*wdata != 0) {
    931 					if (shift > 16) {
    932 						fg = temp | (uint64_t)*wdata >>
    933 					  	  (shift - 16);
    934 					} else {
    935 						fg = temp | (uint64_t)*wdata <<
    936 					  	  (16 - shift);
    937 					}
    938 					sc->sc_rstip[addr] = fg;
    939 				}
    940 				wdata++;
    941 				addr += ri->ri_width;
    942 			}
    943 		}
    944 	} else {
    945 		/* and now the split case ( man this hardware is dumb ) */
    946 		uint64_t bgr, maskr, fgr;
    947 		uint32_t bork;
    948 
    949 		shift = addr & 0x1f;
    950 		addr &= 0xffffe0;
    951 		mask = 0xffffffff >> shift;
    952 		maskr = (uint64_t)(0xffffffffUL <<
    953 		    (32 - (ri->ri_font->fontwidth + shift - 32)));
    954 		bg = 0x3000000000000000LL |
    955 		    ((uint64_t)ri->ri_devcmap[(attr >> 16) & 0xff] &
    956 		      0xff) << 32;
    957 		bgr = bg | maskr;
    958 		bg |= mask;
    959 		temp = 0x3000000000000000LL |
    960 		    ((uint64_t)ri->ri_devcmap[(attr >> 24) & 0xff] & 0xff) <<
    961 		      32;
    962 
    963 		uc = c - ri->ri_font->firstchar;
    964 		cdata = (uint8_t *)ri->ri_font->data + uc * ri->ri_fontscale;
    965 
    966 		if (ri->ri_font->fontwidth < 9) {
    967 			/* byte by byte */
    968 			for (i = 0; i < ri->ri_font->fontheight; i++) {
    969 				sc->sc_rstip[addr] = bg;
    970 				sc->sc_rstip[addr + 32] = bgr;
    971 				bork = *cdata;
    972 				if (bork != 0) {
    973 					fg = (uint64_t)bork >> (shift - 24);
    974 					sc->sc_rstip[addr] = fg | temp;
    975 					fgr = (uint64_t)(bork << (52 - shift));
    976 					sc->sc_rstip[addr] = fgr | temp;
    977 				}
    978 				cdata++;
    979 				addr += ri->ri_width;
    980 			}
    981 		} else if (ri->ri_font->fontwidth < 17) {
    982 			/* short by short */
    983 			wdata = (uint16_t *)cdata;
    984 			for (i = 0; i < ri->ri_font->fontheight; i++) {
    985 				sc->sc_rstip[addr] = bg;
    986 				sc->sc_rstip[addr + 32] = bgr;
    987 				bork = *wdata;
    988 				if (bork != 0) {
    989 					fg = (uint64_t)bork >> (shift - 16);
    990 					sc->sc_rstip[addr] = fg | temp;
    991 					fgr = (uint64_t)(bork << (48 - shift));
    992 					sc->sc_rstip[addr + 32] = fgr | temp;
    993 				}
    994 				wdata++;
    995 				addr += ri->ri_width;
    996 			}
    997 		}
    998 
    999 	}
   1000 }
   1001 
   1002