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