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