cgfourteen.c revision 1.39 1 1.39 tsutsui /* $NetBSD: cgfourteen.c,v 1.39 2005/06/04 04:32:11 tsutsui Exp $ */
2 1.1 abrown
3 1.1 abrown /*
4 1.12 pk * 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.10 mrg
64 1.12 pk /*
65 1.1 abrown * The following is for debugging only; it opens up a security hole
66 1.12 pk * 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.12 pk * 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.35 lukem
80 1.35 lukem #include <sys/cdefs.h>
81 1.39 tsutsui __KERNEL_RCSID(0, "$NetBSD: cgfourteen.c,v 1.39 2005/06/04 04:32:11 tsutsui Exp $");
82 1.1 abrown
83 1.1 abrown #include <sys/param.h>
84 1.1 abrown #include <sys/systm.h>
85 1.1 abrown #include <sys/buf.h>
86 1.1 abrown #include <sys/device.h>
87 1.1 abrown #include <sys/ioctl.h>
88 1.1 abrown #include <sys/malloc.h>
89 1.1 abrown #include <sys/mman.h>
90 1.1 abrown #include <sys/tty.h>
91 1.1 abrown #include <sys/conf.h>
92 1.1 abrown
93 1.18 mrg #include <uvm/uvm_extern.h>
94 1.1 abrown
95 1.19 pk #include <machine/bus.h>
96 1.19 pk #include <machine/autoconf.h>
97 1.19 pk
98 1.19 pk #include <dev/sbus/sbusvar.h>
99 1.19 pk
100 1.20 pk #include <dev/sun/fbio.h>
101 1.20 pk #include <dev/sun/fbvar.h>
102 1.1 abrown
103 1.1 abrown #include <sparc/dev/cgfourteenreg.h>
104 1.1 abrown #include <sparc/dev/cgfourteenvar.h>
105 1.1 abrown
106 1.1 abrown /* autoconfiguration driver */
107 1.1 abrown static void cgfourteenattach(struct device *, struct device *, void *);
108 1.6 pk static int cgfourteenmatch(struct device *, struct cfdata *, void *);
109 1.1 abrown static void cgfourteenunblank(struct device *);
110 1.1 abrown
111 1.29 thorpej CFATTACH_DECL(cgfourteen, sizeof(struct cgfourteen_softc),
112 1.30 thorpej cgfourteenmatch, cgfourteenattach, NULL, NULL);
113 1.1 abrown
114 1.8 thorpej extern struct cfdriver cgfourteen_cd;
115 1.1 abrown
116 1.26 gehenna dev_type_open(cgfourteenopen);
117 1.26 gehenna dev_type_close(cgfourteenclose);
118 1.26 gehenna dev_type_ioctl(cgfourteenioctl);
119 1.26 gehenna dev_type_mmap(cgfourteenmmap);
120 1.26 gehenna
121 1.26 gehenna const struct cdevsw cgfourteen_cdevsw = {
122 1.26 gehenna cgfourteenopen, cgfourteenclose, noread, nowrite, cgfourteenioctl,
123 1.31 jdolecek nostop, notty, nopoll, cgfourteenmmap, nokqfilter,
124 1.26 gehenna };
125 1.26 gehenna
126 1.1 abrown /* frame buffer generic driver */
127 1.1 abrown static struct fbdriver cgfourteenfbdriver = {
128 1.12 pk cgfourteenunblank, cgfourteenopen, cgfourteenclose, cgfourteenioctl,
129 1.31 jdolecek nopoll, cgfourteenmmap, nokqfilter
130 1.1 abrown };
131 1.1 abrown
132 1.1 abrown static void cg14_set_video __P((struct cgfourteen_softc *, int));
133 1.1 abrown static int cg14_get_video __P((struct cgfourteen_softc *));
134 1.1 abrown static int cg14_get_cmap __P((struct fbcmap *, union cg14cmap *, int));
135 1.1 abrown static int cg14_put_cmap __P((struct fbcmap *, union cg14cmap *, int));
136 1.1 abrown static void cg14_load_hwcmap __P((struct cgfourteen_softc *, int, int));
137 1.1 abrown static void cg14_init __P((struct cgfourteen_softc *));
138 1.1 abrown static void cg14_reset __P((struct cgfourteen_softc *));
139 1.1 abrown static void cg14_loadomap __P((struct cgfourteen_softc *));/* cursor overlay */
140 1.1 abrown static void cg14_setcursor __P((struct cgfourteen_softc *));/* set position */
141 1.1 abrown static void cg14_loadcursor __P((struct cgfourteen_softc *));/* set shape */
142 1.1 abrown
143 1.1 abrown /*
144 1.23 darrenr * We map the display memory with an offset of 256K when emulating the cg3 or
145 1.23 darrenr * cg8; the cg3 uses this offset for compatibility with the cg4, and both the
146 1.23 darrenr * cg4 and cg8 have a mono overlay plane and an overlay enable plane in the
147 1.23 darrenr * first 256K. Mapping at an offset of 0x04000000 causes only the color
148 1.23 darrenr * frame buffer to be mapped, without the overlay planes.
149 1.23 darrenr */
150 1.23 darrenr #define START (128*1024 + 128*1024)
151 1.23 darrenr #define NOOVERLAY (0x04000000)
152 1.23 darrenr
153 1.23 darrenr /*
154 1.1 abrown * Match a cgfourteen.
155 1.1 abrown */
156 1.1 abrown int
157 1.6 pk cgfourteenmatch(parent, cf, aux)
158 1.1 abrown struct device *parent;
159 1.6 pk struct cfdata *cf;
160 1.6 pk void *aux;
161 1.1 abrown {
162 1.11 pk union obio_attach_args *uoba = aux;
163 1.11 pk struct sbus_attach_args *sa = &uoba->uoba_sbus;
164 1.1 abrown
165 1.1 abrown /*
166 1.1 abrown * The cgfourteen is a local-bus video adaptor, accessed directly
167 1.1 abrown * via the processor, and not through device space or an external
168 1.1 abrown * bus. Thus we look _only_ at the obio bus.
169 1.1 abrown * Additionally, these things exist only on the Sun4m.
170 1.1 abrown */
171 1.11 pk
172 1.11 pk if (uoba->uoba_isobio4 != 0 || !CPU_ISSUN4M)
173 1.11 pk return (0);
174 1.11 pk
175 1.11 pk /* Check driver name */
176 1.27 thorpej return (strcmp(cf->cf_name, sa->sa_name) == 0);
177 1.1 abrown }
178 1.1 abrown
179 1.1 abrown /*
180 1.1 abrown * Attach a display. We need to notice if it is the console, too.
181 1.1 abrown */
182 1.1 abrown void
183 1.11 pk cgfourteenattach(parent, self, aux)
184 1.1 abrown struct device *parent, *self;
185 1.11 pk void *aux;
186 1.1 abrown {
187 1.11 pk union obio_attach_args *uoba = aux;
188 1.11 pk struct sbus_attach_args *sa = &uoba->uoba_sbus;
189 1.11 pk struct cgfourteen_softc *sc = (struct cgfourteen_softc *)self;
190 1.11 pk struct fbdevice *fb = &sc->sc_fb;
191 1.11 pk bus_space_handle_t bh;
192 1.11 pk int node, ramsize;
193 1.39 tsutsui volatile u_int32_t *lut;
194 1.1 abrown int i, isconsole;
195 1.11 pk
196 1.11 pk node = sa->sa_node;
197 1.11 pk
198 1.11 pk /* Remember cookies for cgfourteenmmap() */
199 1.11 pk sc->sc_bustag = sa->sa_bustag;
200 1.1 abrown
201 1.11 pk fb->fb_driver = &cgfourteenfbdriver;
202 1.11 pk fb->fb_device = &sc->sc_dev;
203 1.11 pk /* Mask out invalid flags from the user. */
204 1.11 pk fb->fb_flags = sc->sc_dev.dv_cfdata->cf_flags & FB_USERMASK;
205 1.1 abrown
206 1.1 abrown /*
207 1.1 abrown * We're emulating a cg3/8, so represent ourselves as one
208 1.1 abrown */
209 1.1 abrown #ifdef CG14_CG8
210 1.11 pk fb->fb_type.fb_type = FBTYPE_MEMCOLOR;
211 1.11 pk fb->fb_type.fb_depth = 32;
212 1.1 abrown #else
213 1.11 pk fb->fb_type.fb_type = FBTYPE_SUN3COLOR;
214 1.11 pk fb->fb_type.fb_depth = 8;
215 1.1 abrown #endif
216 1.11 pk fb_setsize_obp(fb, sc->sc_fb.fb_type.fb_depth, 1152, 900, node);
217 1.23 darrenr #ifdef CG14_CG8
218 1.23 darrenr /*
219 1.23 darrenr * fb_setsize_obp set fb->fb_linebytes based on the current
220 1.23 darrenr * depth reported by obp, but that defaults to 8 bits (as
221 1.23 darrenr * reported by getpropint(). Update the value to reflect
222 1.23 darrenr * the depth that will be used after open.
223 1.23 darrenr * The display memory size returned by the cg8 driver includes
224 1.23 darrenr * the space used by the overlay planes, but the size returned
225 1.23 darrenr * by the cg3 driver does not; emulate the other drivers.
226 1.23 darrenr */
227 1.23 darrenr fb->fb_linebytes = (fb->fb_type.fb_width * fb->fb_type.fb_depth) / 8;
228 1.23 darrenr ramsize = roundup(START + (fb->fb_type.fb_height * fb->fb_linebytes),
229 1.32 thorpej PAGE_SIZE);
230 1.23 darrenr #else
231 1.32 thorpej ramsize = roundup(fb->fb_type.fb_height * fb->fb_linebytes, PAGE_SIZE);
232 1.23 darrenr #endif
233 1.11 pk fb->fb_type.fb_cmsize = CG14_CLUT_SIZE;
234 1.11 pk fb->fb_type.fb_size = ramsize;
235 1.11 pk
236 1.13 pk if (sa->sa_nreg < 2) {
237 1.13 pk printf("%s: only %d register sets\n",
238 1.13 pk self->dv_xname, sa->sa_nreg);
239 1.11 pk return;
240 1.11 pk }
241 1.13 pk bcopy(sa->sa_reg, sc->sc_physadr,
242 1.25 thorpej sa->sa_nreg * sizeof(struct openprom_addr));
243 1.1 abrown
244 1.1 abrown /*
245 1.1 abrown * Now map in the 8 useful pages of registers
246 1.1 abrown */
247 1.11 pk if (sa->sa_size < 0x10000) {
248 1.1 abrown #ifdef DIAGNOSTIC
249 1.5 christos printf("warning: can't find all cgfourteen registers...\n");
250 1.1 abrown #endif
251 1.11 pk sa->sa_size = 0x10000;
252 1.1 abrown }
253 1.22 pk if (sbus_bus_map(sa->sa_bustag,
254 1.22 pk sa->sa_slot, sa->sa_offset, sa->sa_size,
255 1.22 pk BUS_SPACE_MAP_LINEAR, &bh) != 0) {
256 1.11 pk printf("%s: cannot map control registers\n", self->dv_xname);
257 1.11 pk return;
258 1.11 pk }
259 1.11 pk
260 1.11 pk sc->sc_ctl = (struct cg14ctl *) (bh);
261 1.11 pk sc->sc_hwc = (struct cg14curs *) (bh + CG14_OFFSET_CURS);
262 1.11 pk sc->sc_dac = (struct cg14dac *) (bh + CG14_OFFSET_DAC);
263 1.11 pk sc->sc_xlut = (struct cg14xlut *) (bh + CG14_OFFSET_XLUT);
264 1.11 pk sc->sc_clut1 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT1);
265 1.11 pk sc->sc_clut2 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT2);
266 1.11 pk sc->sc_clut3 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT3);
267 1.11 pk sc->sc_clutincr = (u_int *) (bh + CG14_OFFSET_CLUTINCR);
268 1.1 abrown
269 1.1 abrown /*
270 1.1 abrown * Let the user know that we're here
271 1.1 abrown */
272 1.1 abrown #ifdef CG14_CG8
273 1.5 christos printf(": cgeight emulated at %dx%dx24bpp",
274 1.11 pk fb->fb_type.fb_width, fb->fb_type.fb_height);
275 1.1 abrown #else
276 1.5 christos printf(": cgthree emulated at %dx%dx8bpp",
277 1.11 pk fb->fb_type.fb_width, fb->fb_type.fb_height);
278 1.1 abrown #endif
279 1.1 abrown /*
280 1.1 abrown * Enable the video, but don't change the pixel depth.
281 1.1 abrown */
282 1.1 abrown cg14_set_video(sc, 1);
283 1.1 abrown
284 1.1 abrown /*
285 1.1 abrown * Grab the initial colormap
286 1.1 abrown */
287 1.39 tsutsui lut = sc->sc_clut1->clut_lut;
288 1.1 abrown for (i = 0; i < CG14_CLUT_SIZE; i++)
289 1.1 abrown sc->sc_cmap.cm_chip[i] = lut[i];
290 1.1 abrown
291 1.1 abrown /* See if we're the console */
292 1.16 pk isconsole = fb_is_console(node);
293 1.1 abrown
294 1.1 abrown if (isconsole) {
295 1.5 christos printf(" (console)\n");
296 1.1 abrown #ifdef notdef
297 1.11 pk /*
298 1.11 pk * We don't use the raster console since the cg14 is
299 1.11 pk * fast enough already.
300 1.11 pk */
301 1.1 abrown #ifdef RASTERCONSOLE
302 1.11 pk fbrcons_init(fb);
303 1.1 abrown #endif
304 1.1 abrown #endif /* notdef */
305 1.1 abrown } else
306 1.5 christos printf("\n");
307 1.1 abrown
308 1.1 abrown /* Attach to /dev/fb */
309 1.16 pk fb_attach(&sc->sc_fb, isconsole);
310 1.1 abrown }
311 1.1 abrown
312 1.1 abrown /*
313 1.12 pk * Keep track of the number of opens made. In the 24-bit driver, we need to
314 1.1 abrown * switch to 24-bit mode on the first open, and switch back to 8-bit on
315 1.1 abrown * the last close. This kind of nonsense is needed to give screenblank
316 1.1 abrown * a fighting chance of working.
317 1.1 abrown */
318 1.1 abrown static int cg14_opens = 0;
319 1.1 abrown
320 1.1 abrown int
321 1.34 fvdl cgfourteenopen(dev, flags, mode, p)
322 1.1 abrown dev_t dev;
323 1.1 abrown int flags, mode;
324 1.34 fvdl struct proc *p;
325 1.1 abrown {
326 1.11 pk struct cgfourteen_softc *sc = cgfourteen_cd.cd_devs[minor(dev)];
327 1.1 abrown int unit = minor(dev);
328 1.1 abrown int s, oldopens;
329 1.1 abrown
330 1.12 pk if (unit >= cgfourteen_cd.cd_ndevs ||
331 1.1 abrown cgfourteen_cd.cd_devs[unit] == NULL)
332 1.1 abrown return (ENXIO);
333 1.1 abrown
334 1.1 abrown s = splhigh();
335 1.1 abrown oldopens = cg14_opens++;
336 1.1 abrown splx(s);
337 1.1 abrown
338 1.1 abrown /* Setup the cg14 as we want it, and save the original PROM state */
339 1.1 abrown if (oldopens == 0) /* first open only, to make screenblank work */
340 1.1 abrown cg14_init(sc);
341 1.1 abrown
342 1.1 abrown return (0);
343 1.1 abrown }
344 1.1 abrown
345 1.1 abrown int
346 1.34 fvdl cgfourteenclose(dev, flags, mode, p)
347 1.1 abrown dev_t dev;
348 1.1 abrown int flags, mode;
349 1.34 fvdl struct proc *p;
350 1.1 abrown {
351 1.11 pk struct cgfourteen_softc *sc = cgfourteen_cd.cd_devs[minor(dev)];
352 1.1 abrown int s, opens;
353 1.1 abrown
354 1.1 abrown s = splhigh();
355 1.1 abrown opens = --cg14_opens;
356 1.1 abrown if (cg14_opens < 0)
357 1.1 abrown opens = cg14_opens = 0;
358 1.1 abrown splx(s);
359 1.1 abrown
360 1.1 abrown /*
361 1.1 abrown * Restore video state to make the PROM happy, on last close.
362 1.1 abrown */
363 1.1 abrown if (opens == 0)
364 1.1 abrown cg14_reset(sc);
365 1.1 abrown
366 1.1 abrown return (0);
367 1.1 abrown }
368 1.1 abrown
369 1.1 abrown int
370 1.34 fvdl cgfourteenioctl(dev, cmd, data, flags, p)
371 1.1 abrown dev_t dev;
372 1.1 abrown u_long cmd;
373 1.11 pk caddr_t data;
374 1.1 abrown int flags;
375 1.34 fvdl struct proc *p;
376 1.1 abrown {
377 1.11 pk struct cgfourteen_softc *sc = cgfourteen_cd.cd_devs[minor(dev)];
378 1.11 pk struct fbgattr *fba;
379 1.1 abrown union cg14cursor_cmap tcm;
380 1.1 abrown int v, error;
381 1.1 abrown u_int count;
382 1.38 chs u_int eplane[32], cplane[32];
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.12 pk 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.38 chs error = copyout(cc->cc_cplane, p->image, count);
444 1.1 abrown if (error)
445 1.1 abrown return (error);
446 1.38 chs error = copyout(cc->cc_eplane, p->mask, count);
447 1.1 abrown if (error)
448 1.1 abrown return (error);
449 1.1 abrown }
450 1.1 abrown if (p->cmap.red != NULL) {
451 1.1 abrown error = cg14_get_cmap(&p->cmap,
452 1.1 abrown (union cg14cmap *)&cc->cc_color, 2);
453 1.1 abrown if (error)
454 1.1 abrown return (error);
455 1.1 abrown } else {
456 1.1 abrown p->cmap.index = 0;
457 1.1 abrown p->cmap.count = 2;
458 1.1 abrown }
459 1.1 abrown /* end ugh */
460 1.1 abrown break;
461 1.1 abrown
462 1.1 abrown case FBIOSCURSOR:
463 1.1 abrown /*
464 1.1 abrown * For setcmap and setshape, verify parameters, so that
465 1.1 abrown * we do not get halfway through an update and then crap
466 1.1 abrown * out with the software state screwed up.
467 1.1 abrown */
468 1.1 abrown v = p->set;
469 1.1 abrown if (v & FB_CUR_SETCMAP) {
470 1.1 abrown /*
471 1.1 abrown * This use of a temporary copy of the cursor
472 1.1 abrown * colormap is not terribly efficient, but these
473 1.1 abrown * copies are small (8 bytes)...
474 1.1 abrown */
475 1.1 abrown tcm = cc->cc_color;
476 1.1 abrown error = cg14_put_cmap(&p->cmap, (union cg14cmap *)&tcm,
477 1.1 abrown 2);
478 1.1 abrown if (error)
479 1.1 abrown return (error);
480 1.1 abrown }
481 1.1 abrown if (v & FB_CUR_SETSHAPE) {
482 1.1 abrown if ((u_int)p->size.x > 32 || (u_int)p->size.y > 32)
483 1.1 abrown return (EINVAL);
484 1.1 abrown count = p->size.y * 32 / NBBY;
485 1.38 chs error = copyin(p->mask, eplane, count);
486 1.38 chs if (error)
487 1.38 chs return error;
488 1.38 chs error = copyin(p->image, cplane, count);
489 1.38 chs if (error)
490 1.38 chs return error;
491 1.1 abrown }
492 1.1 abrown
493 1.1 abrown /* parameters are OK; do it */
494 1.1 abrown if (v & (FB_CUR_SETCUR | FB_CUR_SETPOS | FB_CUR_SETHOT)) {
495 1.1 abrown if (v & FB_CUR_SETCUR)
496 1.1 abrown cc->cc_enable = p->enable;
497 1.1 abrown if (v & FB_CUR_SETPOS)
498 1.1 abrown cc->cc_pos = p->pos;
499 1.1 abrown if (v & FB_CUR_SETHOT)
500 1.1 abrown cc->cc_hot = p->hot;
501 1.1 abrown cg14_setcursor(sc);
502 1.1 abrown }
503 1.1 abrown if (v & FB_CUR_SETCMAP) {
504 1.1 abrown cc->cc_color = tcm;
505 1.1 abrown cg14_loadomap(sc); /* XXX defer to vertical retrace */
506 1.1 abrown }
507 1.1 abrown if (v & FB_CUR_SETSHAPE) {
508 1.1 abrown cc->cc_size = p->size;
509 1.1 abrown count = p->size.y * 32 / NBBY;
510 1.38 chs memset(cc->cc_eplane, 0, sizeof cc->cc_eplane);
511 1.38 chs memcpy(cc->cc_eplane, eplane, count);
512 1.38 chs memset(cc->cc_cplane, 0, sizeof cc->cc_cplane);
513 1.38 chs memcpy(cc->cc_cplane, cplane, count);
514 1.1 abrown cg14_loadcursor(sc);
515 1.1 abrown }
516 1.1 abrown break;
517 1.1 abrown
518 1.1 abrown #undef cc
519 1.1 abrown #undef p
520 1.1 abrown case FBIOGCURPOS:
521 1.1 abrown *(struct fbcurpos *)data = sc->sc_cursor.cc_pos;
522 1.1 abrown break;
523 1.1 abrown
524 1.1 abrown case FBIOSCURPOS:
525 1.1 abrown sc->sc_cursor.cc_pos = *(struct fbcurpos *)data;
526 1.1 abrown cg14_setcursor(sc);
527 1.1 abrown break;
528 1.1 abrown
529 1.1 abrown case FBIOGCURMAX:
530 1.1 abrown /* max cursor size is 32x32 */
531 1.1 abrown ((struct fbcurpos *)data)->x = 32;
532 1.1 abrown ((struct fbcurpos *)data)->y = 32;
533 1.1 abrown break;
534 1.1 abrown
535 1.1 abrown default:
536 1.1 abrown return (ENOTTY);
537 1.1 abrown }
538 1.1 abrown return (0);
539 1.1 abrown }
540 1.1 abrown
541 1.1 abrown /*
542 1.1 abrown * Undo the effect of an FBIOSVIDEO that turns the video off.
543 1.1 abrown */
544 1.1 abrown static void
545 1.1 abrown cgfourteenunblank(dev)
546 1.1 abrown struct device *dev;
547 1.1 abrown {
548 1.1 abrown
549 1.1 abrown cg14_set_video((struct cgfourteen_softc *)dev, 1);
550 1.1 abrown }
551 1.1 abrown
552 1.1 abrown /*
553 1.1 abrown * Return the address that would map the given device at the given
554 1.1 abrown * offset, allowing for the given protection, or return -1 for error.
555 1.1 abrown *
556 1.1 abrown * The cg14 frame buffer can be mapped in either 8-bit or 32-bit mode
557 1.1 abrown * starting at the address stored in the PROM. In 8-bit mode, the X
558 1.1 abrown * channel is not present, and can be ignored. In 32-bit mode, mapping
559 1.1 abrown * at 0K delivers a 32-bpp buffer where the upper 8 bits select the X
560 1.1 abrown * channel information. We hardwire the Xlut to all zeroes to insure
561 1.1 abrown * that, regardless of this value, direct 24-bit color access will be
562 1.1 abrown * used.
563 1.12 pk *
564 1.12 pk * Alternatively, mapping the frame buffer at an offset of 16M seems to
565 1.1 abrown * tell the chip to ignore the X channel. XXX where does it get the X value
566 1.1 abrown * to use?
567 1.1 abrown */
568 1.17 simonb paddr_t
569 1.1 abrown cgfourteenmmap(dev, off, prot)
570 1.1 abrown dev_t dev;
571 1.17 simonb off_t off;
572 1.17 simonb int prot;
573 1.1 abrown {
574 1.11 pk struct cgfourteen_softc *sc = cgfourteen_cd.cd_devs[minor(dev)];
575 1.12 pk
576 1.1 abrown if (off & PGOFSET)
577 1.1 abrown panic("cgfourteenmmap");
578 1.14 mrg
579 1.14 mrg if (off < 0)
580 1.14 mrg return (-1);
581 1.1 abrown
582 1.1 abrown #if defined(DEBUG) && defined(CG14_MAP_REGS) /* XXX: security hole */
583 1.1 abrown /*
584 1.12 pk * Map the control registers into user space. Should only be
585 1.1 abrown * used for debugging!
586 1.1 abrown */
587 1.1 abrown if ((u_int)off >= 0x10000000 && (u_int)off < 0x10000000 + 16*4096) {
588 1.1 abrown off -= 0x10000000;
589 1.12 pk if (bus_space_mmap(sc->sc_bustag,
590 1.21 eeh BUS_ADDR(sc->sc_physadr[CG14_CTL_IDX].sbr_slot,
591 1.21 eeh sc->sc_physadr[CG14_CTL_IDX].sbr_offset),
592 1.21 eeh off, prot, BUS_SPACE_MAP_LINEAR));
593 1.1 abrown }
594 1.1 abrown #endif
595 1.1 abrown
596 1.1 abrown if ((u_int)off >= NOOVERLAY)
597 1.1 abrown off -= NOOVERLAY;
598 1.23 darrenr else if ((u_int)off >= START)
599 1.23 darrenr off -= START;
600 1.23 darrenr else
601 1.23 darrenr off = 0;
602 1.23 darrenr
603 1.23 darrenr /*
604 1.23 darrenr * fb_size includes the overlay space only for the CG8.
605 1.23 darrenr */
606 1.1 abrown #ifdef CG14_CG8
607 1.23 darrenr if (off >= sc->sc_fb.fb_type.fb_size - START)
608 1.1 abrown #else
609 1.23 darrenr if (off >= sc->sc_fb.fb_type.fb_size)
610 1.1 abrown #endif
611 1.23 darrenr {
612 1.1 abrown #ifdef DEBUG
613 1.5 christos printf("\nmmap request out of bounds: request 0x%x, "
614 1.12 pk "bound 0x%x\n", (unsigned) off,
615 1.4 christos (unsigned)sc->sc_fb.fb_type.fb_size);
616 1.1 abrown #endif
617 1.1 abrown return (-1);
618 1.1 abrown }
619 1.1 abrown
620 1.21 eeh return (bus_space_mmap(sc->sc_bustag,
621 1.25 thorpej BUS_ADDR(sc->sc_physadr[CG14_PXL_IDX].oa_space,
622 1.25 thorpej sc->sc_physadr[CG14_PXL_IDX].oa_base),
623 1.21 eeh off, prot, BUS_SPACE_MAP_LINEAR));
624 1.12 pk }
625 1.1 abrown
626 1.1 abrown /*
627 1.12 pk * Miscellaneous helper functions
628 1.1 abrown */
629 1.1 abrown
630 1.1 abrown /* Initialize the framebuffer, storing away useful state for later reset */
631 1.12 pk static void
632 1.1 abrown cg14_init(sc)
633 1.1 abrown struct cgfourteen_softc *sc;
634 1.1 abrown {
635 1.39 tsutsui volatile u_int32_t *clut;
636 1.39 tsutsui volatile u_int8_t *xlut;
637 1.11 pk int i;
638 1.1 abrown
639 1.1 abrown /*
640 1.1 abrown * We stash away the following to restore on close:
641 1.1 abrown *
642 1.1 abrown * color look-up table 1 (sc->sc_saveclut)
643 1.1 abrown * x look-up table (sc->sc_savexlut)
644 1.1 abrown * control register (sc->sc_savectl)
645 1.1 abrown * cursor control register (sc->sc_savehwc)
646 1.1 abrown */
647 1.1 abrown sc->sc_savectl = sc->sc_ctl->ctl_mctl;
648 1.1 abrown sc->sc_savehwc = sc->sc_hwc->curs_ctl;
649 1.1 abrown
650 1.39 tsutsui clut = sc->sc_clut1->clut_lut;
651 1.39 tsutsui xlut = sc->sc_xlut->xlut_lut;
652 1.1 abrown for (i = 0; i < CG14_CLUT_SIZE; i++) {
653 1.1 abrown sc->sc_saveclut.cm_chip[i] = clut[i];
654 1.1 abrown sc->sc_savexlut[i] = xlut[i];
655 1.1 abrown }
656 1.1 abrown
657 1.1 abrown #ifdef CG14_CG8
658 1.1 abrown /*
659 1.1 abrown * Enable the video, and put in 24 bit mode.
660 1.1 abrown */
661 1.1 abrown sc->sc_ctl->ctl_mctl = CG14_MCTL_ENABLEVID | CG14_MCTL_PIXMODE_32 |
662 1.1 abrown CG14_MCTL_POWERCTL;
663 1.1 abrown
664 1.12 pk /*
665 1.1 abrown * Zero the xlut to enable direct-color mode
666 1.1 abrown */
667 1.38 chs memset(sc->sc_xlut, 0, CG14_CLUT_SIZE);
668 1.1 abrown #else
669 1.1 abrown /*
670 1.1 abrown * Enable the video and put it in 8 bit mode
671 1.1 abrown */
672 1.1 abrown sc->sc_ctl->ctl_mctl = CG14_MCTL_ENABLEVID | CG14_MCTL_PIXMODE_8 |
673 1.1 abrown CG14_MCTL_POWERCTL;
674 1.1 abrown #endif
675 1.1 abrown }
676 1.1 abrown
677 1.1 abrown static void
678 1.1 abrown cg14_reset(sc) /* Restore the state saved on cg14_init */
679 1.1 abrown struct cgfourteen_softc *sc;
680 1.1 abrown {
681 1.39 tsutsui volatile u_int32_t *clut;
682 1.39 tsutsui volatile u_int8_t *xlut;
683 1.11 pk int i;
684 1.1 abrown
685 1.1 abrown /*
686 1.1 abrown * We restore the following, saved in cg14_init:
687 1.1 abrown *
688 1.1 abrown * color look-up table 1 (sc->sc_saveclut)
689 1.1 abrown * x look-up table (sc->sc_savexlut)
690 1.1 abrown * control register (sc->sc_savectl)
691 1.1 abrown * cursor control register (sc->sc_savehwc)
692 1.1 abrown *
693 1.1 abrown * Note that we don't touch the video enable bits in the
694 1.1 abrown * control register; otherwise, screenblank wouldn't work.
695 1.1 abrown */
696 1.1 abrown sc->sc_ctl->ctl_mctl = (sc->sc_ctl->ctl_mctl & (CG14_MCTL_ENABLEVID |
697 1.1 abrown CG14_MCTL_POWERCTL)) |
698 1.1 abrown (sc->sc_savectl & ~(CG14_MCTL_ENABLEVID |
699 1.1 abrown CG14_MCTL_POWERCTL));
700 1.1 abrown sc->sc_hwc->curs_ctl = sc->sc_savehwc;
701 1.1 abrown
702 1.39 tsutsui clut = sc->sc_clut1->clut_lut;
703 1.39 tsutsui xlut = sc->sc_xlut->xlut_lut;
704 1.1 abrown for (i = 0; i < CG14_CLUT_SIZE; i++) {
705 1.1 abrown clut[i] = sc->sc_saveclut.cm_chip[i];
706 1.1 abrown xlut[i] = sc->sc_savexlut[i];
707 1.1 abrown }
708 1.1 abrown }
709 1.1 abrown
710 1.1 abrown /* Enable/disable video display; power down monitor if DPMS-capable */
711 1.1 abrown static void
712 1.1 abrown cg14_set_video(sc, enable)
713 1.1 abrown struct cgfourteen_softc *sc;
714 1.1 abrown int enable;
715 1.1 abrown {
716 1.12 pk /*
717 1.1 abrown * We can only use DPMS to power down the display if the chip revision
718 1.1 abrown * is greater than 0.
719 1.1 abrown */
720 1.1 abrown if (enable) {
721 1.1 abrown if ((sc->sc_ctl->ctl_rsr & CG14_RSR_REVMASK) > 0)
722 1.1 abrown sc->sc_ctl->ctl_mctl |= (CG14_MCTL_ENABLEVID |
723 1.1 abrown CG14_MCTL_POWERCTL);
724 1.1 abrown else
725 1.1 abrown sc->sc_ctl->ctl_mctl |= CG14_MCTL_ENABLEVID;
726 1.1 abrown } else {
727 1.1 abrown if ((sc->sc_ctl->ctl_rsr & CG14_RSR_REVMASK) > 0)
728 1.1 abrown sc->sc_ctl->ctl_mctl &= ~(CG14_MCTL_ENABLEVID |
729 1.1 abrown CG14_MCTL_POWERCTL);
730 1.1 abrown else
731 1.1 abrown sc->sc_ctl->ctl_mctl &= ~CG14_MCTL_ENABLEVID;
732 1.1 abrown }
733 1.1 abrown }
734 1.1 abrown
735 1.1 abrown /* Get status of video display */
736 1.1 abrown static int
737 1.1 abrown cg14_get_video(sc)
738 1.1 abrown struct cgfourteen_softc *sc;
739 1.1 abrown {
740 1.1 abrown return ((sc->sc_ctl->ctl_mctl & CG14_MCTL_ENABLEVID) != 0);
741 1.1 abrown }
742 1.1 abrown
743 1.1 abrown /* Read the software shadow colormap */
744 1.12 pk static int
745 1.1 abrown cg14_get_cmap(p, cm, cmsize)
746 1.11 pk struct fbcmap *p;
747 1.1 abrown union cg14cmap *cm;
748 1.1 abrown int cmsize;
749 1.1 abrown {
750 1.24 itojun u_int i, start, count;
751 1.24 itojun u_char *cp;
752 1.38 chs int error;
753 1.12 pk
754 1.24 itojun start = p->index;
755 1.24 itojun count = p->count;
756 1.24 itojun if (start >= cmsize || count > cmsize - start)
757 1.24 itojun return (EINVAL);
758 1.1 abrown
759 1.24 itojun for (cp = &cm->cm_map[start][0], i = 0; i < count; cp += 4, i++) {
760 1.38 chs error = copyout(&cp[3], &p->red[i], 1);
761 1.38 chs if (error)
762 1.38 chs return error;
763 1.38 chs error = copyout(&cp[2], &p->green[i], 1);
764 1.38 chs if (error)
765 1.38 chs return error;
766 1.38 chs error = copyout(&cp[1], &p->blue[i], 1);
767 1.38 chs if (error)
768 1.38 chs return error;
769 1.24 itojun }
770 1.24 itojun return (0);
771 1.1 abrown }
772 1.1 abrown
773 1.1 abrown /* Write the software shadow colormap */
774 1.1 abrown static int
775 1.1 abrown cg14_put_cmap(p, cm, cmsize)
776 1.24 itojun struct fbcmap *p;
777 1.24 itojun union cg14cmap *cm;
778 1.24 itojun int cmsize;
779 1.24 itojun {
780 1.24 itojun u_int i, start, count;
781 1.24 itojun u_char *cp;
782 1.38 chs u_char cmap[256][4];
783 1.38 chs int error;
784 1.24 itojun
785 1.24 itojun start = p->index;
786 1.24 itojun count = p->count;
787 1.24 itojun if (start >= cmsize || count > cmsize - start)
788 1.24 itojun return (EINVAL);
789 1.1 abrown
790 1.38 chs memcpy(&cmap, &cm->cm_map, sizeof cmap);
791 1.38 chs for (cp = &cmap[start][0], i = 0; i < count; cp += 4, i++) {
792 1.38 chs error = copyin(&p->red[i], &cp[3], 1);
793 1.38 chs if (error)
794 1.38 chs return error;
795 1.38 chs error = copyin(&p->green[i], &cp[2], 1);
796 1.38 chs if (error)
797 1.38 chs return error;
798 1.38 chs error = copyin(&p->blue[i], &cp[1], 1);
799 1.38 chs if (error)
800 1.38 chs return error;
801 1.1 abrown cp[0] = 0; /* no alpha channel */
802 1.24 itojun }
803 1.38 chs memcpy(&cm->cm_map, &cmap, sizeof cmap);
804 1.24 itojun return (0);
805 1.1 abrown }
806 1.1 abrown
807 1.1 abrown static void
808 1.1 abrown cg14_load_hwcmap(sc, start, ncolors)
809 1.11 pk struct cgfourteen_softc *sc;
810 1.11 pk int start, ncolors;
811 1.1 abrown {
812 1.1 abrown /* XXX switch to auto-increment, and on retrace intr */
813 1.1 abrown
814 1.1 abrown /* Setup pointers to source and dest */
815 1.11 pk u_int32_t *colp = &sc->sc_cmap.cm_chip[start];
816 1.11 pk volatile u_int32_t *lutp = &sc->sc_clut1->clut_lut[start];
817 1.1 abrown
818 1.1 abrown /* Copy by words */
819 1.1 abrown while (--ncolors >= 0)
820 1.1 abrown *lutp++ = *colp++;
821 1.1 abrown }
822 1.1 abrown
823 1.1 abrown /*
824 1.1 abrown * Load the cursor (overlay `foreground' and `background') colors.
825 1.1 abrown */
826 1.1 abrown static void
827 1.1 abrown cg14_setcursor(sc)
828 1.11 pk struct cgfourteen_softc *sc;
829 1.1 abrown {
830 1.1 abrown /* we need to subtract the hot-spot value here */
831 1.1 abrown #define COORD(f) (sc->sc_cursor.cc_pos.f - sc->sc_cursor.cc_hot.f)
832 1.1 abrown
833 1.1 abrown sc->sc_hwc->curs_ctl = (sc->sc_cursor.cc_enable ? CG14_CURS_ENABLE : 0);
834 1.1 abrown sc->sc_hwc->curs_x = COORD(x);
835 1.1 abrown sc->sc_hwc->curs_y = COORD(y);
836 1.1 abrown
837 1.1 abrown #undef COORD
838 1.1 abrown }
839 1.1 abrown
840 1.1 abrown static void
841 1.1 abrown cg14_loadcursor(sc)
842 1.11 pk struct cgfourteen_softc *sc;
843 1.1 abrown {
844 1.11 pk volatile struct cg14curs *hwc;
845 1.11 pk u_int edgemask, m;
846 1.11 pk int i;
847 1.1 abrown
848 1.1 abrown /*
849 1.1 abrown * Keep the top size.x bits. Here we *throw out* the top
850 1.1 abrown * size.x bits from an all-one-bits word, introducing zeros in
851 1.1 abrown * the top size.x bits, then invert all the bits to get what
852 1.1 abrown * we really wanted as our mask. But this fails if size.x is
853 1.1 abrown * 32---a sparc uses only the low 5 bits of the shift count---
854 1.1 abrown * so we have to special case that.
855 1.1 abrown */
856 1.1 abrown edgemask = ~0;
857 1.1 abrown if (sc->sc_cursor.cc_size.x < 32)
858 1.1 abrown edgemask = ~(edgemask >> sc->sc_cursor.cc_size.x);
859 1.1 abrown hwc = sc->sc_hwc;
860 1.1 abrown for (i = 0; i < 32; i++) {
861 1.1 abrown m = sc->sc_cursor.cc_eplane[i] & edgemask;
862 1.1 abrown hwc->curs_plane0[i] = m;
863 1.1 abrown hwc->curs_plane1[i] = m & sc->sc_cursor.cc_cplane[i];
864 1.1 abrown }
865 1.1 abrown }
866 1.1 abrown
867 1.1 abrown static void
868 1.1 abrown cg14_loadomap(sc)
869 1.11 pk struct cgfourteen_softc *sc;
870 1.1 abrown {
871 1.1 abrown /* set background color */
872 1.1 abrown sc->sc_hwc->curs_color1 = sc->sc_cursor.cc_color.cm_chip[0];
873 1.1 abrown /* set foreground color */
874 1.1 abrown sc->sc_hwc->curs_color2 = sc->sc_cursor.cc_color.cm_chip[1];
875 1.1 abrown }
876