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