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