xref: /netbsd-src/sys/arch/sparc/dev/cgfour.c (revision 1dc652ef5a0bffbd0917f95e0797bad8c6fc8efd)
1 /*	$NetBSD: cgfour.c,v 1.51 2023/12/20 05:33:18 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe.
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  * Copyright (c) 1992, 1993
34  *	The Regents of the University of California.  All rights reserved.
35  *
36  * This software was developed by the Computer Systems Engineering group
37  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
38  * contributed to Berkeley.
39  *
40  * All advertising materials mentioning features or use of this software
41  * must display the following acknowledgement:
42  *	This product includes software developed by the University of
43  *	California, Lawrence Berkeley Laboratory.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  * 1. Redistributions of source code must retain the above copyright
49  *    notice, this list of conditions and the following disclaimer.
50  * 2. Redistributions in binary form must reproduce the above copyright
51  *    notice, this list of conditions and the following disclaimer in the
52  *    documentation and/or other materials provided with the distribution.
53  * 3. Neither the name of the University nor the names of its contributors
54  *    may be used to endorse or promote products derived from this software
55  *    without specific prior written permission.
56  *
57  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67  * SUCH DAMAGE.
68  *
69  *	from @(#)cgthree.c	8.2 (Berkeley) 10/30/93
70  */
71 
72 /*
73  * Copyright (c) 1995 Theo de Raadt.  All rights reserved.
74  *
75  * Redistribution and use in source and binary forms, with or without
76  * modification, are permitted provided that the following conditions
77  * are met:
78  * 1. Redistributions of source code must retain the above copyright
79  *    notice, this list of conditions and the following disclaimer.
80  * 2. Redistributions in binary form must reproduce the above copyright
81  *    notice, this list of conditions and the following disclaimer in the
82  *    documentation and/or other materials provided with the distribution.
83  *
84  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
85  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
86  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
87  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
88  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
89  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
90  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
91  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
92  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
93  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
94  */
95 
96 /*
97  * color display (cgfour) driver.
98  *
99  * Does not handle interrupts, even though they can occur.
100  *
101  * XXX should defer colormap updates to vertical retrace interrupts
102  */
103 
104 #include <sys/cdefs.h>
105 __KERNEL_RCSID(0, "$NetBSD: cgfour.c,v 1.51 2023/12/20 05:33:18 thorpej Exp $");
106 
107 #include <sys/param.h>
108 #include <sys/systm.h>
109 #include <sys/buf.h>
110 #include <sys/device.h>
111 #include <sys/ioctl.h>
112 #include <sys/mman.h>
113 #include <sys/tty.h>
114 #include <sys/conf.h>
115 
116 #include <machine/autoconf.h>
117 #include <machine/eeprom.h>
118 
119 #include <dev/sun/fbio.h>
120 #include <dev/sun/fbvar.h>
121 #include <dev/sun/btreg.h>
122 #include <dev/sun/btvar.h>
123 #include <dev/sun/pfourreg.h>
124 
125 /* per-display variables */
126 struct cgfour_softc {
127 	struct fbdevice	sc_fb;		/* frame buffer device */
128 	bus_space_tag_t	sc_bustag;
129 	bus_addr_t	sc_paddr;	/* phys address for device mmap() */
130 
131 	volatile struct fbcontrol *sc_fbc;	/* Brooktree registers */
132 	union bt_cmap	sc_cmap;	/* Brooktree color map */
133 };
134 
135 /* autoconfiguration driver */
136 static int	cgfourmatch(device_t, cfdata_t, void *);
137 static void	cgfourattach(device_t, device_t, void *);
138 
139 #if defined(SUN4)
140 static void	cgfourunblank(device_t);
141 #endif
142 
143 static int	cg4_pfour_probe(void *, void *);
144 
145 CFATTACH_DECL_NEW(cgfour, sizeof(struct cgfour_softc),
146     cgfourmatch, cgfourattach, NULL, NULL);
147 
148 extern struct cfdriver cgfour_cd;
149 
150 dev_type_open(cgfouropen);
151 dev_type_ioctl(cgfourioctl);
152 dev_type_mmap(cgfourmmap);
153 
154 const struct cdevsw cgfour_cdevsw = {
155 	.d_open = cgfouropen,
156 	.d_close = nullclose,
157 	.d_read = noread,
158 	.d_write = nowrite,
159 	.d_ioctl = cgfourioctl,
160 	.d_stop = nostop,
161 	.d_tty = notty,
162 	.d_poll = nopoll,
163 	.d_mmap = cgfourmmap,
164 	.d_kqfilter = nokqfilter,
165 	.d_discard = nodiscard,
166 	.d_flag = 0
167 };
168 
169 #if defined(SUN4)
170 /* frame buffer generic driver */
171 static struct fbdriver cgfourfbdriver = {
172 	cgfourunblank, cgfouropen, nullclose, cgfourioctl, nopoll,
173 	cgfourmmap, nokqfilter
174 };
175 
176 static void cgfourloadcmap(struct cgfour_softc *, int, int);
177 static int cgfour_get_video(struct cgfour_softc *);
178 static void cgfour_set_video(struct cgfour_softc *, int);
179 #endif
180 
181 /*
182  * Match a cgfour.
183  */
184 static int
cgfourmatch(device_t parent,cfdata_t cf,void * aux)185 cgfourmatch(device_t parent, cfdata_t cf, void *aux)
186 {
187 	union obio_attach_args *uoba = aux;
188 	struct obio4_attach_args *oba;
189 
190 	if (uoba->uoba_isobio4 == 0)
191 		return (0);
192 
193 	oba = &uoba->uoba_oba4;
194 	return (bus_space_probe(oba->oba_bustag, oba->oba_paddr,
195 				4,	/* probe size */
196 				0,	/* offset */
197 				0,	/* flags */
198 				cg4_pfour_probe, NULL));
199 }
200 
201 static int
cg4_pfour_probe(void * vaddr,void * arg)202 cg4_pfour_probe(void *vaddr, void *arg)
203 {
204 
205 	return (fb_pfour_id(vaddr) == PFOUR_ID_COLOR8P1);
206 }
207 
208 /*
209  * Attach a display.  We need to notice if it is the console, too.
210  */
211 static void
cgfourattach(device_t parent,device_t self,void * aux)212 cgfourattach(device_t parent, device_t self, void *aux)
213 {
214 #if defined(SUN4)
215 	struct cgfour_softc *sc = device_private(self);
216 	union obio_attach_args *uoba = aux;
217 	struct obio4_attach_args *oba = &uoba->uoba_oba4;
218 	bus_space_handle_t bh;
219 	volatile struct bt_regs *bt;
220 	struct fbdevice *fb = &sc->sc_fb;
221 	int ramsize, i, isconsole;
222 
223 	sc->sc_bustag = oba->oba_bustag;
224 	sc->sc_paddr = (bus_addr_t)oba->oba_paddr;
225 
226 	/* Map the pfour register. */
227 	if (bus_space_map(oba->oba_bustag, oba->oba_paddr,
228 			  sizeof(uint32_t),
229 			  BUS_SPACE_MAP_LINEAR,
230 			  &bh) != 0) {
231 		printf("%s: cannot map control registers\n",
232 			device_xname(self));
233 		return;
234 	}
235 	fb->fb_pfour = (volatile uint32_t *)bh;
236 
237 	fb->fb_driver = &cgfourfbdriver;
238 	fb->fb_device = self;
239 	fb->fb_type.fb_type = FBTYPE_SUN4COLOR;
240 	fb->fb_flags = device_cfdata(self)->cf_flags & FB_USERMASK;
241 	fb->fb_flags |= FB_PFOUR;
242 
243 	ramsize = PFOUR_COLOR_OFF_END - PFOUR_COLOR_OFF_OVERLAY;
244 
245 	fb->fb_type.fb_depth = 8;
246 	fb_setsize_eeprom(fb, fb->fb_type.fb_depth, 1152, 900);
247 
248 	fb->fb_type.fb_cmsize = 256;
249 	fb->fb_type.fb_size = ramsize;
250 	printf(": cgfour/p4, %d x %d",
251 		fb->fb_type.fb_width, fb->fb_type.fb_height);
252 
253 	isconsole = 0;
254 
255 	if (CPU_ISSUN4) {
256 		struct eeprom *eep = (struct eeprom *)eeprom_va;
257 
258 		/*
259 		 * Assume this is the console if there's no eeprom info
260 		 * to be found.
261 		 */
262 		if (eep == NULL || eep->eeConsole == EE_CONS_P4OPT)
263 			isconsole = fb_is_console(0);
264 	}
265 
266 #if 0
267 	/*
268 	 * We don't do any of the console handling here.  Instead,
269 	 * we let the bwtwo driver pick up the overlay plane and
270 	 * use it instead.  Rconsole should have better performance
271 	 * with the 1-bit depth.
272 	 *	-- Jason R. Thorpe <thorpej@NetBSD.org>
273 	 */
274 
275 	/*
276 	 * When the ROM has mapped in a cgfour display, the address
277 	 * maps only the video RAM, so in any case we have to map the
278 	 * registers ourselves.  We only need the video RAM if we are
279 	 * going to print characters via rconsole.
280 	 */
281 
282 	if (isconsole) {
283 		/* XXX this is kind of a waste */
284 		fb->fb_pixels = mapiodev(ca->ca_ra.ra_reg,
285 					 PFOUR_COLOR_OFF_OVERLAY, ramsize);
286 	}
287 #endif
288 
289 	/* Map the Brooktree. */
290 	if (bus_space_map(oba->oba_bustag,
291 			  oba->oba_paddr + PFOUR_COLOR_OFF_CMAP,
292 			  sizeof(struct fbcontrol),
293 			  BUS_SPACE_MAP_LINEAR,
294 			  &bh) != 0) {
295 		printf("%s: cannot map control registers\n",
296 			device_xname(self));
297 		return;
298 	}
299 	sc->sc_fbc = (volatile struct fbcontrol *)bh;
300 
301 	/* grab initial (current) color map */
302 	bt = &sc->sc_fbc->fbc_dac;
303 	bt->bt_addr = 0;
304 	for (i = 0; i < 256 * 3 / 4; i++)
305 		((char *)&sc->sc_cmap)[i] = bt->bt_cmap >> 24;
306 
307 	BT_INIT(bt, 24);
308 
309 #if 0	/* See above. */
310 	if (isconsole) {
311 		printf(" (console)\n");
312 #if defined(RASTERCONSOLE) && 0	/* XXX been told it doesn't work well. */
313 		fbrcons_init(fb);
314 #endif
315 	} else
316 #endif /* 0 */
317 		printf("\n");
318 
319 	/*
320 	 * Even though we're not using rconsole, we'd still like
321 	 * to notice if we're the console framebuffer.
322 	 */
323 	fb_attach(fb, isconsole);
324 #endif /* SUN4 */
325 }
326 
327 int
cgfouropen(dev_t dev,int flags,int mode,struct lwp * l)328 cgfouropen(dev_t dev, int flags, int mode, struct lwp *l)
329 {
330 	int unit = minor(dev);
331 
332 	if (device_lookup(&cgfour_cd, unit) == NULL)
333 		return (ENXIO);
334 	return (0);
335 }
336 
337 int
cgfourioctl(dev_t dev,u_long cmd,void * data,int flags,struct lwp * l)338 cgfourioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
339 {
340 #if defined(SUN4)
341 	struct cgfour_softc *sc = device_lookup_private(&cgfour_cd, minor(dev));
342 	struct fbgattr *fba;
343 	int error;
344 
345 	switch (cmd) {
346 
347 	case FBIOGTYPE:
348 		*(struct fbtype *)data = sc->sc_fb.fb_type;
349 		break;
350 
351 	case FBIOGATTR:
352 		fba = (struct fbgattr *)data;
353 		fba->real_type = sc->sc_fb.fb_type.fb_type;
354 		fba->owner = 0;		/* XXX ??? */
355 		fba->fbtype = sc->sc_fb.fb_type;
356 		fba->sattr.flags = 0;
357 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
358 		fba->sattr.dev_specific[0] = -1;
359 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
360 		fba->emu_types[1] = -1;
361 		break;
362 
363 	case FBIOGETCMAP:
364 #define p ((struct fbcmap *)data)
365 		return (bt_getcmap(p, &sc->sc_cmap, 256, 1));
366 
367 	case FBIOPUTCMAP:
368 		/* copy to software map */
369 		error = bt_putcmap(p, &sc->sc_cmap, 256, 1);
370 		if (error)
371 			return (error);
372 		/* now blast them into the chip */
373 		/* XXX should use retrace interrupt */
374 		cgfourloadcmap(sc, p->index, p->count);
375 #undef p
376 		break;
377 
378 	case FBIOGVIDEO:
379 		*(int *)data = cgfour_get_video(sc);
380 		break;
381 
382 	case FBIOSVIDEO:
383 		cgfour_set_video(sc, *(int *)data);
384 		break;
385 
386 	default:
387 		return (ENOTTY);
388 	}
389 #endif /* SUN4 */
390 
391 	return (0);
392 }
393 
394 /*
395  * Return the address that would map the given device at the given
396  * offset, allowing for the given protection, or return -1 for error.
397  *
398  * the cg4 maps its overlay plane for 128K, followed by the enable
399  * plane for 128K, followed by the colour plane (for as much colour
400  * as their is.)
401  *
402  * As well, mapping at an offset of 0x04000000 causes the cg4 to map
403  * only its colour plane, at 0.
404  */
405 paddr_t
cgfourmmap(dev_t dev,off_t off,int prot)406 cgfourmmap(dev_t dev, off_t off, int prot)
407 {
408 	struct cgfour_softc *sc = device_lookup_private(&cgfour_cd, minor(dev));
409 	off_t poff;
410 
411 #define START_ENABLE	(128*1024)
412 #define START_COLOR	((128*1024) + (128*1024))
413 #define COLOR_SIZE	(sc->sc_fb.fb_type.fb_width * \
414 			    sc->sc_fb.fb_type.fb_height)
415 #define END_COLOR	(START_COLOR + COLOR_SIZE)
416 #define NOOVERLAY	(0x04000000)
417 
418 	if (off & PGOFSET)
419 		panic("cgfourmap");
420 
421 	if (off < 0)
422 		return (-1);
423 	else if ((u_int)off >= NOOVERLAY) {
424 		off -= NOOVERLAY;
425 
426 		/*
427 		 * X11 maps a huge chunk of the frame buffer; far more than
428 		 * there really is. We compensate by double-mapping the
429 		 * first page for as many other pages as it wants
430 		 */
431 		while ((u_int)off >= COLOR_SIZE)
432 			off -= COLOR_SIZE;	/* XXX thorpej ??? */
433 
434 		poff = off + PFOUR_COLOR_OFF_COLOR;
435 	} else if ((u_int)off < START_ENABLE) {
436 		/*
437 		 * in overlay plane
438 		 */
439 		poff = PFOUR_COLOR_OFF_OVERLAY + off;
440 	} else if ((u_int)off < START_COLOR) {
441 		/*
442 		 * in enable plane
443 		 */
444 		poff = (off - START_ENABLE) + PFOUR_COLOR_OFF_ENABLE;
445 	} else if ((u_int)off < sc->sc_fb.fb_type.fb_size) {
446 		/*
447 		 * in colour plane
448 		 */
449 		poff = (off - START_COLOR) + PFOUR_COLOR_OFF_COLOR;
450 	} else
451 		return (-1);
452 
453 	return (bus_space_mmap(sc->sc_bustag,
454 		sc->sc_paddr, poff,
455 		prot, BUS_SPACE_MAP_LINEAR));
456 }
457 
458 #if defined(SUN4)
459 /*
460  * Undo the effect of an FBIOSVIDEO that turns the video off.
461  */
462 static void
cgfourunblank(device_t dev)463 cgfourunblank(device_t dev)
464 {
465 
466 	cgfour_set_video(device_private(dev), 1);
467 }
468 
469 static int
cgfour_get_video(struct cgfour_softc * sc)470 cgfour_get_video(struct cgfour_softc *sc)
471 {
472 
473 	return (fb_pfour_get_video(&sc->sc_fb));
474 }
475 
476 static void
cgfour_set_video(struct cgfour_softc * sc,int enable)477 cgfour_set_video(struct cgfour_softc *sc, int enable)
478 {
479 
480 	fb_pfour_set_video(&sc->sc_fb, enable);
481 }
482 
483 /*
484  * Load a subset of the current (new) colormap into the Brooktree DAC.
485  */
486 static void
cgfourloadcmap(struct cgfour_softc * sc,int start,int ncolors)487 cgfourloadcmap(struct cgfour_softc *sc, int start, int ncolors)
488 {
489 	volatile struct bt_regs *bt;
490 	u_int *ip, i;
491 	int count;
492 
493 	ip = &sc->sc_cmap.cm_chip[BT_D4M3(start)];	/* start/4 * 3 */
494 	count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3;
495 	bt = &sc->sc_fbc->fbc_dac;
496 	bt->bt_addr = BT_D4M4(start) << 24;
497 	while (--count >= 0) {
498 		i = *ip++;
499 		/* hardware that makes one want to pound boards with hammers */
500 		bt->bt_cmap = i;
501 		bt->bt_cmap = i << 8;
502 		bt->bt_cmap = i << 16;
503 		bt->bt_cmap = i << 24;
504 	}
505 }
506 #endif /* SUN4 */
507