1/*	$NetBSD: cgtwo.c,v 1.58 2023/12/20 05:33:18 thorpej Exp $ */
2
3/*
4 * Copyright (c) 1992, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * This software was developed by the Computer Systems Engineering group
8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 * contributed to Berkeley.
10 *
11 * All advertising materials mentioning features or use of this software
12 * must display the following acknowledgement:
13 *	This product includes software developed by the University of
14 *	California, Lawrence Berkeley Laboratory.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 *    notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 *    notice, this list of conditions and the following disclaimer in the
23 *    documentation and/or other materials provided with the distribution.
24 * 3. Neither the name of the University nor the names of its contributors
25 *    may be used to endorse or promote products derived from this software
26 *    without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 *	from: @(#)cgthree.c	8.2 (Berkeley) 10/30/93
41 */
42
43/*
44 * color display (cgtwo) driver.
45 *
46 * Does not handle interrupts, even though they can occur.
47 *
48 * XXX should defer colormap updates to vertical retrace interrupts
49 */
50
51#include <sys/cdefs.h>
52__KERNEL_RCSID(0, "$NetBSD: cgtwo.c,v 1.58 2023/12/20 05:33:18 thorpej Exp $");
53
54#include <sys/param.h>
55#include <sys/systm.h>
56#include <sys/buf.h>
57#include <sys/device.h>
58#include <sys/ioctl.h>
59#include <sys/mman.h>
60#include <sys/tty.h>
61#include <sys/conf.h>
62
63#include <uvm/uvm_extern.h>
64
65#include <machine/autoconf.h>
66
67#include <dev/sun/fbio.h>
68#include <dev/sun/fbvar.h>
69
70#include <dev/vme/vmevar.h>
71
72#include <machine/eeprom.h>
73#include <machine/cgtworeg.h>
74
75
76/* per-display variables */
77struct cgtwo_softc {
78	struct	fbdevice sc_fb;		/* frame buffer device */
79	vme_addr_t		sc_paddr;
80	vme_chipset_tag_t	sc_ct;
81	bus_space_tag_t		sc_bt;
82	volatile struct cg2statusreg *sc_reg;	/* CG2 control registers */
83	volatile u_short *sc_cmap;
84#define sc_redmap(sc)	((sc)->sc_cmap)
85#define sc_greenmap(sc)	((sc)->sc_cmap + CG2_CMSIZE)
86#define sc_bluemap(sc)	((sc)->sc_cmap + 2 * CG2_CMSIZE)
87};
88
89/* autoconfiguration driver */
90static int	cgtwomatch(device_t, cfdata_t, void *);
91static void	cgtwoattach(device_t, device_t, void *);
92static void	cgtwounblank(device_t);
93int		cgtwogetcmap(struct cgtwo_softc *, struct fbcmap *);
94int		cgtwoputcmap(struct cgtwo_softc *, struct fbcmap *);
95
96CFATTACH_DECL_NEW(cgtwo, sizeof(struct cgtwo_softc),
97    cgtwomatch, cgtwoattach, NULL, NULL);
98
99extern struct cfdriver cgtwo_cd;
100
101dev_type_open(cgtwoopen);
102dev_type_ioctl(cgtwoioctl);
103dev_type_mmap(cgtwommap);
104
105const struct cdevsw cgtwo_cdevsw = {
106	.d_open = cgtwoopen,
107	.d_close = nullclose,
108	.d_read = noread,
109	.d_write = nowrite,
110	.d_ioctl = cgtwoioctl,
111	.d_stop = nostop,
112	.d_tty = notty,
113	.d_poll = nopoll,
114	.d_mmap = cgtwommap,
115	.d_kqfilter = nokqfilter,
116	.d_discard = nodiscard,
117	.d_flag = 0
118};
119
120/* frame buffer generic driver */
121static struct fbdriver cgtwofbdriver = {
122	cgtwounblank, cgtwoopen, nullclose, cgtwoioctl, nopoll, cgtwommap,
123	nokqfilter
124};
125
126/*
127 * Match a cgtwo.
128 */
129static int
130cgtwomatch(device_t parent, cfdata_t cf, void *aux)
131{
132	struct vme_attach_args	*va = aux;
133	vme_chipset_tag_t	ct = va->va_vct;
134	vme_am_t		mod;
135
136	/*
137	 * Mask out invalid flags from the user.
138	 */
139	cf->cf_flags &= FB_USERMASK;
140
141	mod = 0x3d; /* VME_AM_A24 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA */
142	if (vme_probe(ct, va->r[0].offset + CG2_CTLREG_OFF, 2, mod, VME_D16,
143			  0, 0)) {
144		return (0);
145	}
146
147	return (1);
148}
149
150/*
151 * Attach a display.  We need to notice if it is the console, too.
152 */
153static void
154cgtwoattach(device_t parent, device_t self, void *aux)
155{
156	struct vme_attach_args	*va = aux;
157	vme_chipset_tag_t	ct = va->va_vct;
158	bus_space_tag_t		bt;
159	bus_space_handle_t	bh;
160	vme_am_t		mod;
161	vme_mapresc_t resc;
162	struct cgtwo_softc *sc = device_private(self);
163	struct fbdevice *fb = &sc->sc_fb;
164	struct eeprom *eep = (struct eeprom *)eeprom_va;
165	int isconsole = 0;
166
167	sc->sc_ct = ct;
168	fb->fb_driver = &cgtwofbdriver;
169	fb->fb_device = self;
170	fb->fb_type.fb_type = FBTYPE_SUN2COLOR;
171	fb->fb_flags = device_cfdata(self)->cf_flags;
172
173	fb->fb_type.fb_depth = 8;
174	fb_setsize_eeprom(fb, fb->fb_type.fb_depth, 1152, 900);
175
176	fb->fb_type.fb_cmsize = 256;
177	fb->fb_type.fb_size = roundup(CG2_MAPPED_SIZE, PAGE_SIZE);
178	printf(": cgtwo, %d x %d",
179	       fb->fb_type.fb_width, fb->fb_type.fb_height);
180
181	/*
182	 * When the ROM has mapped in a cgtwo display, the address
183	 * maps only the video RAM, so in any case we have to map the
184	 * registers ourselves.  We only need the video RAM if we are
185	 * going to print characters via rconsole.
186	 */
187	sc->sc_paddr = va->r[0].offset;
188	mod = 0x3d; /* VME_AM_A24 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA */
189
190	if (vme_space_map(ct, sc->sc_paddr + CG2_ROPMEM_OFF +
191				offsetof(struct cg2fb, status.reg),
192			sizeof(struct cg2statusreg), mod, VME_D16, 0,
193			&bt, &bh, &resc) != 0)
194		panic("cgtwo: vme_map status");
195	sc->sc_bt = bt;
196	sc->sc_reg = (volatile struct cg2statusreg *)bh; /* XXX */
197
198	if (vme_space_map(ct, sc->sc_paddr + CG2_ROPMEM_OFF +
199				offsetof(struct cg2fb, redmap[0]),
200			3 * CG2_CMSIZE, mod, VME_D16, 0,
201			&bt, &bh, &resc) != 0)
202		panic("cgtwo: vme_map cmap");
203	sc->sc_cmap = (volatile u_short *)bh; /* XXX */
204
205	/*
206	 * Assume this is the console if there's no eeprom info
207	 * to be found.
208	 */
209	if (eep == NULL || eep->eeConsole == EE_CONS_COLOR)
210		isconsole = fb_is_console(0);
211	else
212		isconsole = 0;
213
214	if (isconsole) {
215		if (vme_space_map(ct, sc->sc_paddr + CG2_PIXMAP_OFF,
216				CG2_PIXMAP_SIZE, mod, VME_D16, 0,
217				  &bt, &bh, &resc) != 0)
218			panic("cgtwo: vme_map pixels");
219
220		fb->fb_pixels = (void *)bh; /* XXX */
221		printf(" (console)\n");
222#ifdef RASTERCONSOLE
223		fbrcons_init(fb);
224#endif
225	} else
226		printf("\n");
227
228	fb_attach(fb, isconsole);
229}
230
231int
232cgtwoopen(dev_t dev, int flags, int mode, struct lwp *l)
233{
234	int unit = minor(dev);
235
236	if (device_lookup(&cgtwo_cd, unit) == NULL)
237		return (ENXIO);
238	return (0);
239}
240
241int
242cgtwoioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
243{
244	register struct cgtwo_softc *sc = device_lookup_private(&cgtwo_cd,
245								minor(dev));
246	register struct fbgattr *fba;
247
248	switch (cmd) {
249
250	case FBIOGTYPE:
251		*(struct fbtype *)data = sc->sc_fb.fb_type;
252		break;
253
254	case FBIOGATTR:
255		fba = (struct fbgattr *)data;
256		fba->real_type = sc->sc_fb.fb_type.fb_type;
257		fba->owner = 0;		/* XXX ??? */
258		fba->fbtype = sc->sc_fb.fb_type;
259		fba->sattr.flags = 0;
260		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
261		fba->sattr.dev_specific[0] = -1;
262		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
263		fba->emu_types[1] = -1;
264		break;
265
266	case FBIOGETCMAP:
267		return cgtwogetcmap(sc, (struct fbcmap *) data);
268
269	case FBIOPUTCMAP:
270		return cgtwoputcmap(sc, (struct fbcmap *) data);
271
272	case FBIOGVIDEO:
273		*(int *)data = sc->sc_reg->video_enab;
274		break;
275
276	case FBIOSVIDEO:
277		sc->sc_reg->video_enab = (*(int*)data) & 1;
278		break;
279
280	default:
281		return (ENOTTY);
282	}
283	return (0);
284}
285
286/*
287 * Undo the effect of an FBIOSVIDEO that turns the video off.
288 */
289static void
290cgtwounblank(device_t dev)
291{
292	struct cgtwo_softc *sc = device_private(dev);
293	sc->sc_reg->video_enab = 1;
294}
295
296/*
297 */
298int
299cgtwogetcmap(struct cgtwo_softc *sc, struct fbcmap *cmap)
300{
301	u_char red[CG2_CMSIZE], green[CG2_CMSIZE], blue[CG2_CMSIZE];
302	int error, start, count, ecount;
303	register u_int i;
304	register volatile u_short *p;
305
306	start = cmap->index;
307	count = cmap->count;
308	ecount = start + count;
309	if (start >= CG2_CMSIZE || count > CG2_CMSIZE - start)
310		return (EINVAL);
311
312	/* XXX - Wait for retrace? */
313
314	/* Copy hardware to local arrays. */
315	p = &sc_redmap(sc)[start];
316	for (i = start; i < ecount; i++)
317		red[i] = *p++;
318	p = &sc_greenmap(sc)[start];
319	for (i = start; i < ecount; i++)
320		green[i] = *p++;
321	p = &sc_bluemap(sc)[start];
322	for (i = start; i < ecount; i++)
323		blue[i] = *p++;
324
325	/* Copy local arrays to user space. */
326	if ((error = copyout(red + start, cmap->red, count)) != 0)
327		return (error);
328	if ((error = copyout(green + start, cmap->green, count)) != 0)
329		return (error);
330	if ((error = copyout(blue + start, cmap->blue, count)) != 0)
331		return (error);
332
333	return (0);
334}
335
336/*
337 */
338int
339cgtwoputcmap(struct cgtwo_softc *sc, struct fbcmap *cmap)
340{
341	u_char red[CG2_CMSIZE], green[CG2_CMSIZE], blue[CG2_CMSIZE];
342	int error;
343	u_int start, count, ecount;
344	register u_int i;
345	register volatile u_short *p;
346
347	start = cmap->index;
348	count = cmap->count;
349	ecount = start + count;
350	if (start >= CG2_CMSIZE || count > CG2_CMSIZE - start)
351		return (EINVAL);
352
353	/* Copy from user space to local arrays. */
354	if ((error = copyin(cmap->red, red + start, count)) != 0)
355		return (error);
356	if ((error = copyin(cmap->green, green + start, count)) != 0)
357		return (error);
358	if ((error = copyin(cmap->blue, blue + start, count)) != 0)
359		return (error);
360
361	/* XXX - Wait for retrace? */
362
363	/* Copy from local arrays to hardware. */
364	p = &sc_redmap(sc)[start];
365	for (i = start; i < ecount; i++)
366		*p++ = red[i];
367	p = &sc_greenmap(sc)[start];
368	for (i = start; i < ecount; i++)
369		*p++ = green[i];
370	p = &sc_bluemap(sc)[start];
371	for (i = start; i < ecount; i++)
372		*p++ = blue[i];
373
374	return (0);
375}
376
377/*
378 * Return the address that would map the given device at the given
379 * offset, allowing for the given protection, or return -1 for error.
380 */
381paddr_t
382cgtwommap(dev_t dev, off_t off, int prot)
383{
384	extern int sparc_vme_mmap_cookie(vme_addr_t, vme_am_t,
385					 bus_space_handle_t *);
386
387	register struct cgtwo_softc *sc = device_lookup_private(&cgtwo_cd,
388								minor(dev));
389	vme_am_t mod;
390	bus_space_handle_t bh;
391
392	if (off & PGOFSET)
393		panic("cgtwommap");
394
395	if (off >= sc->sc_fb.fb_type.fb_size)
396		return (-1);
397
398	/* Apparently, the pixels are in 32-bit data space */
399	mod = 0x3d; /* VME_AM_A24 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA */
400
401	if (sparc_vme_mmap_cookie(sc->sc_paddr + off, mod, &bh) != 0)
402		panic("cgtwommap");
403
404	return ((paddr_t)bh);
405}
406