cgfourteen.c revision 1.48 1 /* $NetBSD: cgfourteen.c,v 1.48 2007/03/04 06:00:43 christos 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 #define CG14_CG8
77
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/buf.h>
81 #include <sys/device.h>
82 #include <sys/ioctl.h>
83 #include <sys/malloc.h>
84 #include <sys/mman.h>
85 #include <sys/tty.h>
86 #include <sys/conf.h>
87
88 #include <uvm/uvm_extern.h>
89
90 #include <dev/sun/fbio.h>
91 #include <machine/autoconf.h>
92 #include <machine/pmap.h>
93 #include <dev/sun/fbvar.h>
94 #include <machine/cpu.h>
95 #include <dev/sbus/sbusvar.h>
96
97 #include <sparc/dev/cgfourteenreg.h>
98 #include <sparc/dev/cgfourteenvar.h>
99
100 /* autoconfiguration driver */
101 static int cgfourteenmatch(struct device *, struct cfdata *, void *);
102 static void cgfourteenattach(struct device *, struct device *, void *);
103 static void cgfourteenunblank(struct device *);
104
105 CFATTACH_DECL(cgfourteen, sizeof(struct cgfourteen_softc),
106 cgfourteenmatch, cgfourteenattach, NULL, NULL);
107
108 extern struct cfdriver cgfourteen_cd;
109
110 dev_type_open(cgfourteenopen);
111 dev_type_close(cgfourteenclose);
112 dev_type_ioctl(cgfourteenioctl);
113 dev_type_mmap(cgfourteenmmap);
114 dev_type_poll(cgfourteenpoll);
115
116 const struct cdevsw cgfourteen_cdevsw = {
117 cgfourteenopen, cgfourteenclose, noread, nowrite, cgfourteenioctl,
118 nostop, notty, cgfourteenpoll, cgfourteenmmap, nokqfilter,
119 };
120
121 /* frame buffer generic driver */
122 static struct fbdriver cgfourteenfbdriver = {
123 cgfourteenunblank, cgfourteenopen, cgfourteenclose, cgfourteenioctl,
124 cgfourteenpoll, cgfourteenmmap, nokqfilter
125 };
126
127 extern struct tty *fbconstty;
128
129 static void cg14_set_video(struct cgfourteen_softc *, int);
130 static int cg14_get_video(struct cgfourteen_softc *);
131 static int cg14_get_cmap(struct fbcmap *, union cg14cmap *, int);
132 static int cg14_put_cmap(struct fbcmap *, union cg14cmap *, int);
133 static void cg14_load_hwcmap(struct cgfourteen_softc *, int, int);
134 static void cg14_init(struct cgfourteen_softc *);
135 static void cg14_reset(struct cgfourteen_softc *);
136
137 /*
138 * Match a cgfourteen.
139 */
140 int
141 cgfourteenmatch(struct device *parent, struct cfdata *cf, void *aux)
142 {
143 union obio_attach_args *uoba = aux;
144 struct sbus_attach_args *sa = &uoba->uoba_sbus;
145
146 /*
147 * The cgfourteen is a local-bus video adaptor, accessed directly
148 * via the processor, and not through device space or an external
149 * bus. Thus we look _only_ at the obio bus.
150 * Additionally, these things exist only on the Sun4m.
151 */
152
153 if (uoba->uoba_isobio4 != 0 || !CPU_ISSUN4M)
154 return (0);
155
156 /* Check driver name */
157 return (strcmp(cf->cf_name, sa->sa_name) == 0);
158 }
159
160 /*
161 * Set COLOUR_OFFSET to the offset of the video RAM. This is to provide
162 * space for faked overlay junk for the cg8 emulation.
163 *
164 * As it happens, this value is correct for both cg3 and cg8 emulation!
165 */
166 #define COLOUR_OFFSET (256*1024)
167
168 #ifdef RASTERCONSOLE
169 static void cg14_set_rcons_luts(struct cgfourteen_softc *sc)
170 {
171 int i;
172
173 for (i=0;i<CG14_CLUT_SIZE;i++) sc->sc_xlut->xlut_lut[i] = 0x22;
174 for (i=0;i<CG14_CLUT_SIZE;i++) sc->sc_clut2->clut_lut[i] = 0x00ffffff;
175 sc->sc_clut2->clut_lut[0] = 0x00ffffff;
176 sc->sc_clut2->clut_lut[255] = 0;
177 }
178 #endif /* RASTERCONSOLE */
179
180 /*
181 * Attach a display. We need to notice if it is the console, too.
182 */
183 void
184 cgfourteenattach(struct device *parent, struct device *self, void *aux)
185 {
186 union obio_attach_args *uoba = aux;
187 struct sbus_attach_args *sa = &uoba->uoba_sbus;
188 struct cgfourteen_softc *sc = (struct cgfourteen_softc *)self;
189 struct fbdevice *fb = &sc->sc_fb;
190 bus_space_handle_t bh;
191 int node, ramsize;
192 volatile uint32_t *lut;
193 int i, isconsole;
194
195 node = sa->sa_node;
196
197 /* Remember cookies for cgfourteenmmap() */
198 sc->sc_bustag = sa->sa_bustag;
199
200 fb->fb_driver = &cgfourteenfbdriver;
201 fb->fb_device = &sc->sc_dev;
202 /* Mask out invalid flags from the user. */
203 fb->fb_flags = device_cfdata(&sc->sc_dev)->cf_flags & FB_USERMASK;
204
205 /*
206 * We're emulating a cg3/8, so represent ourselves as one
207 */
208 #ifdef CG14_CG8
209 fb->fb_type.fb_type = FBTYPE_MEMCOLOR;
210 fb->fb_type.fb_depth = 32;
211 fb_setsize_obp(fb, sc->sc_fb.fb_type.fb_depth, 1152, 900, node);
212 ramsize = roundup(fb->fb_type.fb_height * 1152 * 4, NBPG);
213 #else
214 fb->fb_type.fb_type = FBTYPE_SUN3COLOR;
215 fb->fb_type.fb_depth = 8;
216 fb_setsize_obp(fb, sc->sc_fb.fb_type.fb_depth, 1152, 900, node);
217 ramsize = roundup(fb->fb_type.fb_height * fb->fb_linebytes, NBPG);
218 #endif
219 fb->fb_type.fb_cmsize = CG14_CLUT_SIZE;
220 fb->fb_type.fb_size = ramsize + COLOUR_OFFSET;
221
222 if (sa->sa_nreg < 2) {
223 printf("%s: only %d register sets\n",
224 self->dv_xname, sa->sa_nreg);
225 return;
226 }
227 bcopy(sa->sa_reg, sc->sc_physadr,
228 sa->sa_nreg * sizeof(struct sbus_reg));
229
230 /*
231 * Now map in the 8 useful pages of registers
232 */
233 if (sa->sa_size < 0x10000) {
234 #ifdef DIAGNOSTIC
235 printf("warning: can't find all cgfourteen registers...\n");
236 #endif
237 sa->sa_size = 0x10000;
238 }
239 if (sbus_bus_map(sa->sa_bustag, sa->sa_slot,
240 sa->sa_offset,
241 sa->sa_size,
242 BUS_SPACE_MAP_LINEAR,
243 &bh) != 0) {
244 printf("%s: cannot map control registers\n", self->dv_xname);
245 return;
246 }
247
248 sc->sc_ctl = (struct cg14ctl *) (bh);
249 sc->sc_hwc = (struct cg14curs *) (bh + CG14_OFFSET_CURS);
250 sc->sc_dac = (struct cg14dac *) (bh + CG14_OFFSET_DAC);
251 sc->sc_xlut = (struct cg14xlut *) (bh + CG14_OFFSET_XLUT);
252 sc->sc_clut1 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT1);
253 sc->sc_clut2 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT2);
254 sc->sc_clut3 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT3);
255 sc->sc_clutincr = (u_int *) (bh + CG14_OFFSET_CLUTINCR);
256
257 /*
258 * Let the user know that we're here
259 */
260 #ifdef CG14_CG8
261 printf(": cgeight emulated at %dx%dx24bpp",
262 fb->fb_type.fb_width, fb->fb_type.fb_height);
263 #else
264 printf(": cgthree emulated at %dx%dx8bpp",
265 fb->fb_type.fb_width, fb->fb_type.fb_height);
266 #endif
267 /*
268 * Enable the video.
269 */
270 cg14_set_video(sc, 1);
271
272 /*
273 * Grab the initial colormap
274 */
275 lut = sc->sc_clut1->clut_lut;
276 for (i = 0; i < CG14_CLUT_SIZE; i++)
277 sc->sc_cmap.cm_chip[i] = lut[i];
278
279 /* See if we're the console */
280 isconsole = fb_is_console(node);
281
282 if (isconsole) {
283 printf(" (console)\n");
284 #ifdef RASTERCONSOLE
285 /* *sbus*_bus_map? but that's how we map the regs... */
286 if (sbus_bus_map( sc->sc_bustag,
287 sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
288 sc->sc_physadr[CG14_PXL_IDX].sbr_offset+0x03800000,
289 1152*900, BUS_SPACE_MAP_LINEAR,
290 &bh) != 0) {
291 printf("%s: cannot map pixels\n",&sc->sc_dev.dv_xname[0]);
292 } else {
293 sc->sc_rcfb = sc->sc_fb;
294 sc->sc_rcfb.fb_type.fb_type = FBTYPE_SUN3COLOR;
295 sc->sc_rcfb.fb_type.fb_depth = 8;
296 sc->sc_rcfb.fb_linebytes = 1152;
297 sc->sc_rcfb.fb_type.fb_size = roundup(1152*900,NBPG);
298 sc->sc_rcfb.fb_pixels = (void *)bh;
299 printf("vram at %p\n",(void *)bh);
300 for (i=0;i<1152*900;i++) ((unsigned char *)bh)[i] = 0;
301 fbrcons_init(&sc->sc_rcfb);
302 cg14_set_rcons_luts(sc);
303 sc->sc_ctl->ctl_mctl = CG14_MCTL_ENABLEVID | CG14_MCTL_PIXMODE_32 | CG14_MCTL_POWERCTL;
304 }
305 #endif
306 } else
307 printf("\n");
308
309 /* Attach to /dev/fb */
310 fb_attach(&sc->sc_fb, isconsole);
311 }
312
313 /*
314 * Keep track of the number of opens made. In the 24-bit driver, we need to
315 * switch to 24-bit mode on the first open, and switch back to 8-bit on
316 * the last close. This kind of nonsense is needed to give screenblank
317 * a fighting chance of working.
318 */
319 static int cg14_opens = 0;
320
321 int
322 cgfourteenopen(dev_t dev, int flags, int mode, struct lwp *l)
323 {
324 struct cgfourteen_softc *sc;
325 int unit;
326 int s, oldopens;
327
328 unit = minor(dev);
329 if (unit >= cgfourteen_cd.cd_ndevs)
330 return(ENXIO);
331 sc = cgfourteen_cd.cd_devs[minor(dev)];
332 if (sc == NULL)
333 return(ENXIO);
334 s = splhigh();
335 oldopens = cg14_opens++;
336 splx(s);
337
338 /* Setup the cg14 as we want it, and save the original PROM state */
339 if (oldopens == 0) /* first open only, to make screenblank work */
340 cg14_init(sc);
341
342 return (0);
343 }
344
345 int
346 cgfourteenclose(dev_t dev, int flags, int mode, struct lwp *l)
347 {
348 struct cgfourteen_softc *sc = cgfourteen_cd.cd_devs[minor(dev)];
349 int s, opens;
350
351 s = splhigh();
352 opens = --cg14_opens;
353 if (cg14_opens < 0)
354 opens = cg14_opens = 0;
355 splx(s);
356
357 /*
358 * Restore video state to make the PROM happy, on last close.
359 */
360 if (opens == 0)
361 cg14_reset(sc);
362
363 return (0);
364 }
365
366 int
367 cgfourteenioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
368 {
369 struct cgfourteen_softc *sc = cgfourteen_cd.cd_devs[minor(dev)];
370 struct fbgattr *fba;
371 int error;
372
373 switch (cmd) {
374
375 case FBIOGTYPE:
376 *(struct fbtype *)data = sc->sc_fb.fb_type;
377 break;
378
379 case FBIOGATTR:
380 fba = (struct fbgattr *)data;
381 fba->real_type = FBTYPE_MDICOLOR;
382 fba->owner = 0; /* XXX ??? */
383 fba->fbtype = sc->sc_fb.fb_type;
384 fba->sattr.flags = 0;
385 fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
386 fba->sattr.dev_specific[0] = -1;
387 fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
388 fba->emu_types[1] = -1;
389 break;
390
391 case FBIOGETCMAP:
392 return(cg14_get_cmap((struct fbcmap *)data, &sc->sc_cmap,
393 CG14_CLUT_SIZE));
394
395 case FBIOPUTCMAP:
396 /* copy to software map */
397 #define p ((struct fbcmap *)data)
398 #ifdef CG14_CG8
399 p->index &= 0xffffff;
400 #endif
401 error = cg14_put_cmap(p, &sc->sc_cmap, CG14_CLUT_SIZE);
402 if (error)
403 return (error);
404 /* now blast them into the chip */
405 /* XXX should use retrace interrupt */
406 cg14_load_hwcmap(sc, p->index, p->count);
407 #undef p
408 break;
409
410 case FBIOGVIDEO:
411 *(int *)data = cg14_get_video(sc);
412 break;
413
414 case FBIOSVIDEO:
415 cg14_set_video(sc, *(int *)data);
416 break;
417
418 default:
419 return (ENOTTY);
420 }
421 return (0);
422 }
423
424 /*
425 * Undo the effect of an FBIOSVIDEO that turns the video off.
426 */
427 static void
428 cgfourteenunblank(struct device *dev)
429 {
430
431 cg14_set_video((struct cgfourteen_softc *)dev, 1);
432 }
433
434 /*
435 * Return the address that would map the given device at the given
436 * offset, allowing for the given protection, or return -1 for error.
437 *
438 * Since we're pretending to be a cg8, we put the main video RAM at the
439 * same place the cg8 does, at offset 256k. The cg8 has an enable
440 * plane in the 256k space; our "enable" plane works differently. We
441 * can't emulate the enable plane very well, but all X uses it for is
442 * to clear it at startup - so we map the first page of video RAM over
443 * and over to fill that 256k space. We also map some other views of
444 * the video RAM space.
445 *
446 * Our memory map thus looks like
447 *
448 * mmap range space base offset
449 * 00000000-00040000 vram 0 (multi-mapped - see above)
450 * 00040000-00434800 vram 00000000
451 * 01000000-01400000 vram 01000000
452 * 02000000-02200000 vram 02000000
453 * 02800000-02a00000 vram 02800000
454 * 03000000-03100000 vram 03000000
455 * 03400000-03500000 vram 03400000
456 * 03800000-03900000 vram 03800000
457 * 03c00000-03d00000 vram 03c00000
458 * 10000000-10010000 regs 00000000 (only if CG14_MAP_REGS)
459 */
460 paddr_t
461 cgfourteenmmap(dev_t dev, off_t off, int prot)
462 {
463 struct cgfourteen_softc *sc = cgfourteen_cd.cd_devs[minor(dev)];
464
465 if (off & PGOFSET)
466 panic("cgfourteenmmap");
467
468 if (off < 0)
469 return (-1);
470
471 #if defined(CG14_MAP_REGS) /* XXX: security hole */
472 /*
473 * Map the control registers into user space. Should only be
474 * used for debugging!
475 */
476 if ((u_int)off >= 0x10000000 && (u_int)off < 0x10000000 + 16*4096) {
477 off -= 0x10000000;
478 return (bus_space_mmap(sc->sc_bustag,
479 BUS_ADDR(sc->sc_physadr[CG14_CTL_IDX].sbr_slot,
480 sc->sc_physadr[CG14_CTL_IDX].sbr_offset),
481 off, prot, BUS_SPACE_MAP_LINEAR));
482 }
483 #endif
484
485 if (off < COLOUR_OFFSET)
486 off = 0;
487 else if (off < COLOUR_OFFSET+(1152*900*4))
488 off -= COLOUR_OFFSET;
489 else {
490 switch (off >> 20) {
491 case 0x010: case 0x011: case 0x012: case 0x013:
492 case 0x020: case 0x021:
493 case 0x028: case 0x029:
494 case 0x030:
495 case 0x034:
496 case 0x038:
497 case 0x03c:
498 break;
499 default:
500 return(-1);
501 }
502 }
503
504 return (bus_space_mmap(sc->sc_bustag,
505 BUS_ADDR(sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
506 sc->sc_physadr[CG14_PXL_IDX].sbr_offset),
507 off, prot, BUS_SPACE_MAP_LINEAR));
508 }
509
510 int
511 cgfourteenpoll(dev_t dev, int events, struct lwp *l)
512 {
513
514 return (seltrue(dev, events, l));
515 }
516
517 /*
518 * Miscellaneous helper functions
519 */
520
521 /* Initialize the framebuffer, storing away useful state for later reset */
522 static void
523 cg14_init(struct cgfourteen_softc *sc)
524 {
525 volatile uint32_t *clut;
526 volatile uint8_t *xlut;
527 int i;
528
529 /*
530 * We stash away the following to restore on close:
531 *
532 * color look-up table 1 (sc->sc_saveclut)
533 * x look-up table (sc->sc_savexlut)
534 * control register (sc->sc_savectl)
535 * cursor control register (sc->sc_savehwc)
536 */
537 sc->sc_savectl = sc->sc_ctl->ctl_mctl;
538 sc->sc_savehwc = sc->sc_hwc->curs_ctl;
539
540 clut = (volatile uint32_t *) sc->sc_clut1->clut_lut;
541 xlut = (volatile uint8_t *) sc->sc_xlut->xlut_lut;
542 for (i = 0; i < CG14_CLUT_SIZE; i++) {
543 sc->sc_saveclut.cm_chip[i] = clut[i];
544 sc->sc_savexlut[i] = xlut[i];
545 }
546
547 #ifdef CG14_CG8
548 /*
549 * Enable the video, and put in 24 bit mode.
550 */
551 sc->sc_ctl->ctl_mctl = CG14_MCTL_ENABLEVID | CG14_MCTL_PIXMODE_32 |
552 CG14_MCTL_POWERCTL;
553
554 /*
555 * Zero the xlut to enable direct-color mode
556 */
557 for (i = 0; i < CG14_CLUT_SIZE; i++)
558 sc->sc_xlut->xlut_lut[i] = 0;
559 #else
560 /*
561 * Enable the video and put it in 8 bit mode
562 */
563 sc->sc_ctl->ctl_mctl = CG14_MCTL_ENABLEVID | CG14_MCTL_PIXMODE_8 |
564 CG14_MCTL_POWERCTL;
565 #endif
566 }
567
568 static void
569 /* Restore the state saved on cg14_init */
570 cg14_reset(struct cgfourteen_softc *sc)
571 {
572 volatile uint32_t *clut;
573 volatile uint8_t *xlut;
574 int i;
575
576 /*
577 * We restore the following, saved in cg14_init:
578 *
579 * color look-up table 1 (sc->sc_saveclut)
580 * x look-up table (sc->sc_savexlut)
581 * control register (sc->sc_savectl)
582 * cursor control register (sc->sc_savehwc)
583 *
584 * Note that we don't touch the video enable bits in the
585 * control register; otherwise, screenblank wouldn't work.
586 */
587 sc->sc_ctl->ctl_mctl = (sc->sc_ctl->ctl_mctl & (CG14_MCTL_ENABLEVID |
588 CG14_MCTL_POWERCTL)) |
589 (sc->sc_savectl & ~(CG14_MCTL_ENABLEVID |
590 CG14_MCTL_POWERCTL));
591 sc->sc_hwc->curs_ctl = sc->sc_savehwc;
592
593 clut = sc->sc_clut1->clut_lut;
594 xlut = sc->sc_xlut->xlut_lut;
595 for (i = 0; i < CG14_CLUT_SIZE; i++) {
596 clut[i] = sc->sc_saveclut.cm_chip[i];
597 xlut[i] = sc->sc_savexlut[i];
598 }
599 }
600
601 /* Enable/disable video display; power down monitor if DPMS-capable */
602 static void
603 cg14_set_video(struct cgfourteen_softc *sc, int enable)
604 {
605 /*
606 * We can only use DPMS to power down the display if the chip revision
607 * is greater than 0.
608 */
609 if (enable) {
610 if ((sc->sc_ctl->ctl_rsr & CG14_RSR_REVMASK) > 0)
611 sc->sc_ctl->ctl_mctl |= (CG14_MCTL_ENABLEVID |
612 CG14_MCTL_POWERCTL);
613 else
614 sc->sc_ctl->ctl_mctl |= CG14_MCTL_ENABLEVID;
615 } else {
616 if ((sc->sc_ctl->ctl_rsr & CG14_RSR_REVMASK) > 0)
617 sc->sc_ctl->ctl_mctl &= ~(CG14_MCTL_ENABLEVID |
618 CG14_MCTL_POWERCTL);
619 else
620 sc->sc_ctl->ctl_mctl &= ~CG14_MCTL_ENABLEVID;
621 }
622 }
623
624 /* Get status of video display */
625 static int
626 cg14_get_video(struct cgfourteen_softc *sc)
627 {
628 return ((sc->sc_ctl->ctl_mctl & CG14_MCTL_ENABLEVID) != 0);
629 }
630
631 /* Read the software shadow colormap */
632 static int
633 cg14_get_cmap(struct fbcmap *p, union cg14cmap *cm, int cmsize)
634 {
635 u_int i, start, count;
636 u_char *cp;
637 int error;
638
639 start = p->index;
640 count = p->count;
641 if (start >= cmsize || count > cmsize - start)
642 return (EINVAL);
643
644 for (cp = &cm->cm_map[start][0], i = 0; i < count; cp += 4, i++) {
645 error = copyout(&cp[3], &p->red[i], 1);
646 if (error)
647 return error;
648 error = copyout(&cp[2], &p->green[i], 1);
649 if (error)
650 return error;
651 error = copyout(&cp[1], &p->blue[i], 1);
652 if (error)
653 return error;
654 }
655 return (0);
656 }
657
658 /* Write the software shadow colormap */
659 static int
660 cg14_put_cmap(struct fbcmap *p, union cg14cmap *cm, int cmsize)
661 {
662 u_int i, start, count;
663 u_char *cp;
664 u_char cmap[256][4];
665 int error;
666
667 start = p->index;
668 count = p->count;
669 if (start >= cmsize || count > cmsize - start)
670 return (EINVAL);
671
672 memcpy(&cmap, &cm->cm_map, sizeof cmap);
673 for (cp = &cmap[start][0], i = 0; i < count; cp += 4, i++) {
674 error = copyin(&p->red[i], &cp[3], 1);
675 if (error)
676 return error;
677 error = copyin(&p->green[i], &cp[2], 1);
678 if (error)
679 return error;
680 error = copyin(&p->blue[i], &cp[1], 1);
681 if (error)
682 return error;
683 cp[0] = 0; /* no alpha channel */
684 }
685 memcpy(&cm->cm_map, &cmap, sizeof cmap);
686 return (0);
687 }
688
689 static void
690 cg14_load_hwcmap(struct cgfourteen_softc *sc, int start, int ncolors)
691 {
692 /* XXX switch to auto-increment, and on retrace intr */
693
694 /* Setup pointers to source and dest */
695 uint32_t *colp = &sc->sc_cmap.cm_chip[start];
696 volatile uint32_t *lutp = &sc->sc_clut1->clut_lut[start];
697
698 /* Copy by words */
699 while (--ncolors >= 0)
700 *lutp++ = *colp++;
701 }
702