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