xref: /netbsd-src/sys/dev/sbus/tcx.c (revision f82d7874c259b2a6cc59b714f844919f32bf7b51)
1 /*	$NetBSD: tcx.c,v 1.24 2008/04/28 20:23:57 martin 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.
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.24 2008/04/28 20:23:57 martin 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 /* per-display variables */
76 struct tcx_softc {
77 	struct device	sc_dev;		/* base device */
78 	struct sbusdev	sc_sd;		/* sbus device */
79 	struct fbdevice	sc_fb;		/* frame buffer device */
80 	bus_space_tag_t	sc_bustag;
81 	struct openprom_addr sc_physadr[TCX_NREG];/* phys addr of h/w */
82 
83 	volatile struct bt_regs *sc_bt;	/* Brooktree registers */
84 	volatile struct tcx_thc *sc_thc;/* THC registers */
85 #ifdef TCX_CG8
86 	volatile ulong *sc_cplane;	/* framebuffer with control planes */
87 #endif
88 	short	sc_8bit;		/* true if 8-bit hardware */
89 	short	sc_blanked;		/* true if blanked */
90 	union	bt_cmap sc_cmap;	/* Brooktree color map */
91 };
92 
93 /*
94  * The S24 provides the framebuffer RAM mapped in three ways:
95  * 26 bits per pixel, in 32-bit words; the low-order 24 bits are
96  * blue, green, and red values, and the other two bits select the
97  * display modes, per pixel);
98  * 24 bits per pixel, in 32-bit words; the high-order byte reads as
99  * zero, and is ignored on writes (so the mode bits cannot be altered);
100  * 8 bits per pixel, unpadded; writes to this space do not modify the
101  * other 18 bits.
102  */
103 #define TCX_CTL_8_MAPPED	0x00000000	/* 8 bits, uses color map */
104 #define TCX_CTL_24_MAPPED	0x01000000	/* 24 bits, uses color map */
105 #define TCX_CTL_24_LEVEL	0x03000000	/* 24 bits, ignores color map */
106 #define TCX_CTL_PIXELMASK	0x00FFFFFF	/* mask for index/level */
107 
108 /* autoconfiguration driver */
109 static void	tcxattach(struct device *, struct device *, void *);
110 static int	tcxmatch(struct device *, struct cfdata *, void *);
111 static void	tcx_unblank(struct device *);
112 
113 CFATTACH_DECL(tcx, sizeof(struct tcx_softc),
114     tcxmatch, tcxattach, NULL, NULL);
115 
116 extern struct cfdriver tcx_cd;
117 
118 dev_type_open(tcxopen);
119 dev_type_close(tcxclose);
120 dev_type_ioctl(tcxioctl);
121 dev_type_mmap(tcxmmap);
122 
123 const struct cdevsw tcx_cdevsw = {
124 	tcxopen, tcxclose, noread, nowrite, tcxioctl,
125 	nostop, notty, nopoll, tcxmmap, nokqfilter,
126 };
127 
128 /* frame buffer generic driver */
129 static struct fbdriver tcx_fbdriver = {
130 	tcx_unblank, tcxopen, tcxclose, tcxioctl, nopoll, tcxmmap,
131 	nokqfilter
132 };
133 
134 static void tcx_reset(struct tcx_softc *);
135 static void tcx_loadcmap(struct tcx_softc *, int, int);
136 
137 #define OBPNAME	"SUNW,tcx"
138 
139 #ifdef TCX_CG8
140 /*
141  * For CG8 emulation, we map the 32-bit-deep framebuffer at an offset of
142  * 256K; the cg8 space begins with a mono overlay plane and an overlay
143  * enable plane (128K bytes each, 1 bit per pixel), immediately followed
144  * by the color planes, 32 bits per pixel.  We also map just the 32-bit
145  * framebuffer at 0x04000000 (TCX_USER_RAM_COMPAT), for compatibility
146  * with the cg8 driver.
147  */
148 #define	TCX_CG8OVERLAY	(256 * 1024)
149 #define	TCX_SIZE_DFB32	(1152 * 900 * 4) /* max size of the framebuffer */
150 #endif
151 
152 /*
153  * Match a tcx.
154  */
155 int
156 tcxmatch(parent, cf, aux)
157 	struct device *parent;
158 	struct cfdata *cf;
159 	void *aux;
160 {
161 	struct sbus_attach_args *sa = aux;
162 
163 	return (strcmp(sa->sa_name, OBPNAME) == 0);
164 }
165 
166 /*
167  * Attach a display.
168  */
169 void
170 tcxattach(parent, self, args)
171 	struct device *parent, *self;
172 	void *args;
173 {
174 	struct tcx_softc *sc = (struct tcx_softc *)self;
175 	struct sbus_attach_args *sa = args;
176 	int node, ramsize;
177 	volatile struct bt_regs *bt;
178 	struct fbdevice *fb = &sc->sc_fb;
179 	bus_space_handle_t bh;
180 	int isconsole;
181 
182 	sc->sc_bustag = sa->sa_bustag;
183 	node = sa->sa_node;
184 
185 	fb->fb_driver = &tcx_fbdriver;
186 	fb->fb_device = &sc->sc_dev;
187 	/* Mask out invalid flags from the user. */
188 	fb->fb_flags = device_cfdata(&sc->sc_dev)->cf_flags & FB_USERMASK;
189 	/*
190 	 * The onboard framebuffer on the SS4 supports only 8-bit mode;
191 	 * it can be distinguished from the S24 card for the SS5 by the
192 	 * presence of the "tcx-8-bit" attribute on the SS4 version.
193 	 */
194 	sc->sc_8bit = node_has_property(node, "tcx-8-bit");
195 #ifdef TCX_CG8
196 	if (sc->sc_8bit) {
197 #endif
198 		/*
199 		 * cg8 emulation is either not compiled in or not supported
200 		 * on this hardware.  Report values for the 8-bit framebuffer
201 		 * so cg3 emulation works.  (If this hardware supports
202 		 * 24-bit mode, the 24-bit framebuffer will also be available)
203 		 */
204 		fb->fb_type.fb_depth = 8;
205 		fb_setsize_obp(fb, fb->fb_type.fb_depth, 1152, 900, node);
206 
207 		ramsize = fb->fb_type.fb_height * fb->fb_linebytes;
208 #ifdef TCX_CG8
209 	} else {
210 		/*
211 		 * for cg8 emulation, unconditionally report the depth as
212 		 * 32 bits, but use the height and width reported by the
213 		 * boot prom.  cg8 users want to see the full size of
214 		 * overlay planes plus color planes included in the
215 		 * reported framebuffer size.
216 		 */
217 		fb->fb_type.fb_depth = 32;
218 		fb_setsize_obp(fb, fb->fb_type.fb_depth, 1152, 900, node);
219 		fb->fb_linebytes =
220 			(fb->fb_type.fb_width * fb->fb_type.fb_depth) / 8;
221 		ramsize = TCX_CG8OVERLAY +
222 			(fb->fb_type.fb_height * fb->fb_linebytes);
223 	}
224 #endif
225 	fb->fb_type.fb_cmsize = 256;
226 	fb->fb_type.fb_size = ramsize;
227 	printf(": %s, %d x %d", OBPNAME,
228 		fb->fb_type.fb_width,
229 		fb->fb_type.fb_height);
230 #ifdef TCX_CG8
231 	/*
232 	 * if cg8 emulation is enabled, say so; but if hardware can't
233 	 * emulate cg8, explain that instead
234 	 */
235 	printf( (sc->sc_8bit)?
236 		" (8-bit only)" :
237 		" (emulating cg8)");
238 #endif
239 
240 	/*
241 	 * XXX - should be set to FBTYPE_TCX.
242 	 * XXX For CG3 emulation to work in current (96/6) X11 servers,
243 	 * XXX `fbtype' must point to an "unregocnised" entry.
244 	 */
245 #ifdef TCX_CG8
246 	if (sc->sc_8bit) {
247 		fb->fb_type.fb_type = FBTYPE_RESERVED3;
248 	} else {
249 		fb->fb_type.fb_type = FBTYPE_MEMCOLOR;
250 	}
251 #else
252 	fb->fb_type.fb_type = FBTYPE_RESERVED3;
253 #endif
254 
255 
256 	if (sa->sa_nreg != TCX_NREG) {
257 		printf("%s: only %d register sets\n",
258 			device_xname(self), sa->sa_nreg);
259 		return;
260 	}
261 	bcopy(sa->sa_reg, sc->sc_physadr,
262 	      sa->sa_nreg * sizeof(struct openprom_addr));
263 
264 	/* XXX - fix THC and TEC offsets */
265 	sc->sc_physadr[TCX_REG_TEC].oa_base += 0x1000;
266 	sc->sc_physadr[TCX_REG_THC].oa_base += 0x1000;
267 
268 	/* Map the register banks we care about */
269 	if (sbus_bus_map(sa->sa_bustag,
270 			 sc->sc_physadr[TCX_REG_THC].oa_space,
271 			 sc->sc_physadr[TCX_REG_THC].oa_base,
272 			 sizeof (struct tcx_thc),
273 			 BUS_SPACE_MAP_LINEAR, &bh) != 0) {
274 		printf("tcxattach: cannot map thc registers\n");
275 		return;
276 	}
277 	sc->sc_thc = (volatile struct tcx_thc *)
278 		bus_space_vaddr(sa->sa_bustag, bh);
279 
280 	if (sbus_bus_map(sa->sa_bustag,
281 			 sc->sc_physadr[TCX_REG_CMAP].oa_space,
282 			 sc->sc_physadr[TCX_REG_CMAP].oa_base,
283 			 sizeof (struct bt_regs),
284 			 BUS_SPACE_MAP_LINEAR, &bh) != 0) {
285 		printf("tcxattach: cannot map bt registers\n");
286 		return;
287 	}
288 	sc->sc_bt = bt = (volatile struct bt_regs *)
289 		bus_space_vaddr(sa->sa_bustag, bh);
290 
291 #ifdef TCX_CG8
292 	if (!sc->sc_8bit) {
293 		if (sbus_bus_map(sa->sa_bustag,
294 			 sc->sc_physadr[TCX_REG_RDFB32].oa_space,
295 			 sc->sc_physadr[TCX_REG_RDFB32].oa_base,
296 			 TCX_SIZE_DFB32,
297 			 BUS_SPACE_MAP_LINEAR,
298 			 &bh) != 0) {
299 			printf("tcxattach: cannot map control planes\n");
300 			return;
301 		}
302 		sc->sc_cplane = (volatile ulong *)bh;
303 	}
304 #endif
305 
306 	isconsole = fb_is_console(node);
307 
308 	printf(", id %d, rev %d, sense %d",
309 		(sc->sc_thc->thc_config & THC_CFG_FBID) >> THC_CFG_FBID_SHIFT,
310 		(sc->sc_thc->thc_config & THC_CFG_REV) >> THC_CFG_REV_SHIFT,
311 		(sc->sc_thc->thc_config & THC_CFG_SENSE) >> THC_CFG_SENSE_SHIFT
312 	);
313 
314 	/* reset cursor & frame buffer controls */
315 	tcx_reset(sc);
316 
317 	/* Initialize the default color map. */
318 	bt_initcmap(&sc->sc_cmap, 256);
319 	tcx_loadcmap(sc, 0, 256);
320 
321 	/* enable video */
322 	sc->sc_thc->thc_hcmisc |= THC_MISC_VIDEN;
323 
324 	if (isconsole) {
325 		printf(" (console)\n");
326 	} else
327 		printf("\n");
328 
329 	sbus_establish(&sc->sc_sd, &sc->sc_dev);
330 	fb_attach(&sc->sc_fb, isconsole);
331 }
332 
333 #ifdef TCX_CG8
334 /*
335  * keep track of the number of opens, so we can switch to 24-bit mode
336  * when the device is first opened, and return to 8-bit mode on the
337  * last close.  (stolen from cgfourteen driver...)  There can only be
338  * one TCX per system, so we only need one flag.
339  */
340 static int tcx_opens = 0;
341 #endif
342 
343 int
344 tcxopen(dev, flags, mode, l)
345 	dev_t dev;
346 	int flags, mode;
347 	struct lwp *l;
348 {
349 	int unit = minor(dev);
350 #ifdef TCX_CG8
351 	struct tcx_softc *sc;
352 	int i, s, oldopens;
353 	volatile ulong *cptr;
354 	struct fbdevice *fb;
355 #endif
356 
357 	if (unit >= tcx_cd.cd_ndevs || tcx_cd.cd_devs[unit] == NULL)
358 		return (ENXIO);
359 #ifdef TCX_CG8
360 	sc = tcx_cd.cd_devs[unit];
361 	if (!sc->sc_8bit) {
362 		s = splhigh();
363 		oldopens = tcx_opens++;
364 		splx(s);
365 		if (oldopens == 0) {
366 			/*
367 			 * rewrite the control planes to select 24-bit mode
368 			 * and clear the screen
369 			 */
370 			fb = &sc->sc_fb;
371 			i = fb->fb_type.fb_height * fb->fb_type.fb_width;
372 			cptr = sc->sc_cplane;
373 			while (--i >= 0)
374 				*cptr++ = TCX_CTL_24_LEVEL;
375 		}
376 	}
377 #endif
378 	return (0);
379 }
380 
381 int
382 tcxclose(dev, flags, mode, l)
383 	dev_t dev;
384 	int flags, mode;
385 	struct lwp *l;
386 {
387 	struct tcx_softc *sc = tcx_cd.cd_devs[minor(dev)];
388 #ifdef TCX_CG8
389 	int i, s, opens;
390 	volatile ulong *cptr;
391 	struct fbdevice *fb;
392 #endif
393 
394 	tcx_reset(sc);
395 #ifdef TCX_CG8
396 	if (!sc->sc_8bit) {
397 		s = splhigh();
398 		opens = --tcx_opens;
399 		if (tcx_opens <= 0)
400 			opens = tcx_opens = 0;
401 		splx(s);
402 		if (opens == 0) {
403 			/*
404 			 * rewrite the control planes to select 8-bit mode,
405 			 * preserving the contents of the screen.
406 			 * (or we could just bzero the whole thing...)
407 			 */
408 			fb = &sc->sc_fb;
409 			i = fb->fb_type.fb_height * fb->fb_type.fb_width;
410 			cptr = sc->sc_cplane;
411 			while (--i >= 0)
412 				*cptr++ &= TCX_CTL_PIXELMASK;
413 		}
414 	}
415 #endif
416 	return (0);
417 }
418 
419 int
420 tcxioctl(dev, cmd, data, flags, l)
421 	dev_t dev;
422 	u_long cmd;
423 	void *data;
424 	int flags;
425 	struct lwp *l;
426 {
427 	struct tcx_softc *sc = tcx_cd.cd_devs[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 		return (bt_getcmap(p, &sc->sc_cmap, 256, 1));
453 
454 	case FBIOPUTCMAP:
455 		/* copy to software map */
456 #ifdef TCX_CG8
457 		if (!sc->sc_8bit) {
458 			/*
459 			 * cg8 has extra bits in high-order byte of the index
460 			 * that bt_putcmap doesn't recognize
461 			 */
462 			p->index &= 0xffffff;
463 		}
464 #endif
465 		error = bt_putcmap(p, &sc->sc_cmap, 256, 1);
466 		if (error)
467 			return (error);
468 		/* now blast them into the chip */
469 		/* XXX should use retrace interrupt */
470 		tcx_loadcmap(sc, p->index, p->count);
471 #undef p
472 		break;
473 
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 			sc->sc_blanked = 1;
483 			sc->sc_thc->thc_hcmisc &= ~THC_MISC_VIDEN;
484 			/* Put monitor in `power-saving mode' */
485 			sc->sc_thc->thc_hcmisc |= THC_MISC_VSYNC_DISABLE;
486 			sc->sc_thc->thc_hcmisc |= THC_MISC_HSYNC_DISABLE;
487 		}
488 		break;
489 
490 	default:
491 #ifdef DEBUG
492 		log(LOG_NOTICE, "tcxioctl(0x%lx) (%s[%d])\n", cmd,
493 		    l->l_proc->p_comm, l->l_proc->p_pid);
494 #endif
495 		return (ENOTTY);
496 	}
497 	return (0);
498 }
499 
500 /*
501  * Clean up hardware state (e.g., after bootup or after X crashes).
502  */
503 static void
504 tcx_reset(sc)
505 	struct tcx_softc *sc;
506 {
507 	volatile struct bt_regs *bt;
508 
509 	/* Enable cursor in Brooktree DAC. */
510 	bt = sc->sc_bt;
511 	bt->bt_addr = 0x06 << 24;
512 	bt->bt_ctrl |= 0x03 << 24;
513 }
514 
515 /*
516  * Load a subset of the current (new) colormap into the color DAC.
517  */
518 static void
519 tcx_loadcmap(sc, start, ncolors)
520 	struct tcx_softc *sc;
521 	int start, ncolors;
522 {
523 	volatile struct bt_regs *bt;
524 	u_int *ip, i;
525 	int count;
526 
527 	ip = &sc->sc_cmap.cm_chip[BT_D4M3(start)];	/* start/4 * 3 */
528 	count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3;
529 	bt = sc->sc_bt;
530 	bt->bt_addr = BT_D4M4(start) << 24;
531 	while (--count >= 0) {
532 		i = *ip++;
533 		/* hardware that makes one want to pound boards with hammers */
534 		bt->bt_cmap = i;
535 		bt->bt_cmap = i << 8;
536 		bt->bt_cmap = i << 16;
537 		bt->bt_cmap = i << 24;
538 	}
539 }
540 
541 static void
542 tcx_unblank(dev)
543 	struct device *dev;
544 {
545 	struct tcx_softc *sc = (struct tcx_softc *)dev;
546 
547 	if (sc->sc_blanked) {
548 		sc->sc_blanked = 0;
549 		sc->sc_thc->thc_hcmisc &= ~THC_MISC_VSYNC_DISABLE;
550 		sc->sc_thc->thc_hcmisc &= ~THC_MISC_HSYNC_DISABLE;
551 		sc->sc_thc->thc_hcmisc |= THC_MISC_VIDEN;
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, off, prot)
589 	dev_t dev;
590 	off_t off;
591 	int prot;
592 {
593 	struct tcx_softc *sc = tcx_cd.cd_devs[minor(dev)];
594 	struct openprom_addr *rr = sc->sc_physadr;
595 	struct mmo *mo, *mo_end;
596 	u_int u, sz;
597 	static struct mmo mmo[] = {
598 		{ TCX_USER_RAM, 0, TCX_REG_DFB8 },
599 		{ TCX_USER_RAM24, 0, TCX_REG_DFB24 },
600 		{ TCX_USER_RAM_COMPAT, 0, TCX_REG_DFB8 },
601 
602 		{ TCX_USER_STIP, 1, TCX_REG_STIP },
603 		{ TCX_USER_BLIT, 1, TCX_REG_BLIT },
604 		{ TCX_USER_RDFB32, 0, TCX_REG_RDFB32 },
605 		{ TCX_USER_RSTIP, 1, TCX_REG_RSTIP },
606 		{ TCX_USER_RBLIT, 1, TCX_REG_RBLIT },
607 		{ TCX_USER_TEC, 1, TCX_REG_TEC },
608 		{ TCX_USER_BTREGS, 8192 /* XXX */, TCX_REG_CMAP },
609 		{ TCX_USER_THC, sizeof(struct tcx_thc), TCX_REG_THC },
610 		{ TCX_USER_DHC, 1, TCX_REG_DHC },
611 		{ TCX_USER_ALT, 1, TCX_REG_ALT },
612 		{ TCX_USER_ROM, 65536, TCX_REG_ROM },
613 	};
614 #define NMMO (sizeof mmo / sizeof *mmo)
615 #ifdef TCX_CG8
616 	/*
617 	 * alternate mapping for CG8 emulation:
618 	 * map part of the 8-bit-deep framebuffer into the cg8 overlay
619 	 * space, just so there's something there, and map the 32-bit-deep
620 	 * framebuffer where cg8 users expect to find it.
621 	 */
622 	static struct mmo mmo_cg8[] = {
623 		{ TCX_USER_RAM, TCX_CG8OVERLAY, TCX_REG_DFB8 },
624 		{ TCX_CG8OVERLAY, TCX_SIZE_DFB32, TCX_REG_DFB24 },
625 		{ TCX_USER_RAM_COMPAT, TCX_SIZE_DFB32, TCX_REG_DFB24 }
626 	};
627 #define NMMO_CG8 (sizeof mmo_cg8 / sizeof *mmo_cg8)
628 #endif
629 
630 	if (off & PGOFSET)
631 		panic("tcxmmap");
632 
633 	/*
634 	 * Entries with size 0 map video RAM (i.e., the size in fb data).
635 	 * Entries that map 32-bit deep regions are adjusted for their
636 	 * depth (fb_size gives the size of the 8-bit-deep region).
637 	 *
638 	 * Since we work in pages, the fact that the map offset table's
639 	 * sizes are sometimes bizarre (e.g., 1) is effectively ignored:
640 	 * one byte is as good as one page.
641 	 */
642 #ifdef TCX_CG8
643 	if (sc->sc_8bit) {
644 		mo = mmo;
645 		mo_end = &mmo[NMMO];
646 	} else {
647 		mo = mmo_cg8;
648 		mo_end = &mmo_cg8[NMMO_CG8];
649 	}
650 #else
651 	mo = mmo;
652 	mo_end = &mmo[NMMO];
653 #endif
654 	for (; mo < mo_end; mo++) {
655 		if ((u_int)off < mo->mo_uaddr)
656 			continue;
657 		u = off - mo->mo_uaddr;
658 		sz = mo->mo_size;
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 (u < sz) {
677 			return (bus_space_mmap(sc->sc_bustag,
678 				BUS_ADDR(rr[mo->mo_bank].oa_space,
679 					 rr[mo->mo_bank].oa_base),
680 				u,
681 				prot,
682 				BUS_SPACE_MAP_LINEAR));
683 		}
684 	}
685 	return (-1);
686 }
687