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