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