cg2.c revision 1.33 1 1.33 tsutsui /* $NetBSD: cg2.c,v 1.33 2024/12/20 23:51:59 tsutsui Exp $ */
2 1.1 gwr
3 1.1 gwr /*
4 1.1 gwr * Copyright (c) 1992, 1993
5 1.1 gwr * The Regents of the University of California. All rights reserved.
6 1.1 gwr *
7 1.1 gwr * This software was developed by the Computer Systems Engineering group
8 1.1 gwr * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 1.1 gwr * contributed to Berkeley.
10 1.1 gwr *
11 1.1 gwr * All advertising materials mentioning features or use of this software
12 1.1 gwr * must display the following acknowledgement:
13 1.1 gwr * This product includes software developed by the University of
14 1.1 gwr * California, Lawrence Berkeley Laboratory.
15 1.1 gwr *
16 1.1 gwr * Redistribution and use in source and binary forms, with or without
17 1.1 gwr * modification, are permitted provided that the following conditions
18 1.1 gwr * are met:
19 1.1 gwr * 1. Redistributions of source code must retain the above copyright
20 1.1 gwr * notice, this list of conditions and the following disclaimer.
21 1.1 gwr * 2. Redistributions in binary form must reproduce the above copyright
22 1.1 gwr * notice, this list of conditions and the following disclaimer in the
23 1.1 gwr * documentation and/or other materials provided with the distribution.
24 1.24 agc * 3. Neither the name of the University nor the names of its contributors
25 1.1 gwr * may be used to endorse or promote products derived from this software
26 1.1 gwr * without specific prior written permission.
27 1.1 gwr *
28 1.1 gwr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 1.1 gwr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 1.1 gwr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 1.1 gwr * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 1.1 gwr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 1.1 gwr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 1.1 gwr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 1.1 gwr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 1.1 gwr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 1.1 gwr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 1.1 gwr * SUCH DAMAGE.
39 1.1 gwr *
40 1.1 gwr * from: @(#)cgthree.c 8.2 (Berkeley) 10/30/93
41 1.1 gwr */
42 1.1 gwr
43 1.1 gwr /*
44 1.1 gwr * color display (cg2) driver.
45 1.1 gwr *
46 1.1 gwr * Does not handle interrupts, even though they can occur.
47 1.1 gwr *
48 1.1 gwr * XXX should defer colormap updates to vertical retrace interrupts
49 1.1 gwr */
50 1.23 lukem
51 1.23 lukem #include <sys/cdefs.h>
52 1.33 tsutsui __KERNEL_RCSID(0, "$NetBSD: cg2.c,v 1.33 2024/12/20 23:51:59 tsutsui Exp $");
53 1.1 gwr
54 1.1 gwr #include <sys/param.h>
55 1.8 gwr #include <sys/systm.h>
56 1.8 gwr #include <sys/conf.h>
57 1.1 gwr #include <sys/device.h>
58 1.1 gwr #include <sys/ioctl.h>
59 1.1 gwr #include <sys/mman.h>
60 1.8 gwr #include <sys/proc.h>
61 1.1 gwr #include <sys/tty.h>
62 1.1 gwr
63 1.13 mrg #include <uvm/uvm_extern.h>
64 1.1 gwr
65 1.16 thorpej #include <dev/sun/fbio.h>
66 1.1 gwr #include <machine/autoconf.h>
67 1.1 gwr #include <machine/pmap.h>
68 1.1 gwr #include <machine/cg2reg.h>
69 1.1 gwr
70 1.29 tsutsui #include "ioconf.h"
71 1.1 gwr #include "fbvar.h"
72 1.1 gwr
73 1.1 gwr #define CMSIZE 256
74 1.1 gwr
75 1.1 gwr /* offset to and size of mapped part of frame buffer */
76 1.1 gwr #define PLANEMAP_SIZE 0x100000
77 1.1 gwr #define PIXELMAP_SIZE 0x100000
78 1.1 gwr #define ROWOPMAP_SIZE 0x100000
79 1.1 gwr
80 1.1 gwr #define CTLREGS_OFF 0x300000
81 1.1 gwr #define CTLREGS_SIZE 0x10600
82 1.1 gwr
83 1.1 gwr #define CG2_MAPPED_OFFSET (PLANEMAP_SIZE + PIXELMAP_SIZE)
84 1.2 gwr #define CG2_MAPPED_SIZE (CTLREGS_OFF + CTLREGS_SIZE)
85 1.1 gwr
86 1.1 gwr /* per-display variables */
87 1.1 gwr struct cg2_softc {
88 1.29 tsutsui device_t sc_dev; /* base device */
89 1.1 gwr struct fbdevice sc_fb; /* frame buffer device */
90 1.1 gwr int sc_phys; /* display RAM (phys addr) */
91 1.1 gwr int sc_pmtype; /* pmap type bits */
92 1.1 gwr struct cg2fb *sc_ctlreg; /* control registers */
93 1.1 gwr };
94 1.1 gwr
95 1.1 gwr /* autoconfiguration driver */
96 1.29 tsutsui static int cg2match(device_t, cfdata_t, void *);
97 1.29 tsutsui static void cg2attach(device_t, device_t, void *);
98 1.1 gwr
99 1.29 tsutsui CFATTACH_DECL_NEW(cgtwo, sizeof(struct cg2_softc),
100 1.21 thorpej cg2match, cg2attach, NULL, NULL);
101 1.11 gwr
102 1.18 gehenna dev_type_open(cg2open);
103 1.18 gehenna dev_type_ioctl(cg2ioctl);
104 1.18 gehenna dev_type_mmap(cg2mmap);
105 1.18 gehenna
106 1.18 gehenna const struct cdevsw cgtwo_cdevsw = {
107 1.30 dholland .d_open = cg2open,
108 1.30 dholland .d_close = nullclose,
109 1.30 dholland .d_read = noread,
110 1.30 dholland .d_write = nowrite,
111 1.30 dholland .d_ioctl = cg2ioctl,
112 1.30 dholland .d_stop = nostop,
113 1.30 dholland .d_tty = notty,
114 1.30 dholland .d_poll = nopoll,
115 1.30 dholland .d_mmap = cg2mmap,
116 1.30 dholland .d_kqfilter = nokqfilter,
117 1.31 dholland .d_discard = nodiscard,
118 1.30 dholland .d_flag = 0
119 1.18 gehenna };
120 1.18 gehenna
121 1.25 chs static int cg2gattr(struct fbdevice *, void *);
122 1.25 chs static int cg2gvideo(struct fbdevice *, void *);
123 1.25 chs static int cg2svideo(struct fbdevice *, void *);
124 1.25 chs static int cg2getcmap(struct fbdevice *, void *);
125 1.25 chs static int cg2putcmap(struct fbdevice *, void *);
126 1.1 gwr
127 1.1 gwr static struct fbdriver cg2fbdriver = {
128 1.22 jdolecek cg2open, nullclose, cg2mmap, nokqfilter, cg2gattr,
129 1.1 gwr cg2gvideo, cg2svideo,
130 1.1 gwr cg2getcmap, cg2putcmap };
131 1.1 gwr
132 1.25 chs static int cg2intr(void *);
133 1.1 gwr
134 1.1 gwr /*
135 1.1 gwr * Match a cg2.
136 1.1 gwr */
137 1.33 tsutsui static int
138 1.29 tsutsui cg2match(device_t parent, cfdata_t cf, void *aux)
139 1.1 gwr {
140 1.9 gwr struct confargs *ca = aux;
141 1.9 gwr int probe_addr;
142 1.1 gwr
143 1.9 gwr /* No default VME address. */
144 1.1 gwr if (ca->ca_paddr == -1)
145 1.29 tsutsui return 0;
146 1.1 gwr
147 1.9 gwr /* Make sure something is there... */
148 1.9 gwr probe_addr = ca->ca_paddr + CTLREGS_OFF;
149 1.9 gwr if (bus_peek(ca->ca_bustype, probe_addr, 1) == -1)
150 1.29 tsutsui return 0;
151 1.1 gwr
152 1.9 gwr /* XXX: look at the ID reg? */
153 1.29 tsutsui /* aprint_debug("cg2: id=0x%x\n", x); */
154 1.1 gwr
155 1.9 gwr /* Default interrupt priority. */
156 1.9 gwr if (ca->ca_intpri == -1)
157 1.9 gwr ca->ca_intpri = 4;
158 1.1 gwr
159 1.29 tsutsui return 1;
160 1.1 gwr }
161 1.1 gwr
162 1.1 gwr /*
163 1.1 gwr * Attach a display. We need to notice if it is the console, too.
164 1.1 gwr */
165 1.33 tsutsui static void
166 1.29 tsutsui cg2attach(device_t parent, device_t self, void *args)
167 1.1 gwr {
168 1.28 tsutsui struct cg2_softc *sc = device_private(self);
169 1.1 gwr struct fbdevice *fb = &sc->sc_fb;
170 1.1 gwr struct confargs *ca = args;
171 1.1 gwr struct fbtype *fbt;
172 1.1 gwr
173 1.29 tsutsui sc->sc_dev = self;
174 1.1 gwr sc->sc_phys = ca->ca_paddr;
175 1.1 gwr sc->sc_pmtype = PMAP_NC | PMAP_VME16;
176 1.1 gwr
177 1.29 tsutsui sc->sc_ctlreg = (struct cg2fb *)bus_mapin(ca->ca_bustype,
178 1.29 tsutsui ca->ca_paddr + CTLREGS_OFF, CTLREGS_SIZE);
179 1.1 gwr
180 1.29 tsutsui isr_add_vectored(cg2intr, sc, ca->ca_intpri, ca->ca_intvec);
181 1.1 gwr
182 1.1 gwr /*
183 1.1 gwr * XXX - Initialize? Determine type?
184 1.1 gwr */
185 1.1 gwr sc->sc_ctlreg->intrptvec.reg = ca->ca_intvec;
186 1.1 gwr sc->sc_ctlreg->status.word = 1;
187 1.1 gwr
188 1.1 gwr fb->fb_driver = &cg2fbdriver;
189 1.1 gwr fb->fb_private = sc;
190 1.29 tsutsui fb->fb_name = device_xname(self);
191 1.1 gwr
192 1.1 gwr fbt = &fb->fb_fbtype;
193 1.1 gwr fbt->fb_type = FBTYPE_SUN2COLOR;
194 1.1 gwr fbt->fb_depth = 8;
195 1.1 gwr fbt->fb_cmsize = CMSIZE;
196 1.1 gwr
197 1.1 gwr fbt->fb_width = 1152;
198 1.1 gwr fbt->fb_height = 900;
199 1.1 gwr fbt->fb_size = CG2_MAPPED_SIZE;
200 1.1 gwr
201 1.29 tsutsui aprint_normal(" (%dx%d)\n", fbt->fb_width, fbt->fb_height);
202 1.1 gwr fb_attach(fb, 2);
203 1.1 gwr }
204 1.1 gwr
205 1.33 tsutsui int
206 1.26 christos cg2open(dev_t dev, int flags, int mode, struct lwp *l)
207 1.1 gwr {
208 1.28 tsutsui struct cg2_softc *sc;
209 1.1 gwr int unit = minor(dev);
210 1.1 gwr
211 1.28 tsutsui sc = device_lookup_private(&cgtwo_cd, unit);
212 1.28 tsutsui if (sc == NULL)
213 1.29 tsutsui return ENXIO;
214 1.29 tsutsui return 0;
215 1.1 gwr }
216 1.1 gwr
217 1.33 tsutsui int
218 1.27 christos cg2ioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
219 1.1 gwr {
220 1.28 tsutsui struct cg2_softc *sc = device_lookup_private(&cgtwo_cd, minor(dev));
221 1.1 gwr
222 1.29 tsutsui return fbioctlfb(&sc->sc_fb, cmd, data);
223 1.1 gwr }
224 1.1 gwr
225 1.1 gwr /*
226 1.1 gwr * Return the address that would map the given device at the given
227 1.1 gwr * offset, allowing for the given protection, or return -1 for error.
228 1.1 gwr */
229 1.33 tsutsui paddr_t
230 1.25 chs cg2mmap(dev_t dev, off_t off, int prot)
231 1.1 gwr {
232 1.28 tsutsui struct cg2_softc *sc = device_lookup_private(&cgtwo_cd, minor(dev));
233 1.1 gwr
234 1.1 gwr if (off & PGOFSET)
235 1.29 tsutsui panic("%s: bad offset", __func__);
236 1.1 gwr
237 1.12 simonb if (off >= CG2_MAPPED_SIZE)
238 1.29 tsutsui return -1;
239 1.1 gwr
240 1.1 gwr /*
241 1.1 gwr * I turned on PMAP_NC here to disable the cache as I was
242 1.1 gwr * getting horribly broken behaviour with it on.
243 1.1 gwr */
244 1.29 tsutsui return (sc->sc_phys + off) | sc->sc_pmtype;
245 1.1 gwr }
246 1.1 gwr
247 1.1 gwr /*
248 1.1 gwr * Internal ioctl functions.
249 1.1 gwr */
250 1.1 gwr
251 1.1 gwr /* FBIOGATTR: */
252 1.33 tsutsui static int
253 1.25 chs cg2gattr(struct fbdevice *fb, void *data)
254 1.1 gwr {
255 1.8 gwr struct fbgattr *fba = data;
256 1.1 gwr
257 1.1 gwr fba->real_type = fb->fb_fbtype.fb_type;
258 1.1 gwr fba->owner = 0; /* XXX - TIOCCONS stuff? */
259 1.1 gwr fba->fbtype = fb->fb_fbtype;
260 1.1 gwr fba->sattr.flags = 0;
261 1.1 gwr fba->sattr.emu_type = fb->fb_fbtype.fb_type;
262 1.1 gwr fba->sattr.dev_specific[0] = -1;
263 1.1 gwr fba->emu_types[0] = fb->fb_fbtype.fb_type;
264 1.1 gwr fba->emu_types[1] = -1;
265 1.29 tsutsui return 0;
266 1.1 gwr }
267 1.1 gwr
268 1.1 gwr /* FBIOGVIDEO: */
269 1.33 tsutsui static int
270 1.25 chs cg2gvideo(struct fbdevice *fb, void *data)
271 1.1 gwr {
272 1.8 gwr int *on = data;
273 1.1 gwr struct cg2_softc *sc = fb->fb_private;
274 1.1 gwr
275 1.1 gwr *on = sc->sc_ctlreg->status.reg.video_enab;
276 1.29 tsutsui return 0;
277 1.1 gwr }
278 1.1 gwr
279 1.1 gwr /* FBIOSVIDEO: */
280 1.33 tsutsui static int
281 1.25 chs cg2svideo(struct fbdevice *fb, void *data)
282 1.1 gwr {
283 1.8 gwr int *on = data;
284 1.1 gwr struct cg2_softc *sc = fb->fb_private;
285 1.1 gwr
286 1.1 gwr sc->sc_ctlreg->status.reg.video_enab = (*on) & 1;
287 1.1 gwr
288 1.29 tsutsui return 0;
289 1.1 gwr }
290 1.1 gwr
291 1.1 gwr /* FBIOGETCMAP: */
292 1.33 tsutsui static int
293 1.25 chs cg2getcmap(struct fbdevice *fb, void *data)
294 1.1 gwr {
295 1.8 gwr struct fbcmap *cmap = data;
296 1.1 gwr struct cg2_softc *sc = fb->fb_private;
297 1.29 tsutsui uint8_t red[CMSIZE], green[CMSIZE], blue[CMSIZE];
298 1.1 gwr int error, start, count, ecount;
299 1.15 tsutsui u_int i;
300 1.29 tsutsui uint16_t *p;
301 1.1 gwr
302 1.1 gwr start = cmap->index;
303 1.1 gwr count = cmap->count;
304 1.1 gwr ecount = start + count;
305 1.17 itojun if (start >= CMSIZE || count > CMSIZE - start)
306 1.29 tsutsui return EINVAL;
307 1.1 gwr
308 1.1 gwr /* XXX - Wait for retrace? */
309 1.1 gwr
310 1.1 gwr /* Copy hardware to local arrays. */
311 1.1 gwr p = &sc->sc_ctlreg->redmap[start];
312 1.1 gwr for (i = start; i < ecount; i++)
313 1.1 gwr red[i] = *p++;
314 1.1 gwr p = &sc->sc_ctlreg->greenmap[start];
315 1.1 gwr for (i = start; i < ecount; i++)
316 1.1 gwr green[i] = *p++;
317 1.1 gwr p = &sc->sc_ctlreg->bluemap[start];
318 1.1 gwr for (i = start; i < ecount; i++)
319 1.1 gwr blue[i] = *p++;
320 1.1 gwr
321 1.1 gwr /* Copy local arrays to user space. */
322 1.1 gwr if ((error = copyout(red + start, cmap->red, count)) != 0)
323 1.29 tsutsui return error;
324 1.1 gwr if ((error = copyout(green + start, cmap->green, count)) != 0)
325 1.29 tsutsui return error;
326 1.1 gwr if ((error = copyout(blue + start, cmap->blue, count)) != 0)
327 1.29 tsutsui return error;
328 1.1 gwr
329 1.29 tsutsui return 0;
330 1.1 gwr }
331 1.1 gwr
332 1.1 gwr /* FBIOPUTCMAP: */
333 1.33 tsutsui static int
334 1.25 chs cg2putcmap(struct fbdevice *fb, void *data)
335 1.1 gwr {
336 1.8 gwr struct fbcmap *cmap = data;
337 1.1 gwr struct cg2_softc *sc = fb->fb_private;
338 1.29 tsutsui uint8_t red[CMSIZE], green[CMSIZE], blue[CMSIZE];
339 1.14 jdolecek int error;
340 1.14 jdolecek u_int start, count, ecount;
341 1.15 tsutsui u_int i;
342 1.29 tsutsui uint16_t *p;
343 1.1 gwr
344 1.1 gwr start = cmap->index;
345 1.1 gwr count = cmap->count;
346 1.1 gwr ecount = start + count;
347 1.17 itojun if (start >= CMSIZE || count > CMSIZE - start)
348 1.29 tsutsui return EINVAL;
349 1.1 gwr
350 1.1 gwr /* Copy from user space to local arrays. */
351 1.1 gwr if ((error = copyin(cmap->red, red + start, count)) != 0)
352 1.29 tsutsui return error;
353 1.1 gwr if ((error = copyin(cmap->green, green + start, count)) != 0)
354 1.29 tsutsui return error;
355 1.1 gwr if ((error = copyin(cmap->blue, blue + start, count)) != 0)
356 1.29 tsutsui return error;
357 1.1 gwr
358 1.1 gwr /* XXX - Wait for retrace? */
359 1.1 gwr
360 1.1 gwr /* Copy from local arrays to hardware. */
361 1.1 gwr p = &sc->sc_ctlreg->redmap[start];
362 1.1 gwr for (i = start; i < ecount; i++)
363 1.1 gwr *p++ = red[i];
364 1.1 gwr p = &sc->sc_ctlreg->greenmap[start];
365 1.1 gwr for (i = start; i < ecount; i++)
366 1.1 gwr *p++ = green[i];
367 1.1 gwr p = &sc->sc_ctlreg->bluemap[start];
368 1.1 gwr for (i = start; i < ecount; i++)
369 1.1 gwr *p++ = blue[i];
370 1.1 gwr
371 1.29 tsutsui return 0;
372 1.1 gwr
373 1.1 gwr }
374 1.1 gwr
375 1.33 tsutsui static int
376 1.25 chs cg2intr(void *vsc)
377 1.1 gwr {
378 1.1 gwr struct cg2_softc *sc = vsc;
379 1.1 gwr
380 1.1 gwr /* XXX - Just disable interrupts for now. */
381 1.1 gwr sc->sc_ctlreg->status.reg.inten = 0;
382 1.1 gwr
383 1.7 christos printf("cg2intr\n");
384 1.29 tsutsui return 1;
385 1.1 gwr }
386