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