cgfourteen.c revision 1.1 1 1.1 abrown /* $NetBSD: cgfourteen.c,v 1.1 1996/09/30 22:41:01 abrown Exp $ */
2 1.1 abrown
3 1.1 abrown /*
4 1.1 abrown * Copyright (c) 1996
5 1.1 abrown * The President and Fellows of Harvard College. All rights reserved.
6 1.1 abrown * Copyright (c) 1992, 1993
7 1.1 abrown * The Regents of the University of California. All rights reserved.
8 1.1 abrown *
9 1.1 abrown * This software was developed by the Computer Systems Engineering group
10 1.1 abrown * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
11 1.1 abrown * contributed to Berkeley.
12 1.1 abrown *
13 1.1 abrown * All advertising materials mentioning features or use of this software
14 1.1 abrown * must display the following acknowledgement:
15 1.1 abrown * This product includes software developed by Harvard University.
16 1.1 abrown * This product includes software developed by the University of
17 1.1 abrown * California, Lawrence Berkeley Laboratory.
18 1.1 abrown *
19 1.1 abrown * Redistribution and use in source and binary forms, with or without
20 1.1 abrown * modification, are permitted provided that the following conditions
21 1.1 abrown * are met:
22 1.1 abrown * 1. Redistributions of source code must retain the above copyright
23 1.1 abrown * notice, this list of conditions and the following disclaimer.
24 1.1 abrown * 2. Redistributions in binary form must reproduce the above copyright
25 1.1 abrown * notice, this list of conditions and the following disclaimer in the
26 1.1 abrown * documentation and/or other materials provided with the distribution.
27 1.1 abrown * 3. All advertising materials mentioning features or use of this software
28 1.1 abrown * must display the following acknowledgement:
29 1.1 abrown * This product includes software developed by the University of
30 1.1 abrown * California, Berkeley and its contributors.
31 1.1 abrown * This product includes software developed by Harvard University and
32 1.1 abrown * its contributors.
33 1.1 abrown * 4. Neither the name of the University nor the names of its contributors
34 1.1 abrown * may be used to endorse or promote products derived from this software
35 1.1 abrown * without specific prior written permission.
36 1.1 abrown *
37 1.1 abrown * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
38 1.1 abrown * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 1.1 abrown * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 1.1 abrown * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
41 1.1 abrown * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42 1.1 abrown * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
43 1.1 abrown * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 1.1 abrown * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45 1.1 abrown * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46 1.1 abrown * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 1.1 abrown * SUCH DAMAGE.
48 1.1 abrown *
49 1.1 abrown * Based on:
50 1.1 abrown * NetBSD: cgthree.c,v 1.28 1996/05/31 09:59:22 pk Exp
51 1.1 abrown * NetBSD: cgsix.c,v 1.25 1996/04/01 17:30:00 christos Exp
52 1.1 abrown */
53 1.1 abrown
54 1.1 abrown /*
55 1.1 abrown * Driver for Campus-II on-board mbus-based video (cgfourteen).
56 1.1 abrown * Provides minimum emulation of a Sun cgthree 8-bit framebuffer to
57 1.1 abrown * allow X to run.
58 1.1 abrown *
59 1.1 abrown * Does not handle interrupts, even though they can occur.
60 1.1 abrown *
61 1.1 abrown * XXX should defer colormap updates to vertical retrace interrupts
62 1.1 abrown */
63 1.1 abrown
64 1.1 abrown /*
65 1.1 abrown * The following is for debugging only; it opens up a security hole
66 1.1 abrown * enabled by allowing any user to map the control registers for the
67 1.1 abrown * cg14 into their space.
68 1.1 abrown */
69 1.1 abrown #undef CG14_MAP_REGS
70 1.1 abrown
71 1.1 abrown /*
72 1.1 abrown * The following enables 24-bit operation: when opened, the framebuffer
73 1.1 abrown * will switch to 24-bit mode (actually 32-bit mode), and provide a
74 1.1 abrown * simple cg8 emulation.
75 1.1 abrown *
76 1.1 abrown * XXX Note that the code enabled by this define is currently untested/broken.
77 1.1 abrown */
78 1.1 abrown #undef CG14_CG8
79 1.1 abrown
80 1.1 abrown #include <sys/param.h>
81 1.1 abrown #include <sys/systm.h>
82 1.1 abrown #include <sys/buf.h>
83 1.1 abrown #include <sys/device.h>
84 1.1 abrown #include <sys/ioctl.h>
85 1.1 abrown #include <sys/malloc.h>
86 1.1 abrown #include <sys/mman.h>
87 1.1 abrown #include <sys/tty.h>
88 1.1 abrown #include <sys/conf.h>
89 1.1 abrown
90 1.1 abrown #include <vm/vm.h>
91 1.1 abrown
92 1.1 abrown #include <machine/fbio.h>
93 1.1 abrown #include <machine/autoconf.h>
94 1.1 abrown #include <machine/pmap.h>
95 1.1 abrown #include <machine/fbvar.h>
96 1.1 abrown #include <machine/cpu.h>
97 1.1 abrown #include <machine/conf.h>
98 1.1 abrown
99 1.1 abrown #include <sparc/dev/cgfourteenreg.h>
100 1.1 abrown #include <sparc/dev/cgfourteenvar.h>
101 1.1 abrown
102 1.1 abrown /* autoconfiguration driver */
103 1.1 abrown static void cgfourteenattach(struct device *, struct device *, void *);
104 1.1 abrown static int cgfourteenmatch(struct device *, void *, void *);
105 1.1 abrown int cgfourteenopen __P((dev_t, int, int, struct proc *));
106 1.1 abrown int cgfourteenclose __P((dev_t, int, int, struct proc *));
107 1.1 abrown int cgfourteenioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
108 1.1 abrown int cgfourteenmmap __P((dev_t, int, int));
109 1.1 abrown static void cgfourteenunblank(struct device *);
110 1.1 abrown
111 1.1 abrown struct cfattach cgfourteen_ca = {
112 1.1 abrown sizeof(struct cgfourteen_softc), cgfourteenmatch, cgfourteenattach
113 1.1 abrown };
114 1.1 abrown
115 1.1 abrown struct cfdriver cgfourteen_cd = {
116 1.1 abrown NULL, "cgfourteen", DV_DULL
117 1.1 abrown };
118 1.1 abrown
119 1.1 abrown /* frame buffer generic driver */
120 1.1 abrown static struct fbdriver cgfourteenfbdriver = {
121 1.1 abrown cgfourteenunblank, cgfourteenopen, cgfourteenclose, cgfourteenioctl,
122 1.1 abrown cgfourteenmmap
123 1.1 abrown };
124 1.1 abrown
125 1.1 abrown extern int fbnode;
126 1.1 abrown extern struct tty *fbconstty;
127 1.1 abrown
128 1.1 abrown static void cg14_set_video __P((struct cgfourteen_softc *, int));
129 1.1 abrown static int cg14_get_video __P((struct cgfourteen_softc *));
130 1.1 abrown static int cg14_get_cmap __P((struct fbcmap *, union cg14cmap *, int));
131 1.1 abrown static int cg14_put_cmap __P((struct fbcmap *, union cg14cmap *, int));
132 1.1 abrown static void cg14_load_hwcmap __P((struct cgfourteen_softc *, int, int));
133 1.1 abrown static void cg14_init __P((struct cgfourteen_softc *));
134 1.1 abrown static void cg14_reset __P((struct cgfourteen_softc *));
135 1.1 abrown static void cg14_loadomap __P((struct cgfourteen_softc *));/* cursor overlay */
136 1.1 abrown static void cg14_setcursor __P((struct cgfourteen_softc *));/* set position */
137 1.1 abrown static void cg14_loadcursor __P((struct cgfourteen_softc *));/* set shape */
138 1.1 abrown
139 1.1 abrown /*
140 1.1 abrown * Match a cgfourteen.
141 1.1 abrown */
142 1.1 abrown int
143 1.1 abrown cgfourteenmatch(parent, vcf, aux)
144 1.1 abrown struct device *parent;
145 1.1 abrown void *vcf, *aux;
146 1.1 abrown {
147 1.1 abrown struct cfdata *cf = vcf;
148 1.1 abrown struct confargs *ca = aux;
149 1.1 abrown struct romaux *ra = &ca->ca_ra;
150 1.1 abrown
151 1.1 abrown /*
152 1.1 abrown * Mask out invalid flags from the user.
153 1.1 abrown */
154 1.1 abrown cf->cf_flags &= FB_USERMASK;
155 1.1 abrown
156 1.1 abrown /* Check driver name */
157 1.1 abrown if (strcmp(cf->cf_driver->cd_name, ra->ra_name))
158 1.1 abrown return (0);
159 1.1 abrown
160 1.1 abrown /*
161 1.1 abrown * The cgfourteen is a local-bus video adaptor, accessed directly
162 1.1 abrown * via the processor, and not through device space or an external
163 1.1 abrown * bus. Thus we look _only_ at the obio bus.
164 1.1 abrown * Additionally, these things exist only on the Sun4m.
165 1.1 abrown */
166 1.1 abrown if (CPU_ISSUN4M && ca->ca_bustype == BUS_OBIO)
167 1.1 abrown return(1);
168 1.1 abrown return (0);
169 1.1 abrown }
170 1.1 abrown
171 1.1 abrown /*
172 1.1 abrown * Attach a display. We need to notice if it is the console, too.
173 1.1 abrown */
174 1.1 abrown void
175 1.1 abrown cgfourteenattach(parent, self, args)
176 1.1 abrown struct device *parent, *self;
177 1.1 abrown void *args;
178 1.1 abrown {
179 1.1 abrown register struct cgfourteen_softc *sc = (struct cgfourteen_softc *)self;
180 1.1 abrown register struct confargs *ca = args;
181 1.1 abrown register int node = 0, ramsize;
182 1.1 abrown register u_int32_t *lut;
183 1.1 abrown int i, isconsole;
184 1.1 abrown
185 1.1 abrown sc->sc_fb.fb_driver = &cgfourteenfbdriver;
186 1.1 abrown sc->sc_fb.fb_device = &sc->sc_dev;
187 1.1 abrown sc->sc_fb.fb_flags = sc->sc_dev.dv_cfdata->cf_flags;
188 1.1 abrown
189 1.1 abrown /*
190 1.1 abrown * We're emulating a cg3/8, so represent ourselves as one
191 1.1 abrown */
192 1.1 abrown #ifdef CG14_CG8
193 1.1 abrown sc->sc_fb.fb_type.fb_type = FBTYPE_MEMCOLOR;
194 1.1 abrown #else
195 1.1 abrown sc->sc_fb.fb_type.fb_type = FBTYPE_SUN3COLOR;
196 1.1 abrown #endif
197 1.1 abrown
198 1.1 abrown node = ca->ca_ra.ra_node;
199 1.1 abrown #ifdef CG14_CG8
200 1.1 abrown sc->sc_fb.fb_type.fb_depth = 32;
201 1.1 abrown #else
202 1.1 abrown sc->sc_fb.fb_type.fb_depth = 8;
203 1.1 abrown #endif
204 1.1 abrown fb_setsize(&sc->sc_fb, sc->sc_fb.fb_type.fb_depth,
205 1.1 abrown 1152, 900, node, ca->ca_bustype);
206 1.1 abrown
207 1.1 abrown ramsize = roundup(sc->sc_fb.fb_type.fb_height * sc->sc_fb.fb_linebytes,
208 1.1 abrown NBPG);
209 1.1 abrown
210 1.1 abrown sc->sc_fb.fb_type.fb_cmsize = CG14_CLUT_SIZE;
211 1.1 abrown sc->sc_fb.fb_type.fb_size = ramsize;
212 1.1 abrown
213 1.1 abrown /*
214 1.1 abrown * Now map in the 8 useful pages of registers
215 1.1 abrown */
216 1.1 abrown if (ca->ca_ra.ra_len < 0x10000) {
217 1.1 abrown #ifdef DIAGNOSTIC
218 1.1 abrown printf("warning: can't find all cgfourteen registers...\n");
219 1.1 abrown #endif
220 1.1 abrown ca->ca_ra.ra_len = 0x10000;
221 1.1 abrown }
222 1.1 abrown sc->sc_ctl = (struct cg14ctl *) mapiodev(ca->ca_ra.ra_reg, 0,
223 1.1 abrown ca->ca_ra.ra_len,
224 1.1 abrown ca->ca_bustype);
225 1.1 abrown
226 1.1 abrown sc->sc_hwc = (struct cg14curs *) ((u_int)sc->sc_ctl +
227 1.1 abrown CG14_OFFSET_CURS);
228 1.1 abrown sc->sc_dac = (struct cg14dac *) ((u_int)sc->sc_ctl +
229 1.1 abrown CG14_OFFSET_DAC);
230 1.1 abrown sc->sc_xlut = (struct cg14xlut *) ((u_int)sc->sc_ctl +
231 1.1 abrown CG14_OFFSET_XLUT);
232 1.1 abrown sc->sc_clut1 = (struct cg14clut *) ((u_int)sc->sc_ctl +
233 1.1 abrown CG14_OFFSET_CLUT1);
234 1.1 abrown sc->sc_clut2 = (struct cg14clut *) ((u_int)sc->sc_ctl +
235 1.1 abrown CG14_OFFSET_CLUT2);
236 1.1 abrown sc->sc_clut3 = (struct cg14clut *) ((u_int)sc->sc_ctl +
237 1.1 abrown CG14_OFFSET_CLUT3);
238 1.1 abrown sc->sc_clutincr = (u_int *) ((u_int)sc->sc_ctl +
239 1.1 abrown CG14_OFFSET_CLUTINCR);
240 1.1 abrown
241 1.1 abrown /*
242 1.1 abrown * Stash the physical address of the framebuffer for use by mmap
243 1.1 abrown */
244 1.1 abrown if (ca->ca_ra.ra_nreg < 2)
245 1.1 abrown panic("cgfourteen with only one register set; can't find"
246 1.1 abrown " framebuffer");
247 1.1 abrown sc->sc_phys = ca->ca_ra.ra_reg[1];
248 1.1 abrown
249 1.1 abrown #if defined(DEBUG) && defined(CG14_MAP_REGS)
250 1.1 abrown /* Store the physical address of the control registers */
251 1.1 abrown sc->sc_regphys = ca->ca_ra.ra_reg[0];
252 1.1 abrown #endif
253 1.1 abrown
254 1.1 abrown /*
255 1.1 abrown * Let the user know that we're here
256 1.1 abrown */
257 1.1 abrown #ifdef CG14_CG8
258 1.1 abrown printf(": cgeight emulated at %dx%dx24bpp",
259 1.1 abrown sc->sc_fb.fb_type.fb_width, sc->sc_fb.fb_type.fb_height);
260 1.1 abrown #else
261 1.1 abrown printf(": cgthree emulated at %dx%dx8bpp",
262 1.1 abrown sc->sc_fb.fb_type.fb_width, sc->sc_fb.fb_type.fb_height);
263 1.1 abrown #endif
264 1.1 abrown /*
265 1.1 abrown * Enable the video, but don't change the pixel depth.
266 1.1 abrown */
267 1.1 abrown cg14_set_video(sc, 1);
268 1.1 abrown
269 1.1 abrown /*
270 1.1 abrown * Grab the initial colormap
271 1.1 abrown */
272 1.1 abrown lut = (u_int32_t *) sc->sc_clut1->clut_lut;
273 1.1 abrown for (i = 0; i < CG14_CLUT_SIZE; i++)
274 1.1 abrown sc->sc_cmap.cm_chip[i] = lut[i];
275 1.1 abrown
276 1.1 abrown /* See if we're the console */
277 1.1 abrown isconsole = node == fbnode && fbconstty != NULL;
278 1.1 abrown
279 1.1 abrown /*
280 1.1 abrown * We don't use the raster console since the cg14 is fast enough
281 1.1 abrown * already.
282 1.1 abrown */
283 1.1 abrown #ifdef notdef
284 1.1 abrown /*
285 1.1 abrown * When the ROM has mapped in a cgfourteen display, the address
286 1.1 abrown * maps only the video RAM, so in any case we have to map the
287 1.1 abrown * registers ourselves. We only need the video RAM if we are
288 1.1 abrown * going to print characters via rconsole.
289 1.1 abrown */
290 1.1 abrown if ((sc->sc_fb.fb_pixels = ca->ca_ra.ra_vaddr) == NULL && isconsole) {
291 1.1 abrown /* this probably cannot happen, but what the heck */
292 1.1 abrown sc->sc_fb.fb_pixels = mapiodev(ca->ca_ra.ra_reg, CG3REG_MEM,
293 1.1 abrown ramsize, ca->ca_bustype);
294 1.1 abrown }
295 1.1 abrown #endif /* notdef */
296 1.1 abrown
297 1.1 abrown
298 1.1 abrown if (isconsole) {
299 1.1 abrown printf(" (console)\n");
300 1.1 abrown #ifdef notdef
301 1.1 abrown #ifdef RASTERCONSOLE
302 1.1 abrown fbrcons_init(&sc->sc_fb);
303 1.1 abrown #endif
304 1.1 abrown #endif /* notdef */
305 1.1 abrown } else
306 1.1 abrown printf("\n");
307 1.1 abrown
308 1.1 abrown /* Attach to /dev/fb */
309 1.1 abrown if (node == fbnode)
310 1.1 abrown fb_attach(&sc->sc_fb, isconsole);
311 1.1 abrown }
312 1.1 abrown
313 1.1 abrown /*
314 1.1 abrown * Keep track of the number of opens made. In the 24-bit driver, we need to
315 1.1 abrown * switch to 24-bit mode on the first open, and switch back to 8-bit on
316 1.1 abrown * the last close. This kind of nonsense is needed to give screenblank
317 1.1 abrown * a fighting chance of working.
318 1.1 abrown */
319 1.1 abrown static int cg14_opens = 0;
320 1.1 abrown
321 1.1 abrown int
322 1.1 abrown cgfourteenopen(dev, flags, mode, p)
323 1.1 abrown dev_t dev;
324 1.1 abrown int flags, mode;
325 1.1 abrown struct proc *p;
326 1.1 abrown {
327 1.1 abrown register struct cgfourteen_softc *sc = cgfourteen_cd.cd_devs[minor(dev)];
328 1.1 abrown int unit = minor(dev);
329 1.1 abrown int s, oldopens;
330 1.1 abrown
331 1.1 abrown if (unit >= cgfourteen_cd.cd_ndevs ||
332 1.1 abrown cgfourteen_cd.cd_devs[unit] == NULL)
333 1.1 abrown return (ENXIO);
334 1.1 abrown
335 1.1 abrown s = splhigh();
336 1.1 abrown oldopens = cg14_opens++;
337 1.1 abrown splx(s);
338 1.1 abrown
339 1.1 abrown /* Setup the cg14 as we want it, and save the original PROM state */
340 1.1 abrown if (oldopens == 0) /* first open only, to make screenblank work */
341 1.1 abrown cg14_init(sc);
342 1.1 abrown
343 1.1 abrown return (0);
344 1.1 abrown }
345 1.1 abrown
346 1.1 abrown int
347 1.1 abrown cgfourteenclose(dev, flags, mode, p)
348 1.1 abrown dev_t dev;
349 1.1 abrown int flags, mode;
350 1.1 abrown struct proc *p;
351 1.1 abrown {
352 1.1 abrown register struct cgfourteen_softc *sc = cgfourteen_cd.cd_devs[minor(dev)];
353 1.1 abrown int s, opens;
354 1.1 abrown
355 1.1 abrown s = splhigh();
356 1.1 abrown opens = --cg14_opens;
357 1.1 abrown if (cg14_opens < 0)
358 1.1 abrown opens = cg14_opens = 0;
359 1.1 abrown splx(s);
360 1.1 abrown
361 1.1 abrown /*
362 1.1 abrown * Restore video state to make the PROM happy, on last close.
363 1.1 abrown */
364 1.1 abrown if (opens == 0)
365 1.1 abrown cg14_reset(sc);
366 1.1 abrown
367 1.1 abrown return (0);
368 1.1 abrown }
369 1.1 abrown
370 1.1 abrown int
371 1.1 abrown cgfourteenioctl(dev, cmd, data, flags, p)
372 1.1 abrown dev_t dev;
373 1.1 abrown u_long cmd;
374 1.1 abrown register caddr_t data;
375 1.1 abrown int flags;
376 1.1 abrown struct proc *p;
377 1.1 abrown {
378 1.1 abrown register struct cgfourteen_softc *sc = cgfourteen_cd.cd_devs[minor(dev)];
379 1.1 abrown register struct fbgattr *fba;
380 1.1 abrown union cg14cursor_cmap tcm;
381 1.1 abrown int v, error;
382 1.1 abrown u_int count;
383 1.1 abrown
384 1.1 abrown switch (cmd) {
385 1.1 abrown
386 1.1 abrown case FBIOGTYPE:
387 1.1 abrown *(struct fbtype *)data = sc->sc_fb.fb_type;
388 1.1 abrown break;
389 1.1 abrown
390 1.1 abrown case FBIOGATTR:
391 1.1 abrown fba = (struct fbgattr *)data;
392 1.1 abrown fba->real_type = FBTYPE_MDICOLOR;
393 1.1 abrown fba->owner = 0; /* XXX ??? */
394 1.1 abrown fba->fbtype = sc->sc_fb.fb_type;
395 1.1 abrown fba->sattr.flags = 0;
396 1.1 abrown fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
397 1.1 abrown fba->sattr.dev_specific[0] = -1;
398 1.1 abrown fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
399 1.1 abrown fba->emu_types[1] = -1;
400 1.1 abrown break;
401 1.1 abrown
402 1.1 abrown case FBIOGETCMAP:
403 1.1 abrown return (cg14_get_cmap((struct fbcmap *)data, &sc->sc_cmap,
404 1.1 abrown CG14_CLUT_SIZE));
405 1.1 abrown
406 1.1 abrown case FBIOPUTCMAP:
407 1.1 abrown /* copy to software map */
408 1.1 abrown #define p ((struct fbcmap *)data)
409 1.1 abrown #ifdef CG14_CG8
410 1.1 abrown p->index &= 0xffffff;
411 1.1 abrown #endif
412 1.1 abrown error = cg14_put_cmap(p, &sc->sc_cmap, CG14_CLUT_SIZE);
413 1.1 abrown if (error)
414 1.1 abrown return (error);
415 1.1 abrown /* now blast them into the chip */
416 1.1 abrown /* XXX should use retrace interrupt */
417 1.1 abrown cg14_load_hwcmap(sc, p->index, p->count);
418 1.1 abrown #undef p
419 1.1 abrown break;
420 1.1 abrown
421 1.1 abrown case FBIOGVIDEO:
422 1.1 abrown *(int *)data = cg14_get_video(sc);
423 1.1 abrown break;
424 1.1 abrown
425 1.1 abrown case FBIOSVIDEO:
426 1.1 abrown cg14_set_video(sc, *(int *)data);
427 1.1 abrown break;
428 1.1 abrown
429 1.1 abrown /* these are for both FBIOSCURSOR and FBIOGCURSOR */
430 1.1 abrown #define p ((struct fbcursor *)data)
431 1.1 abrown #define cc (&sc->sc_cursor)
432 1.1 abrown case FBIOGCURSOR:
433 1.1 abrown /* do not quite want everything here... */
434 1.1 abrown p->set = FB_CUR_SETALL; /* close enough, anyway */
435 1.1 abrown p->enable = cc->cc_enable;
436 1.1 abrown p->pos = cc->cc_pos;
437 1.1 abrown p->hot = cc->cc_hot;
438 1.1 abrown p->size = cc->cc_size;
439 1.1 abrown
440 1.1 abrown /* begin ugh ... can we lose some of this crap?? */
441 1.1 abrown if (p->image != NULL) {
442 1.1 abrown count = cc->cc_size.y * 32 / NBBY;
443 1.1 abrown error = copyout((caddr_t)cc->cc_cplane,
444 1.1 abrown (caddr_t)p->image, count);
445 1.1 abrown if (error)
446 1.1 abrown return (error);
447 1.1 abrown error = copyout((caddr_t)cc->cc_eplane,
448 1.1 abrown (caddr_t)p->mask, count);
449 1.1 abrown if (error)
450 1.1 abrown return (error);
451 1.1 abrown }
452 1.1 abrown if (p->cmap.red != NULL) {
453 1.1 abrown error = cg14_get_cmap(&p->cmap,
454 1.1 abrown (union cg14cmap *)&cc->cc_color, 2);
455 1.1 abrown if (error)
456 1.1 abrown return (error);
457 1.1 abrown } else {
458 1.1 abrown p->cmap.index = 0;
459 1.1 abrown p->cmap.count = 2;
460 1.1 abrown }
461 1.1 abrown /* end ugh */
462 1.1 abrown break;
463 1.1 abrown
464 1.1 abrown case FBIOSCURSOR:
465 1.1 abrown /*
466 1.1 abrown * For setcmap and setshape, verify parameters, so that
467 1.1 abrown * we do not get halfway through an update and then crap
468 1.1 abrown * out with the software state screwed up.
469 1.1 abrown */
470 1.1 abrown v = p->set;
471 1.1 abrown if (v & FB_CUR_SETCMAP) {
472 1.1 abrown /*
473 1.1 abrown * This use of a temporary copy of the cursor
474 1.1 abrown * colormap is not terribly efficient, but these
475 1.1 abrown * copies are small (8 bytes)...
476 1.1 abrown */
477 1.1 abrown tcm = cc->cc_color;
478 1.1 abrown error = cg14_put_cmap(&p->cmap, (union cg14cmap *)&tcm,
479 1.1 abrown 2);
480 1.1 abrown if (error)
481 1.1 abrown return (error);
482 1.1 abrown }
483 1.1 abrown if (v & FB_CUR_SETSHAPE) {
484 1.1 abrown if ((u_int)p->size.x > 32 || (u_int)p->size.y > 32)
485 1.1 abrown return (EINVAL);
486 1.1 abrown count = p->size.y * 32 / NBBY;
487 1.1 abrown if (!useracc(p->image, count, B_READ) ||
488 1.1 abrown !useracc(p->mask, count, B_READ))
489 1.1 abrown return (EFAULT);
490 1.1 abrown }
491 1.1 abrown
492 1.1 abrown /* parameters are OK; do it */
493 1.1 abrown if (v & (FB_CUR_SETCUR | FB_CUR_SETPOS | FB_CUR_SETHOT)) {
494 1.1 abrown if (v & FB_CUR_SETCUR)
495 1.1 abrown cc->cc_enable = p->enable;
496 1.1 abrown if (v & FB_CUR_SETPOS)
497 1.1 abrown cc->cc_pos = p->pos;
498 1.1 abrown if (v & FB_CUR_SETHOT)
499 1.1 abrown cc->cc_hot = p->hot;
500 1.1 abrown cg14_setcursor(sc);
501 1.1 abrown }
502 1.1 abrown if (v & FB_CUR_SETCMAP) {
503 1.1 abrown cc->cc_color = tcm;
504 1.1 abrown cg14_loadomap(sc); /* XXX defer to vertical retrace */
505 1.1 abrown }
506 1.1 abrown if (v & FB_CUR_SETSHAPE) {
507 1.1 abrown cc->cc_size = p->size;
508 1.1 abrown count = p->size.y * 32 / NBBY;
509 1.1 abrown bzero((caddr_t)cc->cc_eplane, sizeof cc->cc_eplane);
510 1.1 abrown bzero((caddr_t)cc->cc_cplane, sizeof cc->cc_cplane);
511 1.1 abrown bcopy(p->mask, (caddr_t)cc->cc_eplane, count);
512 1.1 abrown bcopy(p->image, (caddr_t)cc->cc_cplane, count);
513 1.1 abrown cg14_loadcursor(sc);
514 1.1 abrown }
515 1.1 abrown break;
516 1.1 abrown
517 1.1 abrown #undef cc
518 1.1 abrown #undef p
519 1.1 abrown case FBIOGCURPOS:
520 1.1 abrown *(struct fbcurpos *)data = sc->sc_cursor.cc_pos;
521 1.1 abrown break;
522 1.1 abrown
523 1.1 abrown case FBIOSCURPOS:
524 1.1 abrown sc->sc_cursor.cc_pos = *(struct fbcurpos *)data;
525 1.1 abrown cg14_setcursor(sc);
526 1.1 abrown break;
527 1.1 abrown
528 1.1 abrown case FBIOGCURMAX:
529 1.1 abrown /* max cursor size is 32x32 */
530 1.1 abrown ((struct fbcurpos *)data)->x = 32;
531 1.1 abrown ((struct fbcurpos *)data)->y = 32;
532 1.1 abrown break;
533 1.1 abrown
534 1.1 abrown default:
535 1.1 abrown return (ENOTTY);
536 1.1 abrown }
537 1.1 abrown return (0);
538 1.1 abrown }
539 1.1 abrown
540 1.1 abrown /*
541 1.1 abrown * Undo the effect of an FBIOSVIDEO that turns the video off.
542 1.1 abrown */
543 1.1 abrown static void
544 1.1 abrown cgfourteenunblank(dev)
545 1.1 abrown struct device *dev;
546 1.1 abrown {
547 1.1 abrown
548 1.1 abrown cg14_set_video((struct cgfourteen_softc *)dev, 1);
549 1.1 abrown }
550 1.1 abrown
551 1.1 abrown /*
552 1.1 abrown * Return the address that would map the given device at the given
553 1.1 abrown * offset, allowing for the given protection, or return -1 for error.
554 1.1 abrown *
555 1.1 abrown * The cg14 frame buffer can be mapped in either 8-bit or 32-bit mode
556 1.1 abrown * starting at the address stored in the PROM. In 8-bit mode, the X
557 1.1 abrown * channel is not present, and can be ignored. In 32-bit mode, mapping
558 1.1 abrown * at 0K delivers a 32-bpp buffer where the upper 8 bits select the X
559 1.1 abrown * channel information. We hardwire the Xlut to all zeroes to insure
560 1.1 abrown * that, regardless of this value, direct 24-bit color access will be
561 1.1 abrown * used.
562 1.1 abrown *
563 1.1 abrown * Alternatively, mapping the frame buffer at an offset of 16M seems to
564 1.1 abrown * tell the chip to ignore the X channel. XXX where does it get the X value
565 1.1 abrown * to use?
566 1.1 abrown */
567 1.1 abrown int
568 1.1 abrown cgfourteenmmap(dev, off, prot)
569 1.1 abrown dev_t dev;
570 1.1 abrown int off, prot;
571 1.1 abrown {
572 1.1 abrown register struct cgfourteen_softc *sc = cgfourteen_cd.cd_devs[minor(dev)];
573 1.1 abrown
574 1.1 abrown #define CG3START (128*1024 + 128*1024)
575 1.1 abrown #define CG8START (256*1024)
576 1.1 abrown #define NOOVERLAY (0x04000000)
577 1.1 abrown
578 1.1 abrown if (off & PGOFSET)
579 1.1 abrown panic("cgfourteenmmap");
580 1.1 abrown
581 1.1 abrown #if defined(DEBUG) && defined(CG14_MAP_REGS) /* XXX: security hole */
582 1.1 abrown /*
583 1.1 abrown * Map the control registers into user space. Should only be
584 1.1 abrown * used for debugging!
585 1.1 abrown */
586 1.1 abrown if ((u_int)off >= 0x10000000 && (u_int)off < 0x10000000 + 16*4096) {
587 1.1 abrown off -= 0x10000000;
588 1.1 abrown return (REG2PHYS(&sc->sc_regphys, off, 0) | PMAP_NC);
589 1.1 abrown }
590 1.1 abrown #endif
591 1.1 abrown
592 1.1 abrown if ((u_int)off >= NOOVERLAY)
593 1.1 abrown off -= NOOVERLAY;
594 1.1 abrown #ifdef CG14_CG8
595 1.1 abrown else if ((u_int)off >= CG8START) {
596 1.1 abrown off -= CG8START;
597 1.1 abrown }
598 1.1 abrown #else
599 1.1 abrown else if ((u_int)off >= CG3START)
600 1.1 abrown off -= CG3START;
601 1.1 abrown #endif
602 1.1 abrown else
603 1.1 abrown off = 0;
604 1.1 abrown
605 1.1 abrown if ((unsigned)off >= sc->sc_fb.fb_type.fb_size *
606 1.1 abrown sc->sc_fb.fb_type.fb_depth/8) {
607 1.1 abrown #ifdef DEBUG
608 1.1 abrown printf("\nmmap request out of bounds: request 0x%x, "
609 1.1 abrown "bound 0x%x\n", (unsigned) off,
610 1.1 abrown (unsigned)sc->sc_fb.fb_type.fb_size);
611 1.1 abrown #endif
612 1.1 abrown return (-1);
613 1.1 abrown }
614 1.1 abrown
615 1.1 abrown /*
616 1.1 abrown * Use PMAP_NC to disable the cache, since otherwise refresh is
617 1.1 abrown * very confused.
618 1.1 abrown */
619 1.1 abrown return (REG2PHYS(&sc->sc_phys, off, 0) | PMAP_NC);
620 1.1 abrown }
621 1.1 abrown
622 1.1 abrown /*
623 1.1 abrown * Miscellaneous helper functions
624 1.1 abrown */
625 1.1 abrown
626 1.1 abrown /* Initialize the framebuffer, storing away useful state for later reset */
627 1.1 abrown static void
628 1.1 abrown cg14_init(sc)
629 1.1 abrown struct cgfourteen_softc *sc;
630 1.1 abrown {
631 1.1 abrown register u_int32_t *clut;
632 1.1 abrown register u_int8_t *xlut;
633 1.1 abrown register int i;
634 1.1 abrown
635 1.1 abrown /*
636 1.1 abrown * We stash away the following to restore on close:
637 1.1 abrown *
638 1.1 abrown * color look-up table 1 (sc->sc_saveclut)
639 1.1 abrown * x look-up table (sc->sc_savexlut)
640 1.1 abrown * control register (sc->sc_savectl)
641 1.1 abrown * cursor control register (sc->sc_savehwc)
642 1.1 abrown */
643 1.1 abrown sc->sc_savectl = sc->sc_ctl->ctl_mctl;
644 1.1 abrown sc->sc_savehwc = sc->sc_hwc->curs_ctl;
645 1.1 abrown
646 1.1 abrown clut = (u_int32_t *) sc->sc_clut1->clut_lut;
647 1.1 abrown xlut = (u_int8_t *) sc->sc_xlut->xlut_lut;
648 1.1 abrown for (i = 0; i < CG14_CLUT_SIZE; i++) {
649 1.1 abrown sc->sc_saveclut.cm_chip[i] = clut[i];
650 1.1 abrown sc->sc_savexlut[i] = xlut[i];
651 1.1 abrown }
652 1.1 abrown
653 1.1 abrown #ifdef CG14_CG8
654 1.1 abrown /*
655 1.1 abrown * Enable the video, and put in 24 bit mode.
656 1.1 abrown */
657 1.1 abrown sc->sc_ctl->ctl_mctl = CG14_MCTL_ENABLEVID | CG14_MCTL_PIXMODE_32 |
658 1.1 abrown CG14_MCTL_POWERCTL;
659 1.1 abrown
660 1.1 abrown /*
661 1.1 abrown * Zero the xlut to enable direct-color mode
662 1.1 abrown */
663 1.1 abrown bzero(sc->sc_xlut, CG14_CLUT_SIZE);
664 1.1 abrown #else
665 1.1 abrown /*
666 1.1 abrown * Enable the video and put it in 8 bit mode
667 1.1 abrown */
668 1.1 abrown sc->sc_ctl->ctl_mctl = CG14_MCTL_ENABLEVID | CG14_MCTL_PIXMODE_8 |
669 1.1 abrown CG14_MCTL_POWERCTL;
670 1.1 abrown #endif
671 1.1 abrown }
672 1.1 abrown
673 1.1 abrown static void
674 1.1 abrown cg14_reset(sc) /* Restore the state saved on cg14_init */
675 1.1 abrown struct cgfourteen_softc *sc;
676 1.1 abrown {
677 1.1 abrown register u_int32_t *clut;
678 1.1 abrown register u_int8_t *xlut;
679 1.1 abrown register int i;
680 1.1 abrown
681 1.1 abrown /*
682 1.1 abrown * We restore the following, saved in cg14_init:
683 1.1 abrown *
684 1.1 abrown * color look-up table 1 (sc->sc_saveclut)
685 1.1 abrown * x look-up table (sc->sc_savexlut)
686 1.1 abrown * control register (sc->sc_savectl)
687 1.1 abrown * cursor control register (sc->sc_savehwc)
688 1.1 abrown *
689 1.1 abrown * Note that we don't touch the video enable bits in the
690 1.1 abrown * control register; otherwise, screenblank wouldn't work.
691 1.1 abrown */
692 1.1 abrown sc->sc_ctl->ctl_mctl = (sc->sc_ctl->ctl_mctl & (CG14_MCTL_ENABLEVID |
693 1.1 abrown CG14_MCTL_POWERCTL)) |
694 1.1 abrown (sc->sc_savectl & ~(CG14_MCTL_ENABLEVID |
695 1.1 abrown CG14_MCTL_POWERCTL));
696 1.1 abrown sc->sc_hwc->curs_ctl = sc->sc_savehwc;
697 1.1 abrown
698 1.1 abrown clut = (u_int32_t *) sc->sc_clut1->clut_lut;
699 1.1 abrown xlut = (u_int8_t *) sc->sc_xlut->xlut_lut;
700 1.1 abrown for (i = 0; i < CG14_CLUT_SIZE; i++) {
701 1.1 abrown clut[i] = sc->sc_saveclut.cm_chip[i];
702 1.1 abrown xlut[i] = sc->sc_savexlut[i];
703 1.1 abrown }
704 1.1 abrown }
705 1.1 abrown
706 1.1 abrown /* Enable/disable video display; power down monitor if DPMS-capable */
707 1.1 abrown static void
708 1.1 abrown cg14_set_video(sc, enable)
709 1.1 abrown struct cgfourteen_softc *sc;
710 1.1 abrown int enable;
711 1.1 abrown {
712 1.1 abrown /*
713 1.1 abrown * We can only use DPMS to power down the display if the chip revision
714 1.1 abrown * is greater than 0.
715 1.1 abrown */
716 1.1 abrown if (enable) {
717 1.1 abrown if ((sc->sc_ctl->ctl_rsr & CG14_RSR_REVMASK) > 0)
718 1.1 abrown sc->sc_ctl->ctl_mctl |= (CG14_MCTL_ENABLEVID |
719 1.1 abrown CG14_MCTL_POWERCTL);
720 1.1 abrown else
721 1.1 abrown sc->sc_ctl->ctl_mctl |= CG14_MCTL_ENABLEVID;
722 1.1 abrown } else {
723 1.1 abrown if ((sc->sc_ctl->ctl_rsr & CG14_RSR_REVMASK) > 0)
724 1.1 abrown sc->sc_ctl->ctl_mctl &= ~(CG14_MCTL_ENABLEVID |
725 1.1 abrown CG14_MCTL_POWERCTL);
726 1.1 abrown else
727 1.1 abrown sc->sc_ctl->ctl_mctl &= ~CG14_MCTL_ENABLEVID;
728 1.1 abrown }
729 1.1 abrown }
730 1.1 abrown
731 1.1 abrown /* Get status of video display */
732 1.1 abrown static int
733 1.1 abrown cg14_get_video(sc)
734 1.1 abrown struct cgfourteen_softc *sc;
735 1.1 abrown {
736 1.1 abrown return ((sc->sc_ctl->ctl_mctl & CG14_MCTL_ENABLEVID) != 0);
737 1.1 abrown }
738 1.1 abrown
739 1.1 abrown /* Read the software shadow colormap */
740 1.1 abrown static int
741 1.1 abrown cg14_get_cmap(p, cm, cmsize)
742 1.1 abrown register struct fbcmap *p;
743 1.1 abrown union cg14cmap *cm;
744 1.1 abrown int cmsize;
745 1.1 abrown {
746 1.1 abrown register u_int i, start, count;
747 1.1 abrown register u_char *cp;
748 1.1 abrown
749 1.1 abrown start = p->index;
750 1.1 abrown count = p->count;
751 1.1 abrown if (start >= cmsize || start + count > cmsize)
752 1.1 abrown #ifdef DEBUG
753 1.1 abrown {
754 1.1 abrown printf("putcmaperror: start %d cmsize %d count %d\n",
755 1.1 abrown start,cmsize,count);
756 1.1 abrown #endif
757 1.1 abrown return (EINVAL);
758 1.1 abrown #ifdef DEBUG
759 1.1 abrown }
760 1.1 abrown #endif
761 1.1 abrown
762 1.1 abrown if (!useracc(p->red, count, B_WRITE) ||
763 1.1 abrown !useracc(p->green, count, B_WRITE) ||
764 1.1 abrown !useracc(p->blue, count, B_WRITE))
765 1.1 abrown return (EFAULT);
766 1.1 abrown for (cp = &cm->cm_map[start][0], i = 0; i < count; cp += 4, i++) {
767 1.1 abrown p->red[i] = cp[3];
768 1.1 abrown p->green[i] = cp[2];
769 1.1 abrown p->blue[i] = cp[1];
770 1.1 abrown }
771 1.1 abrown return (0);
772 1.1 abrown }
773 1.1 abrown
774 1.1 abrown /* Write the software shadow colormap */
775 1.1 abrown static int
776 1.1 abrown cg14_put_cmap(p, cm, cmsize)
777 1.1 abrown register struct fbcmap *p;
778 1.1 abrown union cg14cmap *cm;
779 1.1 abrown int cmsize;
780 1.1 abrown {
781 1.1 abrown register u_int i, start, count;
782 1.1 abrown register u_char *cp;
783 1.1 abrown
784 1.1 abrown start = p->index;
785 1.1 abrown count = p->count;
786 1.1 abrown if (start >= cmsize || start + count > cmsize)
787 1.1 abrown #ifdef DEBUG
788 1.1 abrown {
789 1.1 abrown printf("putcmaperror: start %d cmsize %d count %d\n",
790 1.1 abrown start,cmsize,count);
791 1.1 abrown #endif
792 1.1 abrown return (EINVAL);
793 1.1 abrown #ifdef DEBUG
794 1.1 abrown }
795 1.1 abrown #endif
796 1.1 abrown
797 1.1 abrown if (!useracc(p->red, count, B_READ) ||
798 1.1 abrown !useracc(p->green, count, B_READ) ||
799 1.1 abrown !useracc(p->blue, count, B_READ))
800 1.1 abrown return (EFAULT);
801 1.1 abrown for (cp = &cm->cm_map[start][0], i = 0; i < count; cp += 4, i++) {
802 1.1 abrown cp[3] = p->red[i];
803 1.1 abrown cp[2] = p->green[i];
804 1.1 abrown cp[1] = p->blue[i];
805 1.1 abrown cp[0] = 0; /* no alpha channel */
806 1.1 abrown }
807 1.1 abrown return (0);
808 1.1 abrown }
809 1.1 abrown
810 1.1 abrown static void
811 1.1 abrown cg14_load_hwcmap(sc, start, ncolors)
812 1.1 abrown register struct cgfourteen_softc *sc;
813 1.1 abrown register int start, ncolors;
814 1.1 abrown {
815 1.1 abrown /* XXX switch to auto-increment, and on retrace intr */
816 1.1 abrown
817 1.1 abrown /* Setup pointers to source and dest */
818 1.1 abrown register u_int32_t *colp = &sc->sc_cmap.cm_chip[start];
819 1.1 abrown volatile register u_int32_t *lutp = &sc->sc_clut1->clut_lut[start];
820 1.1 abrown
821 1.1 abrown /* Copy by words */
822 1.1 abrown while (--ncolors >= 0)
823 1.1 abrown *lutp++ = *colp++;
824 1.1 abrown }
825 1.1 abrown
826 1.1 abrown /*
827 1.1 abrown * Load the cursor (overlay `foreground' and `background') colors.
828 1.1 abrown */
829 1.1 abrown static void
830 1.1 abrown cg14_setcursor(sc)
831 1.1 abrown register struct cgfourteen_softc *sc;
832 1.1 abrown {
833 1.1 abrown /* we need to subtract the hot-spot value here */
834 1.1 abrown #define COORD(f) (sc->sc_cursor.cc_pos.f - sc->sc_cursor.cc_hot.f)
835 1.1 abrown
836 1.1 abrown sc->sc_hwc->curs_ctl = (sc->sc_cursor.cc_enable ? CG14_CURS_ENABLE : 0);
837 1.1 abrown sc->sc_hwc->curs_x = COORD(x);
838 1.1 abrown sc->sc_hwc->curs_y = COORD(y);
839 1.1 abrown
840 1.1 abrown #undef COORD
841 1.1 abrown }
842 1.1 abrown
843 1.1 abrown static void
844 1.1 abrown cg14_loadcursor(sc)
845 1.1 abrown register struct cgfourteen_softc *sc;
846 1.1 abrown {
847 1.1 abrown register volatile struct cg14curs *hwc;
848 1.1 abrown register u_int edgemask, m;
849 1.1 abrown register int i;
850 1.1 abrown
851 1.1 abrown /*
852 1.1 abrown * Keep the top size.x bits. Here we *throw out* the top
853 1.1 abrown * size.x bits from an all-one-bits word, introducing zeros in
854 1.1 abrown * the top size.x bits, then invert all the bits to get what
855 1.1 abrown * we really wanted as our mask. But this fails if size.x is
856 1.1 abrown * 32---a sparc uses only the low 5 bits of the shift count---
857 1.1 abrown * so we have to special case that.
858 1.1 abrown */
859 1.1 abrown edgemask = ~0;
860 1.1 abrown if (sc->sc_cursor.cc_size.x < 32)
861 1.1 abrown edgemask = ~(edgemask >> sc->sc_cursor.cc_size.x);
862 1.1 abrown hwc = sc->sc_hwc;
863 1.1 abrown for (i = 0; i < 32; i++) {
864 1.1 abrown m = sc->sc_cursor.cc_eplane[i] & edgemask;
865 1.1 abrown hwc->curs_plane0[i] = m;
866 1.1 abrown hwc->curs_plane1[i] = m & sc->sc_cursor.cc_cplane[i];
867 1.1 abrown }
868 1.1 abrown }
869 1.1 abrown
870 1.1 abrown static void
871 1.1 abrown cg14_loadomap(sc)
872 1.1 abrown register struct cgfourteen_softc *sc;
873 1.1 abrown {
874 1.1 abrown /* set background color */
875 1.1 abrown sc->sc_hwc->curs_color1 = sc->sc_cursor.cc_color.cm_chip[0];
876 1.1 abrown /* set foreground color */
877 1.1 abrown sc->sc_hwc->curs_color2 = sc->sc_cursor.cc_color.cm_chip[1];
878 1.1 abrown }
879