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