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