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