cgfourteen.c revision 1.50 1 /* $NetBSD: cgfourteen.c,v 1.50 2007/07/30 23:28:13 macallan 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 "wsdisplay.h"
98 #include <dev/wscons/wsconsio.h>
99 #include <dev/wsfont/wsfont.h>
100 #include <dev/rasops/rasops.h>
101
102 #include <dev/wscons/wsdisplay_vconsvar.h>
103
104 #include <sparc/dev/cgfourteenreg.h>
105 #include <sparc/dev/cgfourteenvar.h>
106
107 #include "opt_wsemul.h"
108
109 /* autoconfiguration driver */
110 static int cgfourteenmatch(struct device *, struct cfdata *, void *);
111 static void cgfourteenattach(struct device *, struct device *, void *);
112 static void cgfourteenunblank(struct device *);
113
114 CFATTACH_DECL(cgfourteen, sizeof(struct cgfourteen_softc),
115 cgfourteenmatch, cgfourteenattach, NULL, NULL);
116
117 extern struct cfdriver cgfourteen_cd;
118
119 dev_type_open(cgfourteenopen);
120 dev_type_close(cgfourteenclose);
121 dev_type_ioctl(cgfourteenioctl);
122 dev_type_mmap(cgfourteenmmap);
123 dev_type_poll(cgfourteenpoll);
124
125 const struct cdevsw cgfourteen_cdevsw = {
126 cgfourteenopen, cgfourteenclose, noread, nowrite, cgfourteenioctl,
127 nostop, notty, cgfourteenpoll, cgfourteenmmap, nokqfilter,
128 };
129
130 /* frame buffer generic driver */
131 static struct fbdriver cgfourteenfbdriver = {
132 cgfourteenunblank, cgfourteenopen, cgfourteenclose, cgfourteenioctl,
133 cgfourteenpoll, cgfourteenmmap, nokqfilter
134 };
135
136 extern struct tty *fbconstty;
137
138 static void cg14_set_video(struct cgfourteen_softc *, int);
139 static int cg14_get_video(struct cgfourteen_softc *);
140 static int cg14_get_cmap(struct fbcmap *, union cg14cmap *, int);
141 static int cg14_put_cmap(struct fbcmap *, union cg14cmap *, int);
142 static void cg14_load_hwcmap(struct cgfourteen_softc *, int, int);
143 static void cg14_init(struct cgfourteen_softc *);
144 static void cg14_reset(struct cgfourteen_softc *);
145
146 #if NWSDISPLAY > 0
147 static void cg14_setup_wsdisplay(struct cgfourteen_softc *, int);
148 static void cg14_init_cmap(struct cgfourteen_softc *);
149 static int cg14_putcmap(struct cgfourteen_softc *, struct wsdisplay_cmap *);
150 static int cg14_getcmap(struct cgfourteen_softc *, struct wsdisplay_cmap *);
151 static void cg14_set_depth(struct cgfourteen_softc *, int);
152 static void cg14_move_cursor(struct cgfourteen_softc *, int, int);
153 static int cg14_do_cursor(struct cgfourteen_softc *,
154 struct wsdisplay_cursor *);
155 #endif
156
157 #if defined(RASTERCONSOLE) && (NWSDISPLAY > 0)
158 #error You can't have it both ways - either RASTERCONSOLE or wsdisplay
159 #endif
160
161 /*
162 * Match a cgfourteen.
163 */
164 int
165 cgfourteenmatch(struct device *parent, struct cfdata *cf, void *aux)
166 {
167 union obio_attach_args *uoba = aux;
168 struct sbus_attach_args *sa = &uoba->uoba_sbus;
169
170 /*
171 * The cgfourteen is a local-bus video adaptor, accessed directly
172 * via the processor, and not through device space or an external
173 * bus. Thus we look _only_ at the obio bus.
174 * Additionally, these things exist only on the Sun4m.
175 */
176
177 if (uoba->uoba_isobio4 != 0 || !CPU_ISSUN4M)
178 return (0);
179
180 /* Check driver name */
181 return (strcmp(cf->cf_name, sa->sa_name) == 0);
182 }
183
184 /*
185 * Set COLOUR_OFFSET to the offset of the video RAM. This is to provide
186 * space for faked overlay junk for the cg8 emulation.
187 *
188 * As it happens, this value is correct for both cg3 and cg8 emulation!
189 */
190 #define COLOUR_OFFSET (256*1024)
191
192 #ifdef RASTERCONSOLE
193 static void cg14_set_rcons_luts(struct cgfourteen_softc *sc)
194 {
195 int i;
196
197 for (i=0;i<CG14_CLUT_SIZE;i++) sc->sc_xlut->xlut_lut[i] = 0x22;
198 for (i=0;i<CG14_CLUT_SIZE;i++) sc->sc_clut2->clut_lut[i] = 0x00ffffff;
199 sc->sc_clut2->clut_lut[0] = 0x00ffffff;
200 sc->sc_clut2->clut_lut[255] = 0;
201 }
202 #endif /* RASTERCONSOLE */
203
204 #if NWSDISPLAY > 0
205 static int cg14_ioctl(void *, void *, u_long, void *, int, struct lwp *);
206 static paddr_t cg14_mmap(void *, void *, off_t, int);
207 static void cg14_init_screen(void *, struct vcons_screen *, int, long *);
208
209
210 struct wsdisplay_accessops cg14_accessops = {
211 cg14_ioctl,
212 cg14_mmap,
213 NULL, /* alloc_screen */
214 NULL, /* free_screen */
215 NULL, /* show_screen */
216 NULL, /* load_font */
217 NULL, /* pollc */
218 NULL /* scroll */
219 };
220 #endif
221
222 /*
223 * Attach a display. We need to notice if it is the console, too.
224 */
225 void
226 cgfourteenattach(struct device *parent, struct device *self, void *aux)
227 {
228 union obio_attach_args *uoba = aux;
229 struct sbus_attach_args *sa = &uoba->uoba_sbus;
230 struct cgfourteen_softc *sc = (struct cgfourteen_softc *)self;
231 struct fbdevice *fb = &sc->sc_fb;
232 bus_space_handle_t bh;
233 int node, ramsize;
234 volatile uint32_t *lut;
235 int i, isconsole;
236
237 node = sa->sa_node;
238
239 /* Remember cookies for cgfourteenmmap() */
240 sc->sc_bustag = sa->sa_bustag;
241
242 fb->fb_driver = &cgfourteenfbdriver;
243 fb->fb_device = &sc->sc_dev;
244 /* Mask out invalid flags from the user. */
245 fb->fb_flags = device_cfdata(&sc->sc_dev)->cf_flags & FB_USERMASK;
246
247 /*
248 * We're emulating a cg3/8, so represent ourselves as one
249 */
250 #ifdef CG14_CG8
251 fb->fb_type.fb_type = FBTYPE_MEMCOLOR;
252 fb->fb_type.fb_depth = 32;
253 fb_setsize_obp(fb, sc->sc_fb.fb_type.fb_depth, 1152, 900, node);
254 ramsize = roundup(fb->fb_type.fb_height * 1152 * 4, NBPG);
255 #else
256 fb->fb_type.fb_type = FBTYPE_SUN3COLOR;
257 fb->fb_type.fb_depth = 8;
258 fb_setsize_obp(fb, sc->sc_fb.fb_type.fb_depth, 1152, 900, node);
259 ramsize = roundup(fb->fb_type.fb_height * fb->fb_linebytes, NBPG);
260 #endif
261 fb->fb_type.fb_cmsize = CG14_CLUT_SIZE;
262 fb->fb_type.fb_size = ramsize + COLOUR_OFFSET;
263
264 if (sa->sa_nreg < 2) {
265 printf("%s: only %d register sets\n",
266 self->dv_xname, sa->sa_nreg);
267 return;
268 }
269 bcopy(sa->sa_reg, sc->sc_physadr,
270 sa->sa_nreg * sizeof(struct sbus_reg));
271
272 sc->sc_vramsize = sc->sc_physadr[CG14_PXL_IDX].sbr_size;
273
274 printf(": %d MB VRAM", (uint32_t)(sc->sc_vramsize >> 20));
275 /*
276 * Now map in the 8 useful pages of registers
277 */
278 if (sa->sa_size < 0x10000) {
279 #ifdef DIAGNOSTIC
280 printf("warning: can't find all cgfourteen registers...\n");
281 #endif
282 sa->sa_size = 0x10000;
283 }
284 if (sbus_bus_map(sa->sa_bustag, sa->sa_slot,
285 sa->sa_offset,
286 sa->sa_size,
287 BUS_SPACE_MAP_LINEAR,
288 &bh) != 0) {
289 printf("%s: cannot map control registers\n", self->dv_xname);
290 return;
291 }
292 sc->sc_regh = bh;
293
294 sc->sc_ctl = (struct cg14ctl *) (bh);
295 sc->sc_hwc = (struct cg14curs *) (bh + CG14_OFFSET_CURS);
296 sc->sc_dac = (struct cg14dac *) (bh + CG14_OFFSET_DAC);
297 sc->sc_xlut = (struct cg14xlut *) (bh + CG14_OFFSET_XLUT);
298 sc->sc_clut1 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT1);
299 sc->sc_clut2 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT2);
300 sc->sc_clut3 = (struct cg14clut *) (bh + CG14_OFFSET_CLUT3);
301 sc->sc_clutincr = (u_int *) (bh + CG14_OFFSET_CLUTINCR);
302
303 /*
304 * Let the user know that we're here
305 */
306 #ifdef CG14_CG8
307 printf(": cgeight emulated at %dx%dx24bpp",
308 fb->fb_type.fb_width, fb->fb_type.fb_height);
309 #else
310 printf(": cgthree emulated at %dx%dx8bpp",
311 fb->fb_type.fb_width, fb->fb_type.fb_height);
312 #endif
313 /*
314 * Enable the video.
315 */
316 cg14_set_video(sc, 1);
317
318 /*
319 * Grab the initial colormap
320 */
321 lut = sc->sc_clut1->clut_lut;
322 for (i = 0; i < CG14_CLUT_SIZE; i++)
323 sc->sc_cmap.cm_chip[i] = lut[i];
324
325 /* See if we're the console */
326 isconsole = fb_is_console(node);
327
328 #if defined(RASTERCONSOLE)
329 if (isconsole) {
330 printf(" (console)\n");
331 /* *sbus*_bus_map? but that's how we map the regs... */
332 if (sbus_bus_map( sc->sc_bustag,
333 sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
334 sc->sc_physadr[CG14_PXL_IDX].sbr_offset +
335 0x03800000,
336 1152 * 900, BUS_SPACE_MAP_LINEAR,
337 &bh) != 0) {
338 printf("%s: cannot map pixels\n",&sc->sc_dev.dv_xname[0]);
339 return;
340 }
341 sc->sc_rcfb = sc->sc_fb;
342 sc->sc_rcfb.fb_type.fb_type = FBTYPE_SUN3COLOR;
343 sc->sc_rcfb.fb_type.fb_depth = 8;
344 sc->sc_rcfb.fb_linebytes = 1152;
345 sc->sc_rcfb.fb_type.fb_size = roundup(1152*900,NBPG);
346 sc->sc_rcfb.fb_pixels = (void *)bh;
347
348 printf("vram at %p\n",(void *)bh);
349 /* XXX should use actual screen size */
350
351 for (i = 0; i < 1152 * 900; i++)
352 ((unsigned char *)bh)[i] = 0;
353 fbrcons_init(&sc->sc_rcfb);
354 cg14_set_rcons_luts(sc);
355 sc->sc_ctl->ctl_mctl = CG14_MCTL_ENABLEVID |
356 CG14_MCTL_PIXMODE_32 | CG14_MCTL_POWERCTL;
357 } else
358 printf("\n");
359 #endif
360
361 #if NWSDISPLAY > 0
362 if (sbus_bus_map( sc->sc_bustag,
363 sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
364 sc->sc_physadr[CG14_PXL_IDX].sbr_offset,
365 ramsize, BUS_SPACE_MAP_LINEAR, &bh) != 0) {
366 printf("%s: cannot map pixels\n",&sc->sc_dev.dv_xname[0]);
367 return;
368 }
369
370 sc->sc_fb.fb_pixels = bus_space_vaddr(sc->sc_bustag, bh);
371 if (isconsole)
372 printf(" (console)\n");
373
374 sc->sc_depth = 8;
375 cg14_setup_wsdisplay(sc, isconsole);
376 #endif
377
378 /* Attach to /dev/fb */
379 fb_attach(&sc->sc_fb, isconsole);
380 }
381
382 /*
383 * Keep track of the number of opens made. In the 24-bit driver, we need to
384 * switch to 24-bit mode on the first open, and switch back to 8-bit on
385 * the last close. This kind of nonsense is needed to give screenblank
386 * a fighting chance of working.
387 */
388 static int cg14_opens = 0;
389
390 int
391 cgfourteenopen(dev_t dev, int flags, int mode, struct lwp *l)
392 {
393 struct cgfourteen_softc *sc;
394 int unit;
395 int s, oldopens;
396
397 unit = minor(dev);
398 if (unit >= cgfourteen_cd.cd_ndevs)
399 return(ENXIO);
400 sc = cgfourteen_cd.cd_devs[minor(dev)];
401 if (sc == NULL)
402 return(ENXIO);
403 s = splhigh();
404 oldopens = cg14_opens++;
405 splx(s);
406
407 /* Setup the cg14 as we want it, and save the original PROM state */
408 if (oldopens == 0) /* first open only, to make screenblank work */
409 cg14_init(sc);
410
411 return (0);
412 }
413
414 int
415 cgfourteenclose(dev_t dev, int flags, int mode, struct lwp *l)
416 {
417 struct cgfourteen_softc *sc = cgfourteen_cd.cd_devs[minor(dev)];
418 int s, opens;
419
420 s = splhigh();
421 opens = --cg14_opens;
422 if (cg14_opens < 0)
423 opens = cg14_opens = 0;
424 splx(s);
425
426 /*
427 * Restore video state to make the PROM happy, on last close.
428 */
429 if (opens == 0)
430 cg14_reset(sc);
431
432 return (0);
433 }
434
435 int
436 cgfourteenioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
437 {
438 struct cgfourteen_softc *sc = cgfourteen_cd.cd_devs[minor(dev)];
439 struct fbgattr *fba;
440 int error;
441
442 switch (cmd) {
443
444 case FBIOGTYPE:
445 *(struct fbtype *)data = sc->sc_fb.fb_type;
446 break;
447
448 case FBIOGATTR:
449 fba = (struct fbgattr *)data;
450 fba->real_type = FBTYPE_MDICOLOR;
451 fba->owner = 0; /* XXX ??? */
452 fba->fbtype = sc->sc_fb.fb_type;
453 fba->sattr.flags = 0;
454 fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
455 fba->sattr.dev_specific[0] = -1;
456 fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
457 fba->emu_types[1] = -1;
458 break;
459
460 case FBIOGETCMAP:
461 return(cg14_get_cmap((struct fbcmap *)data, &sc->sc_cmap,
462 CG14_CLUT_SIZE));
463
464 case FBIOPUTCMAP:
465 /* copy to software map */
466 #define p ((struct fbcmap *)data)
467 #ifdef CG14_CG8
468 p->index &= 0xffffff;
469 #endif
470 error = cg14_put_cmap(p, &sc->sc_cmap, CG14_CLUT_SIZE);
471 if (error)
472 return (error);
473 /* now blast them into the chip */
474 /* XXX should use retrace interrupt */
475 cg14_load_hwcmap(sc, p->index, p->count);
476 #undef p
477 break;
478
479 case FBIOGVIDEO:
480 *(int *)data = cg14_get_video(sc);
481 break;
482
483 case FBIOSVIDEO:
484 cg14_set_video(sc, *(int *)data);
485 break;
486
487 default:
488 return (ENOTTY);
489 }
490 return (0);
491 }
492
493 /*
494 * Undo the effect of an FBIOSVIDEO that turns the video off.
495 */
496 static void
497 cgfourteenunblank(struct device *dev)
498 {
499 struct cgfourteen_softc *sc = (struct cgfourteen_softc *)dev;
500
501 cg14_set_video(sc, 1);
502 #if NWSDISPLAY > 0
503 if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL) {
504 cg14_set_depth(sc, 8);
505 cg14_init_cmap(sc);
506 vcons_redraw_screen(sc->sc_vd.active);
507 sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
508 }
509 #endif
510 }
511
512 /*
513 * Return the address that would map the given device at the given
514 * offset, allowing for the given protection, or return -1 for error.
515 *
516 * Since we're pretending to be a cg8, we put the main video RAM at the
517 * same place the cg8 does, at offset 256k. The cg8 has an enable
518 * plane in the 256k space; our "enable" plane works differently. We
519 * can't emulate the enable plane very well, but all X uses it for is
520 * to clear it at startup - so we map the first page of video RAM over
521 * and over to fill that 256k space. We also map some other views of
522 * the video RAM space.
523 *
524 * Our memory map thus looks like
525 *
526 * mmap range space base offset
527 * 00000000-00040000 vram 0 (multi-mapped - see above)
528 * 00040000-00434800 vram 00000000
529 * 01000000-01400000 vram 01000000
530 * 02000000-02200000 vram 02000000
531 * 02800000-02a00000 vram 02800000
532 * 03000000-03100000 vram 03000000
533 * 03400000-03500000 vram 03400000
534 * 03800000-03900000 vram 03800000
535 * 03c00000-03d00000 vram 03c00000
536 * 10000000-10010000 regs 00000000 (only if CG14_MAP_REGS)
537 */
538 paddr_t
539 cgfourteenmmap(dev_t dev, off_t off, int prot)
540 {
541 struct cgfourteen_softc *sc = cgfourteen_cd.cd_devs[minor(dev)];
542
543 if (off & PGOFSET)
544 panic("cgfourteenmmap");
545
546 if (off < 0)
547 return (-1);
548
549 #if defined(CG14_MAP_REGS) /* XXX: security hole */
550 /*
551 * Map the control registers into user space. Should only be
552 * used for debugging!
553 */
554 if ((u_int)off >= 0x10000000 && (u_int)off < 0x10000000 + 16*4096) {
555 off -= 0x10000000;
556 return (bus_space_mmap(sc->sc_bustag,
557 BUS_ADDR(sc->sc_physadr[CG14_CTL_IDX].sbr_slot,
558 sc->sc_physadr[CG14_CTL_IDX].sbr_offset),
559 off, prot, BUS_SPACE_MAP_LINEAR));
560 }
561 #endif
562
563 if (off < COLOUR_OFFSET)
564 off = 0;
565 else if (off < COLOUR_OFFSET+(1152*900*4))
566 off -= COLOUR_OFFSET;
567 else {
568 switch (off >> 20) {
569 case 0x010: case 0x011: case 0x012: case 0x013:
570 case 0x020: case 0x021:
571 case 0x028: case 0x029:
572 case 0x030:
573 case 0x034:
574 case 0x038:
575 case 0x03c:
576 break;
577 default:
578 return(-1);
579 }
580 }
581
582 return (bus_space_mmap(sc->sc_bustag,
583 BUS_ADDR(sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
584 sc->sc_physadr[CG14_PXL_IDX].sbr_offset),
585 off, prot, BUS_SPACE_MAP_LINEAR));
586 }
587
588 int
589 cgfourteenpoll(dev_t dev, int events, struct lwp *l)
590 {
591
592 return (seltrue(dev, events, l));
593 }
594
595 /*
596 * Miscellaneous helper functions
597 */
598
599 /* Initialize the framebuffer, storing away useful state for later reset */
600 static void
601 cg14_init(struct cgfourteen_softc *sc)
602 {
603 volatile uint32_t *clut;
604 volatile uint8_t *xlut;
605 int i;
606
607 /*
608 * We stash away the following to restore on close:
609 *
610 * color look-up table 1 (sc->sc_saveclut)
611 * x look-up table (sc->sc_savexlut)
612 * control register (sc->sc_savectl)
613 * cursor control register (sc->sc_savehwc)
614 */
615 sc->sc_savectl = sc->sc_ctl->ctl_mctl;
616 sc->sc_savehwc = sc->sc_hwc->curs_ctl;
617
618 clut = (volatile uint32_t *) sc->sc_clut1->clut_lut;
619 xlut = (volatile uint8_t *) sc->sc_xlut->xlut_lut;
620 for (i = 0; i < CG14_CLUT_SIZE; i++) {
621 sc->sc_saveclut.cm_chip[i] = clut[i];
622 sc->sc_savexlut[i] = xlut[i];
623 }
624
625 #ifdef CG14_CG8
626 /*
627 * Enable the video, and put in 24 bit mode.
628 */
629 sc->sc_ctl->ctl_mctl = CG14_MCTL_ENABLEVID | CG14_MCTL_PIXMODE_32 |
630 CG14_MCTL_POWERCTL;
631
632 /*
633 * Zero the xlut to enable direct-color mode
634 */
635 for (i = 0; i < CG14_CLUT_SIZE; i++)
636 sc->sc_xlut->xlut_lut[i] = 0;
637 #else
638 /*
639 * Enable the video and put it in 8 bit mode
640 */
641 sc->sc_ctl->ctl_mctl = CG14_MCTL_ENABLEVID | CG14_MCTL_PIXMODE_8 |
642 CG14_MCTL_POWERCTL;
643 #endif
644 }
645
646 static void
647 /* Restore the state saved on cg14_init */
648 cg14_reset(struct cgfourteen_softc *sc)
649 {
650 volatile uint32_t *clut;
651 volatile uint8_t *xlut;
652 int i;
653
654 /*
655 * We restore the following, saved in cg14_init:
656 *
657 * color look-up table 1 (sc->sc_saveclut)
658 * x look-up table (sc->sc_savexlut)
659 * control register (sc->sc_savectl)
660 * cursor control register (sc->sc_savehwc)
661 *
662 * Note that we don't touch the video enable bits in the
663 * control register; otherwise, screenblank wouldn't work.
664 */
665 sc->sc_ctl->ctl_mctl = (sc->sc_ctl->ctl_mctl & (CG14_MCTL_ENABLEVID |
666 CG14_MCTL_POWERCTL)) |
667 (sc->sc_savectl & ~(CG14_MCTL_ENABLEVID |
668 CG14_MCTL_POWERCTL));
669 sc->sc_hwc->curs_ctl = sc->sc_savehwc;
670
671 clut = sc->sc_clut1->clut_lut;
672 xlut = sc->sc_xlut->xlut_lut;
673 for (i = 0; i < CG14_CLUT_SIZE; i++) {
674 clut[i] = sc->sc_saveclut.cm_chip[i];
675 xlut[i] = sc->sc_savexlut[i];
676 }
677 }
678
679 /* Enable/disable video display; power down monitor if DPMS-capable */
680 static void
681 cg14_set_video(struct cgfourteen_softc *sc, int enable)
682 {
683 /*
684 * We can only use DPMS to power down the display if the chip revision
685 * is greater than 0.
686 */
687 if (enable) {
688 if ((sc->sc_ctl->ctl_rsr & CG14_RSR_REVMASK) > 0)
689 sc->sc_ctl->ctl_mctl |= (CG14_MCTL_ENABLEVID |
690 CG14_MCTL_POWERCTL);
691 else
692 sc->sc_ctl->ctl_mctl |= CG14_MCTL_ENABLEVID;
693 } else {
694 if ((sc->sc_ctl->ctl_rsr & CG14_RSR_REVMASK) > 0)
695 sc->sc_ctl->ctl_mctl &= ~(CG14_MCTL_ENABLEVID |
696 CG14_MCTL_POWERCTL);
697 else
698 sc->sc_ctl->ctl_mctl &= ~CG14_MCTL_ENABLEVID;
699 }
700 }
701
702 /* Get status of video display */
703 static int
704 cg14_get_video(struct cgfourteen_softc *sc)
705 {
706 return ((sc->sc_ctl->ctl_mctl & CG14_MCTL_ENABLEVID) != 0);
707 }
708
709 /* Read the software shadow colormap */
710 static int
711 cg14_get_cmap(struct fbcmap *p, union cg14cmap *cm, int cmsize)
712 {
713 u_int i, start, count;
714 u_char *cp;
715 int error;
716
717 start = p->index;
718 count = p->count;
719 if (start >= cmsize || count > cmsize - start)
720 return (EINVAL);
721
722 for (cp = &cm->cm_map[start][0], i = 0; i < count; cp += 4, i++) {
723 error = copyout(&cp[3], &p->red[i], 1);
724 if (error)
725 return error;
726 error = copyout(&cp[2], &p->green[i], 1);
727 if (error)
728 return error;
729 error = copyout(&cp[1], &p->blue[i], 1);
730 if (error)
731 return error;
732 }
733 return (0);
734 }
735
736 /* Write the software shadow colormap */
737 static int
738 cg14_put_cmap(struct fbcmap *p, union cg14cmap *cm, int cmsize)
739 {
740 u_int i, start, count;
741 u_char *cp;
742 u_char cmap[256][4];
743 int error;
744
745 start = p->index;
746 count = p->count;
747 if (start >= cmsize || count > cmsize - start)
748 return (EINVAL);
749
750 memcpy(&cmap, &cm->cm_map, sizeof cmap);
751 for (cp = &cmap[start][0], i = 0; i < count; cp += 4, i++) {
752 error = copyin(&p->red[i], &cp[3], 1);
753 if (error)
754 return error;
755 error = copyin(&p->green[i], &cp[2], 1);
756 if (error)
757 return error;
758 error = copyin(&p->blue[i], &cp[1], 1);
759 if (error)
760 return error;
761 cp[0] = 0; /* no alpha channel */
762 }
763 memcpy(&cm->cm_map, &cmap, sizeof cmap);
764 return (0);
765 }
766
767 static void
768 cg14_load_hwcmap(struct cgfourteen_softc *sc, int start, int ncolors)
769 {
770 /* XXX switch to auto-increment, and on retrace intr */
771
772 /* Setup pointers to source and dest */
773 uint32_t *colp = &sc->sc_cmap.cm_chip[start];
774 volatile uint32_t *lutp = &sc->sc_clut1->clut_lut[start];
775
776 /* Copy by words */
777 while (--ncolors >= 0)
778 *lutp++ = *colp++;
779 }
780
781 #if NWSDISPLAY > 0
782 static void
783 cg14_setup_wsdisplay(struct cgfourteen_softc *sc, int is_cons)
784 {
785 struct wsemuldisplaydev_attach_args aa;
786 struct rasops_info *ri;
787 long defattr;
788
789 sc->sc_defaultscreen_descr = (struct wsscreen_descr){
790 "default",
791 0, 0,
792 NULL,
793 8, 16,
794 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
795 NULL
796 };
797 sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
798 sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
799 sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
800 vcons_init(&sc->sc_vd, sc, &sc->sc_defaultscreen_descr,
801 &cg14_accessops);
802 sc->sc_vd.init_screen = cg14_init_screen;
803
804 ri = &sc->sc_console_screen.scr_ri;
805
806 if (is_cons) {
807 vcons_init_screen(&sc->sc_vd, &sc->sc_console_screen, 1,
808 &defattr);
809 sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
810
811 sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
812 sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
813 sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
814 sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
815 wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
816 defattr);
817 } else {
818 /*
819 * since we're not the console we can postpone the rest
820 * until someone actually allocates a screen for us
821 */
822 }
823
824 cg14_init_cmap(sc);
825
826 aa.console = is_cons;
827 aa.scrdata = &sc->sc_screenlist;
828 aa.accessops = &cg14_accessops;
829 aa.accesscookie = &sc->sc_vd;
830
831 config_found(&sc->sc_dev, &aa, wsemuldisplaydevprint);
832 }
833
834 static void
835 cg14_init_cmap(struct cgfourteen_softc *sc)
836 {
837 int i, j = 0;
838
839 for (i = 0; i < 256; i++) {
840
841 sc->sc_cmap.cm_map[i][3] = rasops_cmap[j];
842 sc->sc_cmap.cm_map[i][2] = rasops_cmap[j + 1];
843 sc->sc_cmap.cm_map[i][1] = rasops_cmap[j + 2];
844 j += 3;
845 }
846 cg14_load_hwcmap(sc, 0, 256);
847 }
848
849 static int
850 cg14_putcmap(struct cgfourteen_softc *sc, struct wsdisplay_cmap *cm)
851 {
852 u_int index = cm->index;
853 u_int count = cm->count;
854 int i, error;
855 u_char rbuf[256], gbuf[256], bbuf[256];
856
857 if (cm->index >= 256 || cm->count > 256 ||
858 (cm->index + cm->count) > 256)
859 return EINVAL;
860 error = copyin(cm->red, &rbuf[index], count);
861 if (error)
862 return error;
863 error = copyin(cm->green, &gbuf[index], count);
864 if (error)
865 return error;
866 error = copyin(cm->blue, &bbuf[index], count);
867 if (error)
868 return error;
869
870 for (i = 0; i < count; i++) {
871 sc->sc_cmap.cm_map[index][3] = rbuf[index];
872 sc->sc_cmap.cm_map[index][2] = gbuf[index];
873 sc->sc_cmap.cm_map[index][1] = bbuf[index];
874
875 index++;
876 }
877 cg14_load_hwcmap(sc, 0, 256);
878 return 0;
879 }
880
881 static int
882 cg14_getcmap(struct cgfourteen_softc *sc, struct wsdisplay_cmap *cm)
883 {
884 uint8_t rbuf[256], gbuf[256], bbuf[256];
885 u_int index = cm->index;
886 u_int count = cm->count;
887 int error, i;
888
889 if (index >= 255 || count > 256 || index + count > 256)
890 return EINVAL;
891
892
893 for (i = 0; i < count; i++) {
894 rbuf[i] = sc->sc_cmap.cm_map[index][3];
895 gbuf[i] = sc->sc_cmap.cm_map[index][2];
896 bbuf[i] = sc->sc_cmap.cm_map[index][1];
897
898 index++;
899 }
900 error = copyout(rbuf, cm->red, count);
901 if (error)
902 return error;
903 error = copyout(gbuf, cm->green, count);
904 if (error)
905 return error;
906 error = copyout(bbuf, cm->blue, count);
907 if (error)
908 return error;
909
910 return 0;
911 }
912 static int
913 cg14_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
914 struct lwp *l)
915 {
916 struct vcons_data *vd = v;
917 struct cgfourteen_softc *sc = vd->cookie;
918 struct wsdisplay_fbinfo *wdf;
919 struct vcons_screen *ms = vd->active;
920
921 switch (cmd) {
922
923 case WSDISPLAYIO_GTYPE:
924 *(uint32_t *)data = WSDISPLAY_TYPE_SUNCG14;
925 return 0;
926
927 case WSDISPLAYIO_GINFO:
928 wdf = (void *)data;
929 wdf->height = ms->scr_ri.ri_height;
930 wdf->width = ms->scr_ri.ri_width;
931 wdf->depth = 32;
932 wdf->cmsize = 256;
933 return 0;
934
935 case WSDISPLAYIO_GETCMAP:
936 return cg14_getcmap(sc,
937 (struct wsdisplay_cmap *)data);
938
939 case WSDISPLAYIO_PUTCMAP:
940 return cg14_putcmap(sc,
941 (struct wsdisplay_cmap *)data);
942
943 case WSDISPLAYIO_LINEBYTES:
944 *(u_int *)data = ms->scr_ri.ri_width << 2;
945 return 0;
946
947 case WSDISPLAYIO_SMODE:
948 {
949 int new_mode = *(int*)data;
950 if (new_mode != sc->sc_mode) {
951 sc->sc_mode = new_mode;
952 if(new_mode == WSDISPLAYIO_MODE_EMUL) {
953 bus_space_write_1(sc->sc_bustag,
954 sc->sc_regh,
955 CG14_CURSOR_CONTROL, 0);
956 cg14_set_depth(sc, 8);
957 cg14_init_cmap(sc);
958 vcons_redraw_screen(ms);
959 } else
960 cg14_set_depth(sc, 32);
961 }
962 }
963 return 0;
964 case WSDISPLAYIO_SVIDEO:
965 cg14_set_video(sc, *(int *)data);
966 return 0;
967 case WSDISPLAYIO_GVIDEO:
968 return cg14_get_video(sc) ?
969 WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF;
970 case WSDISPLAYIO_GCURPOS:
971 {
972 struct wsdisplay_curpos *cp = (void *)data;
973
974 cp->x = sc->sc_cursor.cc_pos.x;
975 cp->y = sc->sc_cursor.cc_pos.y;
976 }
977 return 0;
978 case WSDISPLAYIO_SCURPOS:
979 {
980 struct wsdisplay_curpos *cp = (void *)data;
981
982 cg14_move_cursor(sc, cp->x, cp->y);
983 }
984 return 0;
985 case WSDISPLAYIO_GCURMAX:
986 {
987 struct wsdisplay_curpos *cp = (void *)data;
988
989 cp->x = 32;
990 cp->y = 32;
991 }
992 return 0;
993 case WSDISPLAYIO_SCURSOR:
994 {
995 struct wsdisplay_cursor *cursor = (void *)data;
996
997 return cg14_do_cursor(sc, cursor);
998 }
999 }
1000 return EPASSTHROUGH;
1001 }
1002
1003 static paddr_t
1004 cg14_mmap(void *v, void *vs, off_t offset, int prot)
1005 {
1006 struct vcons_data *vd = v;
1007 struct cgfourteen_softc *sc = vd->cookie;
1008
1009 /* allow mmap()ing the full framebuffer, not just what we use */
1010 if (offset < sc->sc_vramsize)
1011 return bus_space_mmap(sc->sc_bustag,
1012 BUS_ADDR(sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
1013 sc->sc_physadr[CG14_PXL_IDX].sbr_offset),
1014 offset + CG14_FB_CBGR, prot, BUS_SPACE_MAP_LINEAR);
1015
1016 return -1;
1017 }
1018
1019 static void
1020 cg14_init_screen(void *cookie, struct vcons_screen *scr,
1021 int existing, long *defattr)
1022 {
1023 struct cgfourteen_softc *sc = cookie;
1024 struct rasops_info *ri = &scr->scr_ri;
1025
1026 ri->ri_depth = 8;
1027 ri->ri_width = sc->sc_fb.fb_type.fb_width;
1028 ri->ri_height = sc->sc_fb.fb_type.fb_height;
1029 ri->ri_stride = ri->ri_width;
1030 ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
1031
1032 ri->ri_bits = (char *)sc->sc_fb.fb_pixels;
1033
1034 if (existing) {
1035 ri->ri_flg |= RI_CLEAR;
1036 }
1037
1038 rasops_init(ri, sc->sc_fb.fb_type.fb_height / 8,
1039 sc->sc_fb.fb_type.fb_width / 8);
1040 ri->ri_caps = WSSCREEN_WSCOLORS;
1041
1042 rasops_reconfig(ri,
1043 sc->sc_fb.fb_type.fb_height / ri->ri_font->fontheight,
1044 sc->sc_fb.fb_type.fb_width / ri->ri_font->fontwidth);
1045
1046 ri->ri_hw = scr;
1047 }
1048
1049 static void
1050 cg14_set_depth(struct cgfourteen_softc *sc, int depth)
1051 {
1052 int i;
1053
1054 if (sc->sc_depth == depth)
1055 return;
1056 switch (depth) {
1057 case 8:
1058 bus_space_write_1(sc->sc_bustag, sc->sc_regh,
1059 CG14_MCTL, CG14_MCTL_ENABLEVID |
1060 CG14_MCTL_PIXMODE_8 | CG14_MCTL_POWERCTL);
1061 sc->sc_depth = 8;
1062 /* everything is CLUT1 */
1063 for (i = 0; i < CG14_CLUT_SIZE; i++)
1064 sc->sc_xlut->xlut_lut[i] = 0;
1065 break;
1066 case 32:
1067 bus_space_write_1(sc->sc_bustag, sc->sc_regh,
1068 CG14_MCTL, CG14_MCTL_ENABLEVID |
1069 CG14_MCTL_PIXMODE_32 | CG14_MCTL_POWERCTL);
1070 sc->sc_depth = 32;
1071 for (i = 0; i < CG14_CLUT_SIZE; i++)
1072 sc->sc_xlut->xlut_lut[i] = 0;
1073 break;
1074 default:
1075 printf("%s: can't change to depth %d\n",
1076 sc->sc_dev.dv_xname, depth);
1077 }
1078 }
1079
1080 static void
1081 cg14_move_cursor(struct cgfourteen_softc *sc, int x, int y)
1082 {
1083 uint32_t pos;
1084
1085 sc->sc_cursor.cc_pos.x = x;
1086 sc->sc_cursor.cc_pos.y = y;
1087 pos = ((sc->sc_cursor.cc_pos.x - sc->sc_cursor.cc_hot.x ) << 16) |
1088 ((sc->sc_cursor.cc_pos.y - sc->sc_cursor.cc_hot.y ) & 0xffff);
1089 bus_space_write_4(sc->sc_bustag, sc->sc_regh, CG14_CURSOR_X, pos);
1090 }
1091
1092 static int
1093 cg14_do_cursor(struct cgfourteen_softc *sc, struct wsdisplay_cursor *cur)
1094 {
1095 if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
1096
1097 bus_space_write_1(sc->sc_bustag, sc->sc_regh,
1098 CG14_CURSOR_CONTROL, cur->enable ? CG14_CRSR_ENABLE : 0);
1099 }
1100 if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
1101
1102 sc->sc_cursor.cc_hot.x = cur->hot.x;
1103 sc->sc_cursor.cc_hot.y = cur->hot.y;
1104 cur->which |= WSDISPLAY_CURSOR_DOPOS;
1105 }
1106 if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
1107
1108 cg14_move_cursor(sc, cur->pos.x, cur->pos.y);
1109 }
1110 if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
1111 int i;
1112 uint32_t val;
1113
1114 for (i = 0; i < cur->cmap.count; i++) {
1115 val = (cur->cmap.red[i] ) |
1116 (cur->cmap.green[i] << 8) |
1117 (cur->cmap.blue[i] << 16);
1118 bus_space_write_4(sc->sc_bustag, sc->sc_regh,
1119 CG14_CURSOR_COLOR1 + ((i + cur->cmap.index) << 2),
1120 val);
1121 }
1122 }
1123 if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
1124 uint32_t buffer[32], latch, tmp;
1125 int i;
1126
1127 memcpy(buffer, cur->mask, 128);
1128 for (i = 0; i < 32; i++) {
1129 latch = 0;
1130 tmp = buffer[i] & 0x80808080;
1131 latch |= tmp >> 7;
1132 tmp = buffer[i] & 0x40404040;
1133 latch |= tmp >> 5;
1134 tmp = buffer[i] & 0x20202020;
1135 latch |= tmp >> 3;
1136 tmp = buffer[i] & 0x10101010;
1137 latch |= tmp >> 1;
1138 tmp = buffer[i] & 0x08080808;
1139 latch |= tmp << 1;
1140 tmp = buffer[i] & 0x04040404;
1141 latch |= tmp << 3;
1142 tmp = buffer[i] & 0x02020202;
1143 latch |= tmp << 5;
1144 tmp = buffer[i] & 0x01010101;
1145 latch |= tmp << 7;
1146 bus_space_write_4(sc->sc_bustag, sc->sc_regh,
1147 CG14_CURSOR_PLANE0 + (i << 2), latch);
1148 }
1149 memcpy(buffer, cur->image, 128);
1150 for (i = 0; i < 32; i++) {
1151 latch = 0;
1152 tmp = buffer[i] & 0x80808080;
1153 latch |= tmp >> 7;
1154 tmp = buffer[i] & 0x40404040;
1155 latch |= tmp >> 5;
1156 tmp = buffer[i] & 0x20202020;
1157 latch |= tmp >> 3;
1158 tmp = buffer[i] & 0x10101010;
1159 latch |= tmp >> 1;
1160 tmp = buffer[i] & 0x08080808;
1161 latch |= tmp << 1;
1162 tmp = buffer[i] & 0x04040404;
1163 latch |= tmp << 3;
1164 tmp = buffer[i] & 0x02020202;
1165 latch |= tmp << 5;
1166 tmp = buffer[i] & 0x01010101;
1167 latch |= tmp << 7;
1168 bus_space_write_4(sc->sc_bustag, sc->sc_regh,
1169 CG14_CURSOR_PLANE1 + (i << 2), latch);
1170 }
1171 }
1172 return 0;
1173 }
1174 #endif
1175