cgfourteen.c revision 1.61 1 /* $NetBSD: cgfourteen.c,v 1.61 2009/07/14 20:57:22 apb 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 (sbus_bus_map( 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 | BUS_SPACE_MAP_LARGE,
374 &bh) != 0) {
375 printf("%s: cannot map pixels\n",
376 device_xname(sc->sc_dev));
377 return;
378 }
379 sc->sc_fb.fb_pixels = bus_space_vaddr(sc->sc_bustag, bh);
380 } else {
381 sc->sc_fb.fb_pixels = (void *)fbva[1];
382 }
383
384 sc->sc_shadowfb = kmem_alloc(ramsize, KM_NOSLEEP);
385
386 if (isconsole)
387 printf(" (console)\n");
388 else
389 printf("\n");
390
391 sc->sc_depth = 8;
392 cg14_setup_wsdisplay(sc, isconsole);
393 #endif
394
395 /* Attach to /dev/fb */
396 fb_attach(&sc->sc_fb, isconsole);
397 }
398
399 /*
400 * Keep track of the number of opens made. In the 24-bit driver, we need to
401 * switch to 24-bit mode on the first open, and switch back to 8-bit on
402 * the last close. This kind of nonsense is needed to give screenblank
403 * a fighting chance of working.
404 */
405
406 int
407 cgfourteenopen(dev_t dev, int flags, int mode, struct lwp *l)
408 {
409 struct cgfourteen_softc *sc;
410 int oldopens;
411
412 sc = device_lookup_private(&cgfourteen_cd, minor(dev));
413 if (sc == NULL)
414 return(ENXIO);
415 oldopens = sc->sc_opens++;
416
417 /* Setup the cg14 as we want it, and save the original PROM state */
418 if (oldopens == 0) /* first open only, to make screenblank work */
419 cg14_init(sc);
420
421 return (0);
422 }
423
424 int
425 cgfourteenclose(dev_t dev, int flags, int mode, struct lwp *l)
426 {
427 struct cgfourteen_softc *sc =
428 device_lookup_private(&cgfourteen_cd, minor(dev));
429 int opens;
430
431 opens = --sc->sc_opens;
432 if (sc->sc_opens < 0)
433 opens = sc->sc_opens = 0;
434
435 /*
436 * Restore video state to make the PROM happy, on last close.
437 */
438 if (opens == 0)
439 cg14_reset(sc);
440
441 return (0);
442 }
443
444 int
445 cgfourteenioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
446 {
447 struct cgfourteen_softc *sc =
448 device_lookup_private(&cgfourteen_cd, minor(dev));
449 struct fbgattr *fba;
450 int error;
451
452 switch (cmd) {
453
454 case FBIOGTYPE:
455 *(struct fbtype *)data = sc->sc_fb.fb_type;
456 break;
457
458 case FBIOGATTR:
459 fba = (struct fbgattr *)data;
460 fba->real_type = FBTYPE_MDICOLOR;
461 fba->owner = 0; /* XXX ??? */
462 fba->fbtype = sc->sc_fb.fb_type;
463 fba->sattr.flags = 0;
464 fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
465 fba->sattr.dev_specific[0] = -1;
466 fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
467 fba->emu_types[1] = -1;
468 break;
469
470 case FBIOGETCMAP:
471 return(cg14_get_cmap((struct fbcmap *)data, &sc->sc_cmap,
472 CG14_CLUT_SIZE));
473
474 case FBIOPUTCMAP:
475 /* copy to software map */
476 #define p ((struct fbcmap *)data)
477 #ifdef CG14_CG8
478 p->index &= 0xffffff;
479 #endif
480 error = cg14_put_cmap(p, &sc->sc_cmap, CG14_CLUT_SIZE);
481 if (error)
482 return (error);
483 /* now blast them into the chip */
484 /* XXX should use retrace interrupt */
485 cg14_load_hwcmap(sc, p->index, p->count);
486 #undef p
487 break;
488
489 case FBIOGVIDEO:
490 *(int *)data = cg14_get_video(sc);
491 break;
492
493 case FBIOSVIDEO:
494 cg14_set_video(sc, *(int *)data);
495 break;
496
497 default:
498 return (ENOTTY);
499 }
500 return (0);
501 }
502
503 /*
504 * Undo the effect of an FBIOSVIDEO that turns the video off.
505 */
506 static void
507 cgfourteenunblank(device_t dev)
508 {
509 struct cgfourteen_softc *sc = device_private(dev);
510
511 cg14_set_video(sc, 1);
512 #if NWSDISPLAY > 0
513 if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL) {
514 cg14_set_depth(sc, 8);
515 cg14_init_cmap(sc);
516 vcons_redraw_screen(sc->sc_vd.active);
517 sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
518 }
519 #endif
520 }
521
522 /*
523 * Return the address that would map the given device at the given
524 * offset, allowing for the given protection, or return -1 for error.
525 *
526 * Since we're pretending to be a cg8, we put the main video RAM at the
527 * same place the cg8 does, at offset 256k. The cg8 has an enable
528 * plane in the 256k space; our "enable" plane works differently. We
529 * can't emulate the enable plane very well, but all X uses it for is
530 * to clear it at startup - so we map the first page of video RAM over
531 * and over to fill that 256k space. We also map some other views of
532 * the video RAM space.
533 *
534 * Our memory map thus looks like
535 *
536 * mmap range space base offset
537 * 00000000-00040000 vram 0 (multi-mapped - see above)
538 * 00040000-00434800 vram 00000000
539 * 01000000-01400000 vram 01000000
540 * 02000000-02200000 vram 02000000
541 * 02800000-02a00000 vram 02800000
542 * 03000000-03100000 vram 03000000
543 * 03400000-03500000 vram 03400000
544 * 03800000-03900000 vram 03800000
545 * 03c00000-03d00000 vram 03c00000
546 * 10000000-10010000 regs 00000000 (only if CG14_MAP_REGS)
547 */
548 paddr_t
549 cgfourteenmmap(dev_t dev, off_t off, int prot)
550 {
551 struct cgfourteen_softc *sc =
552 device_lookup_private(&cgfourteen_cd, minor(dev));
553
554 if (off & PGOFSET)
555 panic("cgfourteenmmap");
556
557 if (off < 0)
558 return (-1);
559
560 #if defined(CG14_MAP_REGS) /* XXX: security hole */
561 /*
562 * Map the control registers into user space. Should only be
563 * used for debugging!
564 */
565 if ((u_int)off >= 0x10000000 && (u_int)off < 0x10000000 + 16*4096) {
566 off -= 0x10000000;
567 return (bus_space_mmap(sc->sc_bustag,
568 BUS_ADDR(sc->sc_physadr[CG14_CTL_IDX].sbr_slot,
569 sc->sc_physadr[CG14_CTL_IDX].sbr_offset),
570 off, prot, BUS_SPACE_MAP_LINEAR));
571 }
572 #endif
573
574 if (off < COLOUR_OFFSET)
575 off = 0;
576 else if (off < COLOUR_OFFSET+(1152*900*4))
577 off -= COLOUR_OFFSET;
578 else {
579 switch (off >> 20) {
580 case 0x010: case 0x011: case 0x012: case 0x013:
581 case 0x020: case 0x021:
582 case 0x028: case 0x029:
583 case 0x030:
584 case 0x034:
585 case 0x038:
586 case 0x03c:
587 break;
588 default:
589 return(-1);
590 }
591 }
592
593 return (bus_space_mmap(sc->sc_bustag,
594 BUS_ADDR(sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
595 sc->sc_physadr[CG14_PXL_IDX].sbr_offset),
596 off, prot, BUS_SPACE_MAP_LINEAR));
597 }
598
599 int
600 cgfourteenpoll(dev_t dev, int events, struct lwp *l)
601 {
602
603 return (seltrue(dev, events, l));
604 }
605
606 /*
607 * Miscellaneous helper functions
608 */
609
610 /* Initialize the framebuffer, storing away useful state for later reset */
611 static void
612 cg14_init(struct cgfourteen_softc *sc)
613 {
614 volatile uint32_t *clut;
615 volatile uint8_t *xlut;
616 int i;
617
618 /*
619 * We stash away the following to restore on close:
620 *
621 * color look-up table 1 (sc->sc_saveclut)
622 * x look-up table (sc->sc_savexlut)
623 * control register (sc->sc_savectl)
624 * cursor control register (sc->sc_savehwc)
625 */
626 sc->sc_savectl = sc->sc_ctl->ctl_mctl;
627 sc->sc_savehwc = sc->sc_hwc->curs_ctl;
628
629 clut = (volatile uint32_t *) sc->sc_clut1->clut_lut;
630 xlut = (volatile uint8_t *) sc->sc_xlut->xlut_lut;
631 for (i = 0; i < CG14_CLUT_SIZE; i++) {
632 sc->sc_saveclut.cm_chip[i] = clut[i];
633 sc->sc_savexlut[i] = xlut[i];
634 }
635
636 #ifdef CG14_CG8
637 /*
638 * Enable the video, and put in 24 bit mode.
639 */
640 sc->sc_ctl->ctl_mctl = CG14_MCTL_ENABLEVID | CG14_MCTL_PIXMODE_32 |
641 CG14_MCTL_POWERCTL;
642
643 /*
644 * Zero the xlut to enable direct-color mode
645 */
646 for (i = 0; i < CG14_CLUT_SIZE; i++)
647 sc->sc_xlut->xlut_lut[i] = 0;
648 #else
649 /*
650 * Enable the video and put it in 8 bit mode
651 */
652 sc->sc_ctl->ctl_mctl = CG14_MCTL_ENABLEVID | CG14_MCTL_PIXMODE_8 |
653 CG14_MCTL_POWERCTL;
654 #endif
655 }
656
657 static void
658 /* Restore the state saved on cg14_init */
659 cg14_reset(struct cgfourteen_softc *sc)
660 {
661 volatile uint32_t *clut;
662 volatile uint8_t *xlut;
663 int i;
664
665 /*
666 * We restore the following, saved in cg14_init:
667 *
668 * color look-up table 1 (sc->sc_saveclut)
669 * x look-up table (sc->sc_savexlut)
670 * control register (sc->sc_savectl)
671 * cursor control register (sc->sc_savehwc)
672 *
673 * Note that we don't touch the video enable bits in the
674 * control register; otherwise, screenblank wouldn't work.
675 */
676 sc->sc_ctl->ctl_mctl = (sc->sc_ctl->ctl_mctl & (CG14_MCTL_ENABLEVID |
677 CG14_MCTL_POWERCTL)) |
678 (sc->sc_savectl & ~(CG14_MCTL_ENABLEVID |
679 CG14_MCTL_POWERCTL));
680 sc->sc_hwc->curs_ctl = sc->sc_savehwc;
681
682 clut = sc->sc_clut1->clut_lut;
683 xlut = sc->sc_xlut->xlut_lut;
684 for (i = 0; i < CG14_CLUT_SIZE; i++) {
685 clut[i] = sc->sc_saveclut.cm_chip[i];
686 xlut[i] = sc->sc_savexlut[i];
687 }
688 }
689
690 /* Enable/disable video display; power down monitor if DPMS-capable */
691 static void
692 cg14_set_video(struct cgfourteen_softc *sc, int enable)
693 {
694 /*
695 * We can only use DPMS to power down the display if the chip revision
696 * is greater than 0.
697 */
698 if (enable) {
699 if ((sc->sc_ctl->ctl_rsr & CG14_RSR_REVMASK) > 0)
700 sc->sc_ctl->ctl_mctl |= (CG14_MCTL_ENABLEVID |
701 CG14_MCTL_POWERCTL);
702 else
703 sc->sc_ctl->ctl_mctl |= CG14_MCTL_ENABLEVID;
704 } else {
705 if ((sc->sc_ctl->ctl_rsr & CG14_RSR_REVMASK) > 0)
706 sc->sc_ctl->ctl_mctl &= ~(CG14_MCTL_ENABLEVID |
707 CG14_MCTL_POWERCTL);
708 else
709 sc->sc_ctl->ctl_mctl &= ~CG14_MCTL_ENABLEVID;
710 }
711 }
712
713 /* Get status of video display */
714 static int
715 cg14_get_video(struct cgfourteen_softc *sc)
716 {
717 return ((sc->sc_ctl->ctl_mctl & CG14_MCTL_ENABLEVID) != 0);
718 }
719
720 /* Read the software shadow colormap */
721 static int
722 cg14_get_cmap(struct fbcmap *p, union cg14cmap *cm, int cmsize)
723 {
724 u_int i, start, count;
725 u_char *cp;
726 int error;
727
728 start = p->index;
729 count = p->count;
730 if (start >= cmsize || count > cmsize - start)
731 return (EINVAL);
732
733 for (cp = &cm->cm_map[start][0], i = 0; i < count; cp += 4, i++) {
734 error = copyout(&cp[3], &p->red[i], 1);
735 if (error)
736 return error;
737 error = copyout(&cp[2], &p->green[i], 1);
738 if (error)
739 return error;
740 error = copyout(&cp[1], &p->blue[i], 1);
741 if (error)
742 return error;
743 }
744 return (0);
745 }
746
747 /* Write the software shadow colormap */
748 static int
749 cg14_put_cmap(struct fbcmap *p, union cg14cmap *cm, int cmsize)
750 {
751 u_int i, start, count;
752 u_char *cp;
753 u_char cmap[256][4];
754 int error;
755
756 start = p->index;
757 count = p->count;
758 if (start >= cmsize || count > cmsize - start)
759 return (EINVAL);
760
761 memcpy(&cmap, &cm->cm_map, sizeof cmap);
762 for (cp = &cmap[start][0], i = 0; i < count; cp += 4, i++) {
763 error = copyin(&p->red[i], &cp[3], 1);
764 if (error)
765 return error;
766 error = copyin(&p->green[i], &cp[2], 1);
767 if (error)
768 return error;
769 error = copyin(&p->blue[i], &cp[1], 1);
770 if (error)
771 return error;
772 cp[0] = 0; /* no alpha channel */
773 }
774 memcpy(&cm->cm_map, &cmap, sizeof cmap);
775 return (0);
776 }
777
778 static void
779 cg14_load_hwcmap(struct cgfourteen_softc *sc, int start, int ncolors)
780 {
781 /* XXX switch to auto-increment, and on retrace intr */
782
783 /* Setup pointers to source and dest */
784 uint32_t *colp = &sc->sc_cmap.cm_chip[start];
785 volatile uint32_t *lutp = &sc->sc_clut1->clut_lut[start];
786
787 /* Copy by words */
788 while (--ncolors >= 0)
789 *lutp++ = *colp++;
790 }
791
792 #if NWSDISPLAY > 0
793 static void
794 cg14_setup_wsdisplay(struct cgfourteen_softc *sc, int is_cons)
795 {
796 struct wsemuldisplaydev_attach_args aa;
797 struct rasops_info *ri;
798 long defattr;
799
800 sc->sc_defaultscreen_descr = (struct wsscreen_descr){
801 "default",
802 0, 0,
803 NULL,
804 8, 16,
805 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
806 NULL
807 };
808 sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
809 sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
810 sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
811 vcons_init(&sc->sc_vd, sc, &sc->sc_defaultscreen_descr,
812 &cg14_accessops);
813 sc->sc_vd.init_screen = cg14_init_screen;
814
815 ri = &sc->sc_console_screen.scr_ri;
816
817 if (is_cons) {
818 vcons_init_screen(&sc->sc_vd, &sc->sc_console_screen, 1,
819 &defattr);
820
821 /* clear the screen with the default background colour */
822 memset(sc->sc_fb.fb_pixels,
823 (defattr >> 16) & 0xff,
824 ri->ri_stride * ri->ri_height);
825 if (sc->sc_shadowfb != NULL)
826 memset(sc->sc_shadowfb,
827 (defattr >> 16) & 0xff,
828 ri->ri_stride * ri->ri_height);
829 sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
830
831 sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
832 sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
833 sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
834 sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
835 wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
836 defattr);
837 } else {
838 /*
839 * since we're not the console we can postpone the rest
840 * until someone actually allocates a screen for us
841 */
842 }
843
844 cg14_init_cmap(sc);
845
846 aa.console = is_cons;
847 aa.scrdata = &sc->sc_screenlist;
848 aa.accessops = &cg14_accessops;
849 aa.accesscookie = &sc->sc_vd;
850
851 config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
852 }
853
854 static void
855 cg14_init_cmap(struct cgfourteen_softc *sc)
856 {
857 int i, j = 0;
858
859 for (i = 0; i < 256; i++) {
860
861 sc->sc_cmap.cm_map[i][3] = rasops_cmap[j];
862 sc->sc_cmap.cm_map[i][2] = rasops_cmap[j + 1];
863 sc->sc_cmap.cm_map[i][1] = rasops_cmap[j + 2];
864 j += 3;
865 }
866 cg14_load_hwcmap(sc, 0, 256);
867 }
868
869 static int
870 cg14_putcmap(struct cgfourteen_softc *sc, struct wsdisplay_cmap *cm)
871 {
872 u_int index = cm->index;
873 u_int count = cm->count;
874 int i, error;
875 u_char rbuf[256], gbuf[256], bbuf[256];
876
877 if (cm->index >= 256 || cm->count > 256 ||
878 (cm->index + cm->count) > 256)
879 return EINVAL;
880 error = copyin(cm->red, &rbuf[index], count);
881 if (error)
882 return error;
883 error = copyin(cm->green, &gbuf[index], count);
884 if (error)
885 return error;
886 error = copyin(cm->blue, &bbuf[index], count);
887 if (error)
888 return error;
889
890 for (i = 0; i < count; i++) {
891 sc->sc_cmap.cm_map[index][3] = rbuf[index];
892 sc->sc_cmap.cm_map[index][2] = gbuf[index];
893 sc->sc_cmap.cm_map[index][1] = bbuf[index];
894
895 index++;
896 }
897 cg14_load_hwcmap(sc, 0, 256);
898 return 0;
899 }
900
901 static int
902 cg14_getcmap(struct cgfourteen_softc *sc, struct wsdisplay_cmap *cm)
903 {
904 uint8_t rbuf[256], gbuf[256], bbuf[256];
905 u_int index = cm->index;
906 u_int count = cm->count;
907 int error, i;
908
909 if (index >= 255 || count > 256 || index + count > 256)
910 return EINVAL;
911
912
913 for (i = 0; i < count; i++) {
914 rbuf[i] = sc->sc_cmap.cm_map[index][3];
915 gbuf[i] = sc->sc_cmap.cm_map[index][2];
916 bbuf[i] = sc->sc_cmap.cm_map[index][1];
917
918 index++;
919 }
920 error = copyout(rbuf, cm->red, count);
921 if (error)
922 return error;
923 error = copyout(gbuf, cm->green, count);
924 if (error)
925 return error;
926 error = copyout(bbuf, cm->blue, count);
927 if (error)
928 return error;
929
930 return 0;
931 }
932 static int
933 cg14_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
934 struct lwp *l)
935 {
936 struct vcons_data *vd = v;
937 struct cgfourteen_softc *sc = vd->cookie;
938 struct wsdisplay_fbinfo *wdf;
939 struct vcons_screen *ms = vd->active;
940
941 switch (cmd) {
942
943 case WSDISPLAYIO_GTYPE:
944 *(uint32_t *)data = WSDISPLAY_TYPE_SUNCG14;
945 return 0;
946
947 case WSDISPLAYIO_GINFO:
948 wdf = (void *)data;
949 wdf->height = ms->scr_ri.ri_height;
950 wdf->width = ms->scr_ri.ri_width;
951 wdf->depth = 32;
952 wdf->cmsize = 256;
953 return 0;
954
955 case WSDISPLAYIO_GETCMAP:
956 return cg14_getcmap(sc,
957 (struct wsdisplay_cmap *)data);
958
959 case WSDISPLAYIO_PUTCMAP:
960 return cg14_putcmap(sc,
961 (struct wsdisplay_cmap *)data);
962
963 case WSDISPLAYIO_LINEBYTES:
964 *(u_int *)data = ms->scr_ri.ri_stride << 2;
965 return 0;
966
967 case WSDISPLAYIO_SMODE:
968 {
969 int new_mode = *(int*)data;
970 if (new_mode != sc->sc_mode) {
971 sc->sc_mode = new_mode;
972 if(new_mode == WSDISPLAYIO_MODE_EMUL) {
973 bus_space_write_1(sc->sc_bustag,
974 sc->sc_regh,
975 CG14_CURSOR_CONTROL, 0);
976 cg14_set_depth(sc, 8);
977 cg14_init_cmap(sc);
978 vcons_redraw_screen(ms);
979 } else {
980 cg14_set_depth(sc, 32);
981 }
982 }
983 }
984 return 0;
985 case WSDISPLAYIO_SVIDEO:
986 cg14_set_video(sc, *(int *)data);
987 return 0;
988 case WSDISPLAYIO_GVIDEO:
989 return cg14_get_video(sc) ?
990 WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF;
991 case WSDISPLAYIO_GCURPOS:
992 {
993 struct wsdisplay_curpos *cp = (void *)data;
994
995 cp->x = sc->sc_cursor.cc_pos.x;
996 cp->y = sc->sc_cursor.cc_pos.y;
997 }
998 return 0;
999 case WSDISPLAYIO_SCURPOS:
1000 {
1001 struct wsdisplay_curpos *cp = (void *)data;
1002
1003 cg14_move_cursor(sc, cp->x, cp->y);
1004 }
1005 return 0;
1006 case WSDISPLAYIO_GCURMAX:
1007 {
1008 struct wsdisplay_curpos *cp = (void *)data;
1009
1010 cp->x = 32;
1011 cp->y = 32;
1012 }
1013 return 0;
1014 case WSDISPLAYIO_SCURSOR:
1015 {
1016 struct wsdisplay_cursor *cursor = (void *)data;
1017
1018 return cg14_do_cursor(sc, cursor);
1019 }
1020 case PCI_IOC_CFGREAD:
1021 case PCI_IOC_CFGWRITE:
1022 return EINVAL;
1023
1024 }
1025 return EPASSTHROUGH;
1026 }
1027
1028 static paddr_t
1029 cg14_mmap(void *v, void *vs, off_t offset, int prot)
1030 {
1031 struct vcons_data *vd = v;
1032 struct cgfourteen_softc *sc = vd->cookie;
1033
1034 /* allow mmap()ing the full framebuffer, not just what we use */
1035 if (offset < sc->sc_vramsize)
1036 return bus_space_mmap(sc->sc_bustag,
1037 BUS_ADDR(sc->sc_physadr[CG14_PXL_IDX].sbr_slot,
1038 sc->sc_physadr[CG14_PXL_IDX].sbr_offset),
1039 offset + CG14_FB_CBGR, prot, BUS_SPACE_MAP_LINEAR);
1040
1041 return -1;
1042 }
1043
1044 static void
1045 cg14_init_screen(void *cookie, struct vcons_screen *scr,
1046 int existing, long *defattr)
1047 {
1048 struct cgfourteen_softc *sc = cookie;
1049 struct rasops_info *ri = &scr->scr_ri;
1050
1051 ri->ri_depth = 8;
1052 ri->ri_width = sc->sc_fb.fb_type.fb_width;
1053 ri->ri_height = sc->sc_fb.fb_type.fb_height;
1054 ri->ri_stride = ri->ri_width;
1055 ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
1056
1057 if (sc->sc_shadowfb != NULL) {
1058 ri->ri_bits = sc->sc_shadowfb;
1059 ri->ri_hwbits = (char *)sc->sc_fb.fb_pixels;
1060 } else
1061 ri->ri_bits = (char *)sc->sc_fb.fb_pixels;
1062
1063 if (existing) {
1064 ri->ri_flg |= RI_CLEAR;
1065 }
1066
1067 rasops_init(ri, sc->sc_fb.fb_type.fb_height / 8,
1068 sc->sc_fb.fb_type.fb_width / 8);
1069 ri->ri_caps = WSSCREEN_WSCOLORS;
1070
1071 rasops_reconfig(ri,
1072 sc->sc_fb.fb_type.fb_height / ri->ri_font->fontheight,
1073 sc->sc_fb.fb_type.fb_width / ri->ri_font->fontwidth);
1074
1075 ri->ri_hw = scr;
1076 }
1077
1078 static void
1079 cg14_set_depth(struct cgfourteen_softc *sc, int depth)
1080 {
1081 int i;
1082
1083 if (sc->sc_depth == depth)
1084 return;
1085 switch (depth) {
1086 case 8:
1087 bus_space_write_1(sc->sc_bustag, sc->sc_regh,
1088 CG14_MCTL, CG14_MCTL_ENABLEVID |
1089 CG14_MCTL_PIXMODE_8 | CG14_MCTL_POWERCTL);
1090 sc->sc_depth = 8;
1091 /* everything is CLUT1 */
1092 for (i = 0; i < CG14_CLUT_SIZE; i++)
1093 sc->sc_xlut->xlut_lut[i] = 0;
1094 break;
1095 case 32:
1096 bus_space_write_1(sc->sc_bustag, sc->sc_regh,
1097 CG14_MCTL, CG14_MCTL_ENABLEVID |
1098 CG14_MCTL_PIXMODE_32 | CG14_MCTL_POWERCTL);
1099 sc->sc_depth = 32;
1100 for (i = 0; i < CG14_CLUT_SIZE; i++)
1101 sc->sc_xlut->xlut_lut[i] = 0;
1102 break;
1103 default:
1104 printf("%s: can't change to depth %d\n",
1105 device_xname(sc->sc_dev), depth);
1106 }
1107 }
1108
1109 static void
1110 cg14_move_cursor(struct cgfourteen_softc *sc, int x, int y)
1111 {
1112 uint32_t pos;
1113
1114 sc->sc_cursor.cc_pos.x = x;
1115 sc->sc_cursor.cc_pos.y = y;
1116 pos = ((sc->sc_cursor.cc_pos.x - sc->sc_cursor.cc_hot.x ) << 16) |
1117 ((sc->sc_cursor.cc_pos.y - sc->sc_cursor.cc_hot.y ) & 0xffff);
1118 bus_space_write_4(sc->sc_bustag, sc->sc_regh, CG14_CURSOR_X, pos);
1119 }
1120
1121 static int
1122 cg14_do_cursor(struct cgfourteen_softc *sc, struct wsdisplay_cursor *cur)
1123 {
1124 if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
1125
1126 bus_space_write_1(sc->sc_bustag, sc->sc_regh,
1127 CG14_CURSOR_CONTROL, cur->enable ? CG14_CRSR_ENABLE : 0);
1128 }
1129 if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
1130
1131 sc->sc_cursor.cc_hot.x = cur->hot.x;
1132 sc->sc_cursor.cc_hot.y = cur->hot.y;
1133 cur->which |= WSDISPLAY_CURSOR_DOPOS;
1134 }
1135 if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
1136
1137 cg14_move_cursor(sc, cur->pos.x, cur->pos.y);
1138 }
1139 if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
1140 int i;
1141 uint32_t val;
1142
1143 for (i = 0; i < min(cur->cmap.count, 3); i++) {
1144 val = (cur->cmap.red[i] ) |
1145 (cur->cmap.green[i] << 8) |
1146 (cur->cmap.blue[i] << 16);
1147 bus_space_write_4(sc->sc_bustag, sc->sc_regh,
1148 CG14_CURSOR_COLOR1 + ((i + cur->cmap.index) << 2),
1149 val);
1150 }
1151 }
1152 if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
1153 uint32_t buffer[32], latch, tmp;
1154 int i;
1155
1156 copyin(cur->mask, buffer, 128);
1157 for (i = 0; i < 32; i++) {
1158 latch = 0;
1159 tmp = buffer[i] & 0x80808080;
1160 latch |= tmp >> 7;
1161 tmp = buffer[i] & 0x40404040;
1162 latch |= tmp >> 5;
1163 tmp = buffer[i] & 0x20202020;
1164 latch |= tmp >> 3;
1165 tmp = buffer[i] & 0x10101010;
1166 latch |= tmp >> 1;
1167 tmp = buffer[i] & 0x08080808;
1168 latch |= tmp << 1;
1169 tmp = buffer[i] & 0x04040404;
1170 latch |= tmp << 3;
1171 tmp = buffer[i] & 0x02020202;
1172 latch |= tmp << 5;
1173 tmp = buffer[i] & 0x01010101;
1174 latch |= tmp << 7;
1175 bus_space_write_4(sc->sc_bustag, sc->sc_regh,
1176 CG14_CURSOR_PLANE0 + (i << 2), latch);
1177 }
1178 copyin(cur->image, buffer, 128);
1179 for (i = 0; i < 32; i++) {
1180 latch = 0;
1181 tmp = buffer[i] & 0x80808080;
1182 latch |= tmp >> 7;
1183 tmp = buffer[i] & 0x40404040;
1184 latch |= tmp >> 5;
1185 tmp = buffer[i] & 0x20202020;
1186 latch |= tmp >> 3;
1187 tmp = buffer[i] & 0x10101010;
1188 latch |= tmp >> 1;
1189 tmp = buffer[i] & 0x08080808;
1190 latch |= tmp << 1;
1191 tmp = buffer[i] & 0x04040404;
1192 latch |= tmp << 3;
1193 tmp = buffer[i] & 0x02020202;
1194 latch |= tmp << 5;
1195 tmp = buffer[i] & 0x01010101;
1196 latch |= tmp << 7;
1197 bus_space_write_4(sc->sc_bustag, sc->sc_regh,
1198 CG14_CURSOR_PLANE1 + (i << 2), latch);
1199 }
1200 }
1201 return 0;
1202 }
1203 #endif
1204