Home | History | Annotate | Line # | Download | only in sun
cgthree.c revision 1.30
      1 /*	$NetBSD: cgthree.c,v 1.30 2014/03/16 05:20:29 dholland Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 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 (cgthree) 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: cgthree.c,v 1.30 2014/03/16 05:20:29 dholland Exp $");
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/buf.h>
     46 #include <sys/device.h>
     47 #include <sys/ioctl.h>
     48 #include <sys/malloc.h>
     49 #include <sys/mman.h>
     50 #include <sys/tty.h>
     51 #include <sys/conf.h>
     52 
     53 #include <sys/bus.h>
     54 #include <machine/autoconf.h>
     55 
     56 #include <dev/sun/fbio.h>
     57 #include <dev/sun/fbvar.h>
     58 
     59 #include <dev/sun/btreg.h>
     60 #include <dev/sun/btvar.h>
     61 #include <dev/sun/cgthreereg.h>
     62 #include <dev/sun/cgthreevar.h>
     63 
     64 #if NWSDISPLAY > 0
     65 #include <dev/wscons/wsconsio.h>
     66 #include <dev/wsfont/wsfont.h>
     67 #include <dev/rasops/rasops.h>
     68 
     69 #include "opt_wsemul.h"
     70 #endif
     71 
     72 #include "ioconf.h"
     73 
     74 static void	cgthreeunblank(device_t);
     75 static void	cgthreeloadcmap(struct cgthree_softc *, int, int);
     76 static void	cgthree_set_video(struct cgthree_softc *, int);
     77 static int	cgthree_get_video(struct cgthree_softc *);
     78 
     79 dev_type_open(cgthreeopen);
     80 dev_type_ioctl(cgthreeioctl);
     81 dev_type_mmap(cgthreemmap);
     82 
     83 const struct cdevsw cgthree_cdevsw = {
     84 	.d_open = cgthreeopen,
     85 	.d_close = nullclose,
     86 	.d_read = noread,
     87 	.d_write = nowrite,
     88 	.d_ioctl = cgthreeioctl,
     89 	.d_stop = nostop,
     90 	.d_tty = notty,
     91 	.d_poll = nopoll,
     92 	.d_mmap = cgthreemmap,
     93 	.d_kqfilter = nokqfilter,
     94 	.d_flag = D_OTHER
     95 };
     96 
     97 /* frame buffer generic driver */
     98 static struct fbdriver cgthreefbdriver = {
     99 	cgthreeunblank, cgthreeopen, nullclose, cgthreeioctl, nopoll,
    100 	cgthreemmap, nokqfilter
    101 };
    102 
    103 /* Video control parameters */
    104 struct cg3_videoctrl {
    105 	unsigned char	sense;		/* Monitor sense value */
    106 	unsigned char	vctrl[12];
    107 } cg3_videoctrl[] = {
    108 /* Missing entries: sense 0x10, 0x30, 0x50 */
    109 	{ 0x40, /* this happens to be my 19'' 1152x900 gray-scale monitor */
    110 	   {0xbb, 0x2b, 0x3, 0xb, 0xb3, 0x3, 0xaf, 0x2b, 0x2, 0xa, 0xff, 0x1}
    111 	},
    112 	{ 0x00, /* default? must be last */
    113 	   {0xbb, 0x2b, 0x3, 0xb, 0xb3, 0x3, 0xaf, 0x2b, 0x2, 0xa, 0xff, 0x1}
    114 	}
    115 };
    116 
    117 #if NWSDISPLAY > 0
    118 #ifdef RASTERCONSOLE
    119 #error RASTERCONSOLE and wsdisplay are mutually exclusive
    120 #endif
    121 
    122 static void cg3_setup_palette(struct cgthree_softc *);
    123 
    124 struct wsscreen_descr cgthree_defaultscreen = {
    125 	"std",
    126 	0, 0,	/* will be filled in -- XXX shouldn't, it's global */
    127 	NULL,		/* textops */
    128 	8, 16,	/* font width/height */
    129 	WSSCREEN_WSCOLORS,	/* capabilities */
    130 	NULL	/* modecookie */
    131 };
    132 
    133 static int 	cgthree_ioctl(void *, void *, u_long, void *, int, struct lwp *);
    134 static paddr_t	cgthree_mmap(void *, void *, off_t, int);
    135 static void	cgthree_init_screen(void *, struct vcons_screen *, int, long *);
    136 
    137 int	cgthree_putcmap(struct cgthree_softc *, struct wsdisplay_cmap *);
    138 int	cgthree_getcmap(struct cgthree_softc *, struct wsdisplay_cmap *);
    139 
    140 struct wsdisplay_accessops cgthree_accessops = {
    141 	cgthree_ioctl,
    142 	cgthree_mmap,
    143 	NULL,	/* alloc_screen */
    144 	NULL,	/* free_screen */
    145 	NULL,	/* show_screen */
    146 	NULL, 	/* load_font */
    147 	NULL,	/* pollc */
    148 	NULL	/* scroll */
    149 };
    150 
    151 const struct wsscreen_descr *_cgthree_scrlist[] = {
    152 	&cgthree_defaultscreen
    153 };
    154 
    155 struct wsscreen_list cgthree_screenlist = {
    156 	sizeof(_cgthree_scrlist) / sizeof(struct wsscreen_descr *),
    157 	_cgthree_scrlist
    158 };
    159 
    160 
    161 extern const u_char rasops_cmap[768];
    162 
    163 static struct vcons_screen cg3_console_screen;
    164 #endif /* NWSDISPLAY > 0 */
    165 
    166 void
    167 cgthreeattach(struct cgthree_softc *sc, const char *name, int isconsole)
    168 {
    169 	int i;
    170 	struct fbdevice *fb = &sc->sc_fb;
    171 #if NWSDISPLAY > 0
    172 	struct wsemuldisplaydev_attach_args aa;
    173 	struct rasops_info *ri = &cg3_console_screen.scr_ri;
    174 	unsigned long defattr;
    175 #endif
    176 	volatile struct fbcontrol *fbc = sc->sc_fbc;
    177 	volatile struct bt_regs *bt = &fbc->fbc_dac;
    178 
    179 	fb->fb_driver = &cgthreefbdriver;
    180 	fb->fb_type.fb_cmsize = 256;
    181 	fb->fb_type.fb_size = fb->fb_type.fb_height * fb->fb_linebytes;
    182 	printf(": %s, %d x %d", name,
    183 		fb->fb_type.fb_width, fb->fb_type.fb_height);
    184 
    185 	/* Transfer video magic to board, if it's not running */
    186 	if ((fbc->fbc_ctrl & FBC_TIMING) == 0) {
    187 		int sense = (fbc->fbc_status & FBS_MSENSE);
    188 		/* Search table for video timings fitting this monitor */
    189 		for (i = 0; i < sizeof(cg3_videoctrl)/sizeof(cg3_videoctrl[0]);
    190 		     i++) {
    191 			int j;
    192 			if (sense != cg3_videoctrl[i].sense)
    193 				continue;
    194 
    195 			printf(" setting video ctrl");
    196 			for (j = 0; j < 12; j++)
    197 				fbc->fbc_vcontrol[j] =
    198 					cg3_videoctrl[i].vctrl[j];
    199 			fbc->fbc_ctrl |= FBC_TIMING;
    200 			break;
    201 		}
    202 	}
    203 
    204 	/* make sure we are not blanked */
    205 	cgthree_set_video(sc, 1);
    206 	BT_INIT(bt, 0);
    207 
    208 	if (isconsole) {
    209 		printf(" (console)\n");
    210 #ifdef RASTERCONSOLE
    211 		fbrcons_init(fb);
    212 #endif
    213 	} else
    214 		printf("\n");
    215 
    216 	fb_attach(fb, isconsole);
    217 
    218 #if NWSDISPLAY > 0
    219 	sc->sc_width = fb->fb_type.fb_width;
    220 	sc->sc_stride = fb->fb_type.fb_width;
    221 	sc->sc_height = fb->fb_type.fb_height;
    222 
    223 	/* setup rasops and so on for wsdisplay */
    224 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    225 
    226 	vcons_init(&sc->vd, sc, &cgthree_defaultscreen, &cgthree_accessops);
    227 	sc->vd.init_screen = cgthree_init_screen;
    228 
    229 	if(isconsole) {
    230 		/* we mess with cg3_console_screen only once */
    231 		vcons_init_screen(&sc->vd, &cg3_console_screen, 1,
    232 		    &defattr);
    233 		memset(sc->sc_fb.fb_pixels, (defattr >> 16) & 0xff,
    234 		    sc->sc_stride * sc->sc_height);
    235 		cg3_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    236 
    237 		cgthree_defaultscreen.textops = &ri->ri_ops;
    238 		cgthree_defaultscreen.capabilities = ri->ri_caps;
    239 		cgthree_defaultscreen.nrows = ri->ri_rows;
    240 		cgthree_defaultscreen.ncols = ri->ri_cols;
    241 		sc->vd.active = &cg3_console_screen;
    242 		wsdisplay_cnattach(&cgthree_defaultscreen, ri, 0, 0, defattr);
    243 		vcons_replay_msgbuf(&cg3_console_screen);
    244 	} else {
    245 		/*
    246 		 * we're not the console so we just clear the screen and don't
    247 		 * set up any sort of text display
    248 		 */
    249 	}
    250 
    251 	/* Initialize the default color map. */
    252 	cg3_setup_palette(sc);
    253 
    254 	aa.scrdata = &cgthree_screenlist;
    255 	aa.console = isconsole;
    256 	aa.accessops = &cgthree_accessops;
    257 	aa.accesscookie = &sc->vd;
    258 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
    259 #else
    260 	/* Initialize the default color map. */
    261 	bt_initcmap(&sc->sc_cmap, 256);
    262 	cgthreeloadcmap(sc, 0, 256);
    263 #endif
    264 
    265 }
    266 
    267 
    268 int
    269 cgthreeopen(dev_t dev, int flags, int mode, struct lwp *l)
    270 {
    271 	int unit = minor(dev);
    272 
    273 	if (device_lookup(&cgthree_cd, unit) == NULL)
    274 		return (ENXIO);
    275 	return (0);
    276 }
    277 
    278 int
    279 cgthreeioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
    280 {
    281 	struct cgthree_softc *sc = device_lookup_private(&cgthree_cd,
    282 							 minor(dev));
    283 	struct fbgattr *fba;
    284 	int error;
    285 
    286 	switch (cmd) {
    287 
    288 	case FBIOGTYPE:
    289 		*(struct fbtype *)data = sc->sc_fb.fb_type;
    290 		break;
    291 
    292 	case FBIOGATTR:
    293 		fba = (struct fbgattr *)data;
    294 		fba->real_type = sc->sc_fb.fb_type.fb_type;
    295 		fba->owner = 0;		/* XXX ??? */
    296 		fba->fbtype = sc->sc_fb.fb_type;
    297 		fba->sattr.flags = 0;
    298 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
    299 		fba->sattr.dev_specific[0] = -1;
    300 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
    301 		fba->emu_types[1] = -1;
    302 		break;
    303 
    304 	case FBIOGETCMAP:
    305 #define p ((struct fbcmap *)data)
    306 		return (bt_getcmap(p, &sc->sc_cmap, 256, 1));
    307 
    308 	case FBIOPUTCMAP:
    309 		/* copy to software map */
    310 		error = bt_putcmap(p, &sc->sc_cmap, 256, 1);
    311 		if (error)
    312 			return (error);
    313 		/* now blast them into the chip */
    314 		/* XXX should use retrace interrupt */
    315 		cgthreeloadcmap(sc, p->index, p->count);
    316 #undef p
    317 		break;
    318 
    319 	case FBIOGVIDEO:
    320 		*(int *)data = cgthree_get_video(sc);
    321 		break;
    322 
    323 	case FBIOSVIDEO:
    324 		cgthree_set_video(sc, *(int *)data);
    325 		break;
    326 
    327 	default:
    328 		return (ENOTTY);
    329 	}
    330 	return (0);
    331 }
    332 
    333 /*
    334  * Undo the effect of an FBIOSVIDEO that turns the video off.
    335  */
    336 static void
    337 cgthreeunblank(device_t self)
    338 {
    339 	struct cgthree_softc *sc = device_private(self);
    340 
    341 	cgthree_set_video(sc, 1);
    342 }
    343 
    344 static void
    345 cgthree_set_video(struct cgthree_softc *sc, int enable)
    346 {
    347 
    348 	if (enable)
    349 		sc->sc_fbc->fbc_ctrl |= FBC_VENAB;
    350 	else
    351 		sc->sc_fbc->fbc_ctrl &= ~FBC_VENAB;
    352 }
    353 
    354 static int
    355 cgthree_get_video(struct cgthree_softc *sc)
    356 {
    357 
    358 	return ((sc->sc_fbc->fbc_ctrl & FBC_VENAB) != 0);
    359 }
    360 
    361 /*
    362  * Load a subset of the current (new) colormap into the Brooktree DAC.
    363  */
    364 static void
    365 cgthreeloadcmap(struct cgthree_softc *sc, int start, int ncolors)
    366 {
    367 	volatile struct bt_regs *bt;
    368 	u_int *ip;
    369 	int count;
    370 
    371 	ip = &sc->sc_cmap.cm_chip[BT_D4M3(start)];	/* start/4 * 3 */
    372 	count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3;
    373 	bt = &sc->sc_fbc->fbc_dac;
    374 	bt->bt_addr = BT_D4M4(start);
    375 	while (--count >= 0)
    376 		bt->bt_cmap = *ip++;
    377 }
    378 
    379 /*
    380  * Return the address that would map the given device at the given
    381  * offset, allowing for the given protection, or return -1 for error.
    382  *
    383  * The cg3 is mapped starting at 256KB, for pseudo-compatibility with
    384  * the cg4 (which had an overlay plane in the first 128K and an enable
    385  * plane in the next 128K).  X11 uses only 256k+ region but tries to
    386  * map the whole thing, so we repeatedly map the first 256K to the
    387  * first page of the color screen.  If someone tries to use the overlay
    388  * and enable regions, they will get a surprise....
    389  *
    390  * As well, mapping at an offset of 0x04000000 causes the cg3 to be
    391  * mapped in flat mode without the cg4 emulation.
    392  */
    393 paddr_t
    394 cgthreemmap(dev_t dev, off_t off, int prot)
    395 {
    396 	struct cgthree_softc *sc = device_lookup_private(&cgthree_cd,
    397 							 minor(dev));
    398 
    399 #define START		(128*1024 + 128*1024)
    400 #define NOOVERLAY	(0x04000000)
    401 
    402 	if (off & PGOFSET)
    403 		panic("cgthreemmap");
    404 	if (off < 0)
    405 		return (-1);
    406 	if ((u_int)off >= NOOVERLAY)
    407 		off -= NOOVERLAY;
    408 	else if ((u_int)off >= START)
    409 		off -= START;
    410 	else
    411 		off = 0;
    412 
    413 	if (off >= sc->sc_fb.fb_type.fb_size)
    414 		return (-1);
    415 
    416 	return (bus_space_mmap(sc->sc_bustag,
    417 		sc->sc_paddr, CG3REG_MEM + off,
    418 		prot, BUS_SPACE_MAP_LINEAR));
    419 }
    420 
    421 #if NWSDISPLAY > 0
    422 
    423 static void
    424 cg3_setup_palette(struct cgthree_softc *sc)
    425 {
    426 	int i, j;
    427 
    428 	j = 0;
    429 	for (i = 0; i < 256; i++) {
    430 		sc->sc_cmap.cm_map[i][0] = rasops_cmap[j];
    431 		j++;
    432 		sc->sc_cmap.cm_map[i][1] = rasops_cmap[j];
    433 		j++;
    434 		sc->sc_cmap.cm_map[i][2] = rasops_cmap[j];
    435 		j++;
    436 	}
    437 	cgthreeloadcmap(sc, 0, 256);
    438 }
    439 
    440 int
    441 cgthree_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    442 	struct lwp *l)
    443 {
    444 	/* we'll probably need to add more stuff here */
    445 	struct vcons_data *vd = v;
    446 	struct cgthree_softc *sc = vd->cookie;
    447 	struct wsdisplay_fbinfo *wdf;
    448 	struct rasops_info *ri = &sc->sc_fb.fb_rinfo;
    449 	struct vcons_screen *ms = sc->vd.active;
    450 	switch (cmd) {
    451 		case WSDISPLAYIO_GTYPE:
    452 			*(u_int *)data = WSDISPLAY_TYPE_SUNTCX;
    453 			return 0;
    454 		case WSDISPLAYIO_GINFO:
    455 			wdf = (void *)data;
    456 			wdf->height = ri->ri_height;
    457 			wdf->width = ri->ri_width;
    458 			wdf->depth = ri->ri_depth;
    459 			wdf->cmsize = 256;
    460 			return 0;
    461 
    462 		case WSDISPLAYIO_GETCMAP:
    463 			return cgthree_getcmap(sc,
    464 			    (struct wsdisplay_cmap *)data);
    465 		case WSDISPLAYIO_PUTCMAP:
    466 			return cgthree_putcmap(sc,
    467 			    (struct wsdisplay_cmap *)data);
    468 
    469 		case WSDISPLAYIO_SMODE:
    470 			{
    471 				int new_mode = *(int*)data;
    472 				if (new_mode != sc->sc_mode)
    473 				{
    474 					sc->sc_mode = new_mode;
    475 					if(new_mode == WSDISPLAYIO_MODE_EMUL)
    476 					{
    477 						cg3_setup_palette(sc);
    478 						vcons_redraw_screen(ms);
    479 					}
    480 				}
    481 			}
    482 	}
    483 	return EPASSTHROUGH;
    484 }
    485 
    486 paddr_t
    487 cgthree_mmap(void *v, void *vs, off_t offset, int prot)
    488 {
    489 	/* I'm not at all sure this is the right thing to do */
    490 	return cgthreemmap(0, offset, prot); /* assume minor dev 0 for now */
    491 }
    492 
    493 int
    494 cgthree_putcmap(struct cgthree_softc *sc, struct wsdisplay_cmap *cm)
    495 {
    496 	u_int index = cm->index;
    497 	u_int count = cm->count;
    498 	int error,i;
    499 	if (index >= 256 || count > 256 || index + count > 256)
    500 		return EINVAL;
    501 
    502 	for (i = 0; i < count; i++)
    503 	{
    504 		error = copyin(&cm->red[i],
    505 		    &sc->sc_cmap.cm_map[index + i][0], 1);
    506 		if (error)
    507 			return error;
    508 		error = copyin(&cm->green[i],
    509 		    &sc->sc_cmap.cm_map[index + i][1],
    510 		    1);
    511 		if (error)
    512 			return error;
    513 		error = copyin(&cm->blue[i],
    514 		    &sc->sc_cmap.cm_map[index + i][2], 1);
    515 		if (error)
    516 			return error;
    517 	}
    518 	cgthreeloadcmap(sc, index, count);
    519 
    520 	return 0;
    521 }
    522 
    523 int
    524 cgthree_getcmap(struct cgthree_softc *sc, struct wsdisplay_cmap *cm)
    525 {
    526 	u_int index = cm->index;
    527 	u_int count = cm->count;
    528 	int error,i;
    529 
    530 	if (index >= 256 || count > 256 || index + count > 256)
    531 		return EINVAL;
    532 
    533 	for (i = 0; i < count; i++)
    534 	{
    535 		error = copyout(&sc->sc_cmap.cm_map[index + i][0],
    536 		    &cm->red[i], 1);
    537 		if (error)
    538 			return error;
    539 		error = copyout(&sc->sc_cmap.cm_map[index + i][1],
    540 		    &cm->green[i], 1);
    541 		if (error)
    542 			return error;
    543 		error = copyout(&sc->sc_cmap.cm_map[index + i][2],
    544 		    &cm->blue[i], 1);
    545 		if (error)
    546 			return error;
    547 	}
    548 
    549 	return 0;
    550 }
    551 
    552 void
    553 cgthree_init_screen(void *cookie, struct vcons_screen *scr,
    554     int existing, long *defattr)
    555 {
    556 	struct cgthree_softc *sc = cookie;
    557 	struct rasops_info *ri = &scr->scr_ri;
    558 
    559 	scr->scr_flags |= VCONS_DONT_READ;
    560 
    561 	ri->ri_depth = 8;
    562 	ri->ri_width = sc->sc_width;
    563 	ri->ri_height = sc->sc_height;
    564 	ri->ri_stride = sc->sc_stride;
    565 	ri->ri_flg = RI_CENTER;
    566 
    567 	ri->ri_bits = sc->sc_fb.fb_pixels;
    568 
    569 	rasops_init(ri, 0, 0);
    570 	ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_REVERSE;
    571 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    572 		    sc->sc_width / ri->ri_font->fontwidth);
    573 
    574 	ri->ri_hw = scr;
    575 }
    576 
    577 #endif /* NWSDISPLAY > 0 */
    578