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