xref: /netbsd-src/sys/arch/sparc/dev/cgeight.c (revision 1dc652ef5a0bffbd0917f95e0797bad8c6fc8efd)
1 /*	$NetBSD: cgeight.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 (cgeight) 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: cgeight.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 <uvm/uvm_extern.h>
117 
118 #include <machine/autoconf.h>
119 #include <machine/eeprom.h>
120 
121 #include <dev/sun/fbio.h>
122 #include <dev/sun/fbvar.h>
123 #include <dev/sun/btreg.h>
124 #include <dev/sun/btvar.h>
125 #include <dev/sun/pfourreg.h>
126 
127 /* per-display variables */
128 struct cgeight_softc {
129 	struct fbdevice	sc_fb;		/* frame buffer device */
130 	bus_space_tag_t	sc_bustag;
131 	bus_addr_t	sc_paddr;	/* phys address for device mmap() */
132 
133 	volatile struct fbcontrol *sc_fbc;	/* Brooktree registers */
134 	union bt_cmap	sc_cmap;	/* Brooktree color map */
135 };
136 
137 /* autoconfiguration driver */
138 static void	cgeightattach(device_t, device_t, void *);
139 static int	cgeightmatch(device_t, cfdata_t, void *);
140 #if defined(SUN4)
141 static void	cgeightunblank(device_t);
142 #endif
143 
144 static int	cg8_pfour_probe(void *, void *);
145 
146 CFATTACH_DECL_NEW(cgeight, sizeof(struct cgeight_softc),
147     cgeightmatch, cgeightattach, NULL, NULL);
148 
149 extern struct cfdriver cgeight_cd;
150 
151 dev_type_open(cgeightopen);
152 dev_type_ioctl(cgeightioctl);
153 dev_type_mmap(cgeightmmap);
154 
155 const struct cdevsw cgeight_cdevsw = {
156 	.d_open = cgeightopen,
157 	.d_close = nullclose,
158 	.d_read = noread,
159 	.d_write = nowrite,
160 	.d_ioctl = cgeightioctl,
161 	.d_stop = nostop,
162 	.d_tty = notty,
163 	.d_poll = nopoll,
164 	.d_mmap = cgeightmmap,
165 	.d_kqfilter = nokqfilter,
166 	.d_discard = nodiscard,
167 	.d_flag = 0
168 };
169 
170 #if defined(SUN4)
171 /* frame buffer generic driver */
172 static struct fbdriver cgeightfbdriver = {
173 	cgeightunblank, cgeightopen, nullclose, cgeightioctl,
174 	nopoll, cgeightmmap, nokqfilter
175 };
176 
177 static void cgeightloadcmap(struct cgeight_softc *, int, int);
178 static int cgeight_get_video(struct cgeight_softc *);
179 static void cgeight_set_video(struct cgeight_softc *, int);
180 #endif
181 
182 /*
183  * Match a cgeight.
184  */
185 static int
cgeightmatch(device_t parent,cfdata_t cf,void * aux)186 cgeightmatch(device_t parent, cfdata_t cf, void *aux)
187 {
188 	union obio_attach_args *uoba = aux;
189 	struct obio4_attach_args *oba;
190 
191 	if (uoba->uoba_isobio4 == 0)
192 		return (0);
193 
194 	oba = &uoba->uoba_oba4;
195 	return (bus_space_probe(oba->oba_bustag, oba->oba_paddr,
196 				4,	/* probe size */
197 				0,	/* offset */
198 				0,	/* flags */
199 				cg8_pfour_probe, NULL));
200 }
201 
202 static int
cg8_pfour_probe(void * vaddr,void * arg)203 cg8_pfour_probe(void *vaddr, void *arg)
204 {
205 
206 	return (fb_pfour_id(vaddr) == PFOUR_ID_COLOR24);
207 }
208 
209 /*
210  * Attach a display.  We need to notice if it is the console, too.
211  */
212 static void
cgeightattach(device_t parent,device_t self,void * aux)213 cgeightattach(device_t parent, device_t self, void *aux)
214 {
215 #if defined(SUN4)
216 	union obio_attach_args *uoba = aux;
217 	struct obio4_attach_args *oba = &uoba->uoba_oba4;
218 	struct cgeight_softc *sc = device_private(self);
219 	struct fbdevice *fb = &sc->sc_fb;
220 	bus_space_handle_t bh;
221 	volatile struct bt_regs *bt;
222 	int ramsize, i, isconsole;
223 
224 	sc->sc_bustag = oba->oba_bustag;
225 	sc->sc_paddr = (bus_addr_t)oba->oba_paddr;
226 
227 	/* Map the pfour register. */
228 	if (bus_space_map(oba->oba_bustag, oba->oba_paddr,
229 			  sizeof(uint32_t),
230 			  BUS_SPACE_MAP_LINEAR,
231 			  &bh) != 0) {
232 		printf("%s: cannot map pfour register\n",
233 			device_xname(self));
234 		return;
235 	}
236 	fb->fb_pfour = (volatile uint32_t *)bh;
237 
238 	fb->fb_driver = &cgeightfbdriver;
239 	fb->fb_device = self;
240 	fb->fb_type.fb_type = FBTYPE_MEMCOLOR;
241 	fb->fb_flags = device_cfdata(self)->cf_flags & FB_USERMASK;
242 	fb->fb_flags |= FB_PFOUR;
243 
244 	ramsize = PFOUR_COLOR_OFF_END - PFOUR_COLOR_OFF_OVERLAY;
245 
246 	fb->fb_type.fb_depth = 24;
247 	fb_setsize_eeprom(fb, fb->fb_type.fb_depth, 1152, 900);
248 
249 	sc->sc_fb.fb_type.fb_cmsize = 256;
250 	sc->sc_fb.fb_type.fb_size = ramsize;
251 	printf(": cgeight/p4, %d x %d",
252 		fb->fb_type.fb_width,
253 		fb->fb_type.fb_height);
254 
255 	isconsole = 0;
256 
257 	if (CPU_ISSUN4) {
258 		struct eeprom *eep = (struct eeprom *)eeprom_va;
259 
260 		/*
261 		 * Assume this is the console if there's no eeprom info
262 		 * to be found.
263 		 */
264 		if (eep == NULL || eep->eeConsole == EE_CONS_P4OPT)
265 			isconsole = fb_is_console(0);
266 	}
267 
268 #if 0
269 	/*
270 	 * We don't do any of the console handling here.  Instead,
271 	 * we let the bwtwo driver pick up the overlay plane and
272 	 * use it instead.  Rconsole should have better performance
273 	 * with the 1-bit depth.
274 	 *      -- Jason R. Thorpe <thorpej@NetBSD.org>
275 	 */
276 
277 	/*
278 	 * When the ROM has mapped in a cgfour display, the address
279 	 * maps only the video RAM, so in any case we have to map the
280 	 * registers ourselves.  We only need the video RAM if we are
281 	 * going to print characters via rconsole.
282 	 */
283 
284 	if (isconsole) {
285 		/* XXX this is kind of a waste */
286 		fb->fb_pixels = mapiodev(ca->ca_ra.ra_reg,
287 					 PFOUR_COLOR_OFF_OVERLAY, ramsize);
288 	}
289 #endif
290 
291 	/* Map the Brooktree. */
292 	if (bus_space_map(oba->oba_bustag,
293 			  oba->oba_paddr + PFOUR_COLOR_OFF_CMAP,
294 			  sizeof(struct fbcontrol),
295 			  BUS_SPACE_MAP_LINEAR,
296 			  &bh) != 0) {
297 		printf("%s: cannot map control registers\n",
298 			device_xname(self));
299 		return;
300 	}
301 	sc->sc_fbc = (volatile struct fbcontrol *)bh;
302 
303 #if 0	/* XXX thorpej ??? */
304 	/* tell the enable plane to look at the mono image */
305 	memset(ca->ca_ra.ra_vaddr, 0xff,
306 	    sc->sc_fb.fb_type.fb_width * sc->sc_fb.fb_type.fb_height / 8);
307 #endif
308 
309 	/* grab initial (current) color map */
310 	bt = &sc->sc_fbc->fbc_dac;
311 	bt->bt_addr = 0;
312 	for (i = 0; i < 256 * 3 / 4; i++)
313 		sc->sc_cmap.cm_chip[i] = bt->bt_cmap;
314 
315 	BT_INIT(bt, 0);
316 
317 #if 0	/* see above */
318 	if (isconsole) {
319 		printf(" (console)\n");
320 #if defined(RASTERCONSOLE) && 0	/* XXX been told it doesn't work well. */
321 		fbrcons_init(fb);
322 #endif
323 	} else
324 #endif /* 0 */
325 		printf("\n");
326 
327 	/*
328 	 * Even though we're not using rconsole, we'd still like
329 	 * to notice if we're the console framebuffer.
330 	 */
331 	fb_attach(&sc->sc_fb, isconsole);
332 #endif
333 }
334 
335 int
cgeightopen(dev_t dev,int flags,int mode,struct lwp * l)336 cgeightopen(dev_t dev, int flags, int mode, struct lwp *l)
337 {
338 	int unit = minor(dev);
339 
340 	if (device_lookup(&cgeight_cd, unit) == NULL)
341 		return (ENXIO);
342 	return (0);
343 }
344 
345 int
cgeightioctl(dev_t dev,u_long cmd,void * data,int flags,struct lwp * l)346 cgeightioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
347 {
348 #if defined(SUN4)
349 	struct cgeight_softc *sc = device_lookup_private(&cgeight_cd,
350 							 minor(dev));
351 	struct fbgattr *fba;
352 	int error;
353 
354 	switch (cmd) {
355 
356 	case FBIOGTYPE:
357 		*(struct fbtype *)data = sc->sc_fb.fb_type;
358 		break;
359 
360 	case FBIOGATTR:
361 		fba = (struct fbgattr *)data;
362 		fba->real_type = sc->sc_fb.fb_type.fb_type;
363 		fba->owner = 0;		/* XXX ??? */
364 		fba->fbtype = sc->sc_fb.fb_type;
365 		fba->sattr.flags = 0;
366 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
367 		fba->sattr.dev_specific[0] = -1;
368 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
369 		fba->emu_types[1] = -1;
370 		break;
371 
372 	case FBIOGETCMAP:
373 #define p ((struct fbcmap *)data)
374 		return (bt_getcmap(p, &sc->sc_cmap, 256, 1));
375 
376 	case FBIOPUTCMAP:
377 		/* copy to software map */
378 		error = bt_putcmap(p, &sc->sc_cmap, 256, 1);
379 		if (error)
380 			return (error);
381 		/* now blast them into the chip */
382 		/* XXX should use retrace interrupt */
383 		cgeightloadcmap(sc, p->index, p->count);
384 #undef p
385 		break;
386 
387 	case FBIOGVIDEO:
388 		*(int *)data = cgeight_get_video(sc);
389 		break;
390 
391 	case FBIOSVIDEO:
392 		cgeight_set_video(sc, *(int *)data);
393 		break;
394 
395 	default:
396 		return (ENOTTY);
397 	}
398 #endif /* SUN4 */
399 
400 	return (0);
401 }
402 
403 /*
404  * Return the address that would map the given device at the given
405  * offset, allowing for the given protection, or return -1 for error.
406  *
407  * The cg8 maps its overlay plane at 0 for 128K, followed by the
408  * enable plane for 128K, followed by the colour for as long as it
409  * goes. Starting at 8MB, it maps the ramdac for PAGE_SIZE, then the p4
410  * register for PAGE_SIZE, then the bootrom for 0x40000.
411  */
412 paddr_t
cgeightmmap(dev_t dev,off_t off,int prot)413 cgeightmmap(dev_t dev, off_t off, int prot)
414 {
415 	struct cgeight_softc *sc = device_lookup_private(&cgeight_cd,
416 							 minor(dev));
417 	off_t poff;
418 
419 #define START_ENABLE	(128*1024)
420 #define START_COLOR	((128*1024) + (128*1024))
421 #define COLOR_SIZE	(sc->sc_fb.fb_type.fb_width * \
422 			    sc->sc_fb.fb_type.fb_height * 3)
423 #define END_COLOR	(START_COLOR + COLOR_SIZE)
424 #define START_SPECIAL	0x800000
425 #define PROMSIZE	0x40000
426 #define NOOVERLAY	(0x04000000)
427 
428 	if (off & PGOFSET)
429 		panic("cgeightmap");
430 
431 	if (off < 0)
432 		return (-1);
433 	else if ((u_int)off >= NOOVERLAY) {
434 		off -= NOOVERLAY;
435 
436 		/*
437 		 * X11 maps a huge chunk of the frame buffer; far more than
438 		 * there really is. We compensate by double-mapping the
439 		 * first page for as many other pages as it wants
440 		 */
441 		while ((u_int)off >= COLOR_SIZE)
442 			off -= COLOR_SIZE;	/* XXX thorpej ??? */
443 
444 		poff = off + PFOUR_COLOR_OFF_COLOR;
445 	} else if ((u_int)off < START_ENABLE) {
446 		/*
447 		 * in overlay plane
448 		 */
449 		poff = PFOUR_COLOR_OFF_OVERLAY + off;
450 	} else if ((u_int)off < START_COLOR) {
451 		/*
452 		 * in enable plane
453 		 */
454 		poff = (off - START_ENABLE) + PFOUR_COLOR_OFF_ENABLE;
455 	} else if ((u_int)off < sc->sc_fb.fb_type.fb_size) {
456 		/*
457 		 * in colour plane
458 		 */
459 		poff = (off - START_COLOR) + PFOUR_COLOR_OFF_COLOR;
460 	} else if ((u_int)off < START_SPECIAL) {
461 		/*
462 		 * hole
463 		 */
464 		poff = 0;	/* XXX */
465 	} else if ((u_int)off == START_SPECIAL) {
466 		/*
467 		 * colour map (Brooktree)
468 		 */
469 		poff = PFOUR_COLOR_OFF_CMAP;
470 	} else if ((u_int)off == START_SPECIAL + PAGE_SIZE) {
471 		/*
472 		 * p4 register
473 		 */
474 		poff = 0;
475 	} else if ((u_int)off > (START_SPECIAL + (PAGE_SIZE * 2)) &&
476 	    (u_int) off < (START_SPECIAL + (PAGE_SIZE * 2) + PROMSIZE)) {
477 		/*
478 		 * rom
479 		 */
480 		poff = 0x8000 + (off - (START_SPECIAL + (PAGE_SIZE * 2)));
481 	} else
482 		return (-1);
483 
484 	return (bus_space_mmap(sc->sc_bustag,
485 		sc->sc_paddr, poff,
486 		prot, BUS_SPACE_MAP_LINEAR));
487 }
488 
489 #if defined(SUN4)
490 /*
491  * Undo the effect of an FBIOSVIDEO that turns the video off.
492  */
493 static void
cgeightunblank(device_t dev)494 cgeightunblank(device_t dev)
495 {
496 
497 	cgeight_set_video(device_private(dev), 1);
498 }
499 
500 static int
cgeight_get_video(struct cgeight_softc * sc)501 cgeight_get_video(struct cgeight_softc *sc)
502 {
503 
504 	return (fb_pfour_get_video(&sc->sc_fb));
505 }
506 
507 static void
cgeight_set_video(struct cgeight_softc * sc,int enable)508 cgeight_set_video(struct cgeight_softc *sc, int enable)
509 {
510 
511 	fb_pfour_set_video(&sc->sc_fb, enable);
512 }
513 
514 /*
515  * Load a subset of the current (new) colormap into the Brooktree DAC.
516  */
517 static void
cgeightloadcmap(struct cgeight_softc * sc,int start,int ncolors)518 cgeightloadcmap(struct cgeight_softc *sc, int start, int ncolors)
519 {
520 	volatile struct bt_regs *bt;
521 	u_int *ip;
522 	int count;
523 
524 	ip = &sc->sc_cmap.cm_chip[BT_D4M3(start)];	/* start/4 * 3 */
525 	count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3;
526 	bt = &sc->sc_fbc->fbc_dac;
527 	bt->bt_addr = BT_D4M4(start);
528 	while (--count >= 0)
529 		bt->bt_cmap = *ip++;
530 }
531 #endif /* SUN4 */
532