Home | History | Annotate | Line # | Download | only in sbus
tcx.c revision 1.5
      1 /*	$NetBSD: tcx.c,v 1.5 2002/03/11 16:00:58 pk 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  *  3. All advertising materials mentioning features or use of this software
     19  *     must display the following acknowledgement:
     20  *         This product includes software developed by the NetBSD
     21  *         Foundation, Inc. and its contributors.
     22  *  4. Neither the name of The NetBSD Foundation nor the names of its
     23  *     contributors may be used to endorse or promote products derived
     24  *     from this software without specific prior written permission.
     25  *
     26  *  THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  *  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  *  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  *  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  *  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  *  POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * color display (TCX) driver.
     41  *
     42  * Does not handle interrupts, even though they can occur.
     43  *
     44  * XXX should defer colormap updates to vertical retrace interrupts
     45  */
     46 
     47 #include <sys/cdefs.h>
     48 __KERNEL_RCSID(0, "$NetBSD: tcx.c,v 1.5 2002/03/11 16:00:58 pk Exp $");
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/buf.h>
     53 #include <sys/device.h>
     54 #include <sys/ioctl.h>
     55 #include <sys/malloc.h>
     56 #include <sys/mman.h>
     57 #include <sys/tty.h>
     58 #include <sys/conf.h>
     59 
     60 #ifdef DEBUG
     61 #include <sys/proc.h>
     62 #include <sys/syslog.h>
     63 #endif
     64 
     65 #include <machine/bus.h>
     66 #include <machine/autoconf.h>
     67 
     68 #include <dev/sun/fbio.h>
     69 #include <dev/sun/fbvar.h>
     70 #include <dev/sun/btreg.h>
     71 #include <dev/sun/btvar.h>
     72 
     73 #include <dev/sbus/sbusvar.h>
     74 #include <dev/sbus/tcxreg.h>
     75 
     76 #include <machine/conf.h>
     77 
     78 /* per-display variables */
     79 struct tcx_softc {
     80 	struct device	sc_dev;		/* base device */
     81 	struct sbusdev	sc_sd;		/* sbus device */
     82 	struct fbdevice	sc_fb;		/* frame buffer device */
     83 	bus_space_tag_t	sc_bustag;
     84 	struct sbus_reg	sc_physadr[TCX_NREG];	/* phys addr of h/w */
     85 
     86 	volatile struct bt_regs *sc_bt;	/* Brooktree registers */
     87 	volatile struct tcx_thc *sc_thc;/* THC registers */
     88 	short	sc_blanked;		/* true if blanked */
     89 	union	bt_cmap sc_cmap;	/* Brooktree color map */
     90 };
     91 
     92 /* autoconfiguration driver */
     93 static void	tcxattach __P((struct device *, struct device *, void *));
     94 static int	tcxmatch __P((struct device *, struct cfdata *, void *));
     95 static void	tcx_unblank __P((struct device *));
     96 
     97 /* cdevsw prototypes */
     98 cdev_decl(tcx);
     99 
    100 struct cfattach tcx_ca = {
    101 	sizeof(struct tcx_softc), tcxmatch, tcxattach
    102 };
    103 
    104 extern struct cfdriver tcx_cd;
    105 
    106 /* frame buffer generic driver */
    107 static struct fbdriver tcx_fbdriver = {
    108 	tcx_unblank, tcxopen, tcxclose, tcxioctl, tcxpoll, tcxmmap
    109 };
    110 
    111 static void tcx_reset __P((struct tcx_softc *));
    112 static void tcx_loadcmap __P((struct tcx_softc *, int, int));
    113 
    114 #define OBPNAME	"SUNW,tcx"
    115 /*
    116  * Match a tcx.
    117  */
    118 int
    119 tcxmatch(parent, cf, aux)
    120 	struct device *parent;
    121 	struct cfdata *cf;
    122 	void *aux;
    123 {
    124 	struct sbus_attach_args *sa = aux;
    125 
    126 	return (strcmp(sa->sa_name, OBPNAME) == 0);
    127 }
    128 
    129 /*
    130  * Attach a display.
    131  */
    132 void
    133 tcxattach(parent, self, args)
    134 	struct device *parent, *self;
    135 	void *args;
    136 {
    137 	struct tcx_softc *sc = (struct tcx_softc *)self;
    138 	struct sbus_attach_args *sa = args;
    139 	int node, ramsize;
    140 	volatile struct bt_regs *bt;
    141 	struct fbdevice *fb = &sc->sc_fb;
    142 	bus_space_handle_t bh;
    143 	int isconsole;
    144 
    145 	sc->sc_bustag = sa->sa_bustag;
    146 	node = sa->sa_node;
    147 
    148 	fb->fb_driver = &tcx_fbdriver;
    149 	fb->fb_device = &sc->sc_dev;
    150 	/* Mask out invalid flags from the user. */
    151 	fb->fb_flags = sc->sc_dev.dv_cfdata->cf_flags & FB_USERMASK;
    152 	fb->fb_type.fb_depth = node_has_property(node, "tcx-24-bit")
    153 		? 24
    154 		: (node_has_property(node, "tcx-8-bit")
    155 			? 8
    156 			: 8);
    157 
    158 	fb_setsize_obp(fb, fb->fb_type.fb_depth, 1152, 900, node);
    159 
    160 	ramsize = fb->fb_type.fb_height * fb->fb_linebytes;
    161 	fb->fb_type.fb_cmsize = 256;
    162 	fb->fb_type.fb_size = ramsize;
    163 	printf(": %s, %d x %d", OBPNAME,
    164 		fb->fb_type.fb_width,
    165 		fb->fb_type.fb_height);
    166 
    167 	/*
    168 	 * XXX - should be set to FBTYPE_TCX.
    169 	 * XXX For CG3 emulation to work in current (96/6) X11 servers,
    170 	 * XXX `fbtype' must point to an "unregocnised" entry.
    171 	 */
    172 	fb->fb_type.fb_type = FBTYPE_RESERVED3;
    173 
    174 
    175 	if (sa->sa_nreg != TCX_NREG) {
    176 		printf("%s: only %d register sets\n",
    177 			self->dv_xname, sa->sa_nreg);
    178 		return;
    179 	}
    180 	bcopy(sa->sa_reg, sc->sc_physadr,
    181 	      sa->sa_nreg * sizeof(struct sbus_reg));
    182 
    183 	/* XXX - fix THC and TEC offsets */
    184 	sc->sc_physadr[TCX_REG_TEC].sbr_offset += 0x1000;
    185 	sc->sc_physadr[TCX_REG_THC].sbr_offset += 0x1000;
    186 
    187 	/* Map the register banks we care about */
    188 	if (sbus_bus_map(sa->sa_bustag,
    189 			 sc->sc_physadr[TCX_REG_THC].sbr_slot,
    190 			 sc->sc_physadr[TCX_REG_THC].sbr_offset,
    191 			 sizeof (struct tcx_thc),
    192 			 BUS_SPACE_MAP_LINEAR, &bh) != 0) {
    193 		printf("tcxattach: cannot map thc registers\n");
    194 		return;
    195 	}
    196 	sc->sc_thc = (volatile struct tcx_thc *)bh;
    197 
    198 	if (sbus_bus_map(sa->sa_bustag,
    199 			 sc->sc_physadr[TCX_REG_CMAP].sbr_slot,
    200 			 sc->sc_physadr[TCX_REG_CMAP].sbr_offset,
    201 			 sizeof (struct bt_regs),
    202 			 BUS_SPACE_MAP_LINEAR, &bh) != 0) {
    203 		printf("tcxattach: cannot map bt registers\n");
    204 		return;
    205 	}
    206 	sc->sc_bt = bt = (volatile struct bt_regs *)bh;
    207 
    208 	isconsole = fb_is_console(node);
    209 
    210 	printf(", id %d, rev %d, sense %d",
    211 		(sc->sc_thc->thc_config & THC_CFG_FBID) >> THC_CFG_FBID_SHIFT,
    212 		(sc->sc_thc->thc_config & THC_CFG_REV) >> THC_CFG_REV_SHIFT,
    213 		(sc->sc_thc->thc_config & THC_CFG_SENSE) >> THC_CFG_SENSE_SHIFT
    214 	);
    215 
    216 	/* reset cursor & frame buffer controls */
    217 	tcx_reset(sc);
    218 
    219 	/* Initialize the default color map. */
    220 	bt_initcmap(&sc->sc_cmap, 256);
    221 	tcx_loadcmap(sc, 0, 256);
    222 
    223 	/* enable video */
    224 	sc->sc_thc->thc_hcmisc |= THC_MISC_VIDEN;
    225 
    226 	if (isconsole) {
    227 		printf(" (console)\n");
    228 	} else
    229 		printf("\n");
    230 
    231 	sbus_establish(&sc->sc_sd, &sc->sc_dev);
    232 	fb_attach(&sc->sc_fb, isconsole);
    233 }
    234 
    235 int
    236 tcxopen(dev, flags, mode, p)
    237 	dev_t dev;
    238 	int flags, mode;
    239 	struct proc *p;
    240 {
    241 	int unit = minor(dev);
    242 
    243 	if (unit >= tcx_cd.cd_ndevs || tcx_cd.cd_devs[unit] == NULL)
    244 		return (ENXIO);
    245 	return (0);
    246 }
    247 
    248 int
    249 tcxclose(dev, flags, mode, p)
    250 	dev_t dev;
    251 	int flags, mode;
    252 	struct proc *p;
    253 {
    254 	struct tcx_softc *sc = tcx_cd.cd_devs[minor(dev)];
    255 
    256 	tcx_reset(sc);
    257 	return (0);
    258 }
    259 
    260 int
    261 tcxioctl(dev, cmd, data, flags, p)
    262 	dev_t dev;
    263 	u_long cmd;
    264 	caddr_t data;
    265 	int flags;
    266 	struct proc *p;
    267 {
    268 	struct tcx_softc *sc = tcx_cd.cd_devs[minor(dev)];
    269 	int error;
    270 
    271 	switch (cmd) {
    272 
    273 	case FBIOGTYPE:
    274 		*(struct fbtype *)data = sc->sc_fb.fb_type;
    275 		break;
    276 
    277 	case FBIOGATTR:
    278 #define fba ((struct fbgattr *)data)
    279 		fba->real_type = sc->sc_fb.fb_type.fb_type;
    280 		fba->owner = 0;		/* XXX ??? */
    281 		fba->fbtype = sc->sc_fb.fb_type;
    282 		fba->sattr.flags = 0;
    283 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
    284 		fba->sattr.dev_specific[0] = -1;
    285 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
    286 		fba->emu_types[1] = FBTYPE_SUN3COLOR;
    287 		fba->emu_types[2] = -1;
    288 #undef fba
    289 		break;
    290 
    291 	case FBIOGETCMAP:
    292 #define	p ((struct fbcmap *)data)
    293 		return (bt_getcmap(p, &sc->sc_cmap, 256, 1));
    294 
    295 	case FBIOPUTCMAP:
    296 		/* copy to software map */
    297 		error = bt_putcmap(p, &sc->sc_cmap, 256, 1);
    298 		if (error)
    299 			return (error);
    300 		/* now blast them into the chip */
    301 		/* XXX should use retrace interrupt */
    302 		tcx_loadcmap(sc, p->index, p->count);
    303 #undef p
    304 		break;
    305 
    306 	case FBIOGVIDEO:
    307 		*(int *)data = sc->sc_blanked;
    308 		break;
    309 
    310 	case FBIOSVIDEO:
    311 		if (*(int *)data)
    312 			tcx_unblank(&sc->sc_dev);
    313 		else if (!sc->sc_blanked) {
    314 			sc->sc_blanked = 1;
    315 			sc->sc_thc->thc_hcmisc &= ~THC_MISC_VIDEN;
    316 			/* Put monitor in `power-saving mode' */
    317 			sc->sc_thc->thc_hcmisc |= THC_MISC_VSYNC_DISABLE;
    318 			sc->sc_thc->thc_hcmisc |= THC_MISC_HSYNC_DISABLE;
    319 		}
    320 		break;
    321 
    322 	default:
    323 #ifdef DEBUG
    324 		log(LOG_NOTICE, "tcxioctl(0x%lx) (%s[%d])\n", cmd,
    325 		    p->p_comm, p->p_pid);
    326 #endif
    327 		return (ENOTTY);
    328 	}
    329 	return (0);
    330 }
    331 
    332 int
    333 tcxpoll(dev, events, p)
    334 	dev_t dev;
    335 	int events;
    336 	struct proc *p;
    337 {
    338 
    339 	return (seltrue(dev, events, p));
    340 }
    341 
    342 /*
    343  * Clean up hardware state (e.g., after bootup or after X crashes).
    344  */
    345 static void
    346 tcx_reset(sc)
    347 	struct tcx_softc *sc;
    348 {
    349 	volatile struct bt_regs *bt;
    350 
    351 	/* Enable cursor in Brooktree DAC. */
    352 	bt = sc->sc_bt;
    353 	bt->bt_addr = 0x06 << 24;
    354 	bt->bt_ctrl |= 0x03 << 24;
    355 }
    356 
    357 /*
    358  * Load a subset of the current (new) colormap into the color DAC.
    359  */
    360 static void
    361 tcx_loadcmap(sc, start, ncolors)
    362 	struct tcx_softc *sc;
    363 	int start, ncolors;
    364 {
    365 	volatile struct bt_regs *bt;
    366 	u_int *ip, i;
    367 	int count;
    368 
    369 	ip = &sc->sc_cmap.cm_chip[BT_D4M3(start)];	/* start/4 * 3 */
    370 	count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3;
    371 	bt = sc->sc_bt;
    372 	bt->bt_addr = BT_D4M4(start) << 24;
    373 	while (--count >= 0) {
    374 		i = *ip++;
    375 		/* hardware that makes one want to pound boards with hammers */
    376 		bt->bt_cmap = i;
    377 		bt->bt_cmap = i << 8;
    378 		bt->bt_cmap = i << 16;
    379 		bt->bt_cmap = i << 24;
    380 	}
    381 }
    382 
    383 static void
    384 tcx_unblank(dev)
    385 	struct device *dev;
    386 {
    387 	struct tcx_softc *sc = (struct tcx_softc *)dev;
    388 
    389 	if (sc->sc_blanked) {
    390 		sc->sc_blanked = 0;
    391 		sc->sc_thc->thc_hcmisc &= ~THC_MISC_VSYNC_DISABLE;
    392 		sc->sc_thc->thc_hcmisc &= ~THC_MISC_HSYNC_DISABLE;
    393 		sc->sc_thc->thc_hcmisc |= THC_MISC_VIDEN;
    394 	}
    395 }
    396 
    397 /*
    398  * Base addresses at which users can mmap() the various pieces of a tcx.
    399  */
    400 #define	TCX_USER_RAM	0x00000000
    401 #define	TCX_USER_RAM24	0x01000000
    402 #define	TCX_USER_RAM_COMPAT	0x04000000	/* cg3 emulation */
    403 #define	TCX_USER_STIP	0x10000000
    404 #define	TCX_USER_BLIT	0x20000000
    405 #define	TCX_USER_RDFB32	0x28000000
    406 #define	TCX_USER_RSTIP	0x30000000
    407 #define	TCX_USER_RBLIT	0x38000000
    408 #define	TCX_USER_TEC	0x70001000
    409 #define	TCX_USER_BTREGS	0x70002000
    410 #define	TCX_USER_THC	0x70004000
    411 #define	TCX_USER_DHC	0x70008000
    412 #define	TCX_USER_ALT	0x7000a000
    413 #define	TCX_USER_UART	0x7000c000
    414 #define	TCX_USER_VRT	0x7000e000
    415 #define	TCX_USER_ROM	0x70010000
    416 
    417 struct mmo {
    418 	u_int	mo_uaddr;	/* user (virtual) address */
    419 	u_int	mo_size;	/* size, or 0 for video ram size */
    420 	u_int	mo_bank;	/* register bank number */
    421 };
    422 
    423 /*
    424  * Return the address that would map the given device at the given
    425  * offset, allowing for the given protection, or return -1 for error.
    426  *
    427  * XXX	needs testing against `demanding' applications (e.g., aviator)
    428  */
    429 paddr_t
    430 tcxmmap(dev, off, prot)
    431 	dev_t dev;
    432 	off_t off;
    433 	int prot;
    434 {
    435 	struct tcx_softc *sc = tcx_cd.cd_devs[minor(dev)];
    436 	struct sbus_reg *rr = sc->sc_physadr;
    437 	struct mmo *mo;
    438 	u_int u, sz;
    439 	static struct mmo mmo[] = {
    440 		{ TCX_USER_RAM, 0, TCX_REG_DFB8 },
    441 		{ TCX_USER_RAM24, 0, TCX_REG_DFB24 },
    442 		{ TCX_USER_RAM_COMPAT, 0, TCX_REG_DFB8 },
    443 
    444 		{ TCX_USER_STIP, 1, TCX_REG_STIP },
    445 		{ TCX_USER_BLIT, 1, TCX_REG_BLIT },
    446 		{ TCX_USER_RDFB32, 1, TCX_REG_RDFB32 },
    447 		{ TCX_USER_RSTIP, 1, TCX_REG_RSTIP },
    448 		{ TCX_USER_RBLIT, 1, TCX_REG_RBLIT },
    449 		{ TCX_USER_TEC, 1, TCX_REG_TEC },
    450 		{ TCX_USER_BTREGS, 8192 /* XXX */, TCX_REG_CMAP },
    451 		{ TCX_USER_THC, sizeof(struct tcx_thc), TCX_REG_THC },
    452 		{ TCX_USER_DHC, 1, TCX_REG_DHC },
    453 		{ TCX_USER_ALT, 1, TCX_REG_ALT },
    454 		{ TCX_USER_ROM, 65536, TCX_REG_ROM },
    455 	};
    456 #define NMMO (sizeof mmo / sizeof *mmo)
    457 
    458 	if (off & PGOFSET)
    459 		panic("tcxmmap");
    460 
    461 	/*
    462 	 * Entries with size 0 map video RAM (i.e., the size in fb data).
    463 	 *
    464 	 * Since we work in pages, the fact that the map offset table's
    465 	 * sizes are sometimes bizarre (e.g., 1) is effectively ignored:
    466 	 * one byte is as good as one page.
    467 	 */
    468 	for (mo = mmo; mo < &mmo[NMMO]; mo++) {
    469 		if ((u_int)off < mo->mo_uaddr)
    470 			continue;
    471 		u = off - mo->mo_uaddr;
    472 		sz = mo->mo_size ? mo->mo_size : sc->sc_fb.fb_type.fb_size;
    473 		if (u < sz) {
    474 			return (bus_space_mmap(sc->sc_bustag,
    475 				BUS_ADDR(rr[mo->mo_bank].sbr_slot,
    476 					 rr[mo->mo_bank].sbr_offset),
    477 				u,
    478 				prot,
    479 				BUS_SPACE_MAP_LINEAR));
    480 		}
    481 	}
    482 	return (-1);
    483 }
    484