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