1 /* $NetBSD: cgthree.c,v 1.31 2014/07/25 08:10:39 dholland Exp $ */ 2 3 /*- 4 * Copyright (c) 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Paul Kranenburg. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * color display (cgthree) driver. 34 * 35 * Does not handle interrupts, even though they can occur. 36 * 37 * XXX should defer colormap updates to vertical retrace interrupts 38 */ 39 40 #include <sys/cdefs.h> 41 __KERNEL_RCSID(0, "$NetBSD: cgthree.c,v 1.31 2014/07/25 08:10:39 dholland Exp $"); 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/buf.h> 46 #include <sys/device.h> 47 #include <sys/ioctl.h> 48 #include <sys/malloc.h> 49 #include <sys/mman.h> 50 #include <sys/tty.h> 51 #include <sys/conf.h> 52 53 #include <sys/bus.h> 54 #include <machine/autoconf.h> 55 56 #include <dev/sun/fbio.h> 57 #include <dev/sun/fbvar.h> 58 59 #include <dev/sun/btreg.h> 60 #include <dev/sun/btvar.h> 61 #include <dev/sun/cgthreereg.h> 62 #include <dev/sun/cgthreevar.h> 63 64 #if NWSDISPLAY > 0 65 #include <dev/wscons/wsconsio.h> 66 #include <dev/wsfont/wsfont.h> 67 #include <dev/rasops/rasops.h> 68 69 #include "opt_wsemul.h" 70 #endif 71 72 #include "ioconf.h" 73 74 static void cgthreeunblank(device_t); 75 static void cgthreeloadcmap(struct cgthree_softc *, int, int); 76 static void cgthree_set_video(struct cgthree_softc *, int); 77 static int cgthree_get_video(struct cgthree_softc *); 78 79 dev_type_open(cgthreeopen); 80 dev_type_ioctl(cgthreeioctl); 81 dev_type_mmap(cgthreemmap); 82 83 const struct cdevsw cgthree_cdevsw = { 84 .d_open = cgthreeopen, 85 .d_close = nullclose, 86 .d_read = noread, 87 .d_write = nowrite, 88 .d_ioctl = cgthreeioctl, 89 .d_stop = nostop, 90 .d_tty = notty, 91 .d_poll = nopoll, 92 .d_mmap = cgthreemmap, 93 .d_kqfilter = nokqfilter, 94 .d_discard = nodiscard, 95 .d_flag = D_OTHER 96 }; 97 98 /* frame buffer generic driver */ 99 static struct fbdriver cgthreefbdriver = { 100 cgthreeunblank, cgthreeopen, nullclose, cgthreeioctl, nopoll, 101 cgthreemmap, nokqfilter 102 }; 103 104 /* Video control parameters */ 105 struct cg3_videoctrl { 106 unsigned char sense; /* Monitor sense value */ 107 unsigned char vctrl[12]; 108 } cg3_videoctrl[] = { 109 /* Missing entries: sense 0x10, 0x30, 0x50 */ 110 { 0x40, /* this happens to be my 19'' 1152x900 gray-scale monitor */ 111 {0xbb, 0x2b, 0x3, 0xb, 0xb3, 0x3, 0xaf, 0x2b, 0x2, 0xa, 0xff, 0x1} 112 }, 113 { 0x00, /* default? must be last */ 114 {0xbb, 0x2b, 0x3, 0xb, 0xb3, 0x3, 0xaf, 0x2b, 0x2, 0xa, 0xff, 0x1} 115 } 116 }; 117 118 #if NWSDISPLAY > 0 119 #ifdef RASTERCONSOLE 120 #error RASTERCONSOLE and wsdisplay are mutually exclusive 121 #endif 122 123 static void cg3_setup_palette(struct cgthree_softc *); 124 125 struct wsscreen_descr cgthree_defaultscreen = { 126 "std", 127 0, 0, /* will be filled in -- XXX shouldn't, it's global */ 128 NULL, /* textops */ 129 8, 16, /* font width/height */ 130 WSSCREEN_WSCOLORS, /* capabilities */ 131 NULL /* modecookie */ 132 }; 133 134 static int cgthree_ioctl(void *, void *, u_long, void *, int, struct lwp *); 135 static paddr_t cgthree_mmap(void *, void *, off_t, int); 136 static void cgthree_init_screen(void *, struct vcons_screen *, int, long *); 137 138 int cgthree_putcmap(struct cgthree_softc *, struct wsdisplay_cmap *); 139 int cgthree_getcmap(struct cgthree_softc *, struct wsdisplay_cmap *); 140 141 struct wsdisplay_accessops cgthree_accessops = { 142 cgthree_ioctl, 143 cgthree_mmap, 144 NULL, /* alloc_screen */ 145 NULL, /* free_screen */ 146 NULL, /* show_screen */ 147 NULL, /* load_font */ 148 NULL, /* pollc */ 149 NULL /* scroll */ 150 }; 151 152 const struct wsscreen_descr *_cgthree_scrlist[] = { 153 &cgthree_defaultscreen 154 }; 155 156 struct wsscreen_list cgthree_screenlist = { 157 sizeof(_cgthree_scrlist) / sizeof(struct wsscreen_descr *), 158 _cgthree_scrlist 159 }; 160 161 162 extern const u_char rasops_cmap[768]; 163 164 static struct vcons_screen cg3_console_screen; 165 #endif /* NWSDISPLAY > 0 */ 166 167 void 168 cgthreeattach(struct cgthree_softc *sc, const char *name, int isconsole) 169 { 170 int i; 171 struct fbdevice *fb = &sc->sc_fb; 172 #if NWSDISPLAY > 0 173 struct wsemuldisplaydev_attach_args aa; 174 struct rasops_info *ri = &cg3_console_screen.scr_ri; 175 unsigned long defattr; 176 #endif 177 volatile struct fbcontrol *fbc = sc->sc_fbc; 178 volatile struct bt_regs *bt = &fbc->fbc_dac; 179 180 fb->fb_driver = &cgthreefbdriver; 181 fb->fb_type.fb_cmsize = 256; 182 fb->fb_type.fb_size = fb->fb_type.fb_height * fb->fb_linebytes; 183 printf(": %s, %d x %d", name, 184 fb->fb_type.fb_width, fb->fb_type.fb_height); 185 186 /* Transfer video magic to board, if it's not running */ 187 if ((fbc->fbc_ctrl & FBC_TIMING) == 0) { 188 int sense = (fbc->fbc_status & FBS_MSENSE); 189 /* Search table for video timings fitting this monitor */ 190 for (i = 0; i < sizeof(cg3_videoctrl)/sizeof(cg3_videoctrl[0]); 191 i++) { 192 int j; 193 if (sense != cg3_videoctrl[i].sense) 194 continue; 195 196 printf(" setting video ctrl"); 197 for (j = 0; j < 12; j++) 198 fbc->fbc_vcontrol[j] = 199 cg3_videoctrl[i].vctrl[j]; 200 fbc->fbc_ctrl |= FBC_TIMING; 201 break; 202 } 203 } 204 205 /* make sure we are not blanked */ 206 cgthree_set_video(sc, 1); 207 BT_INIT(bt, 0); 208 209 if (isconsole) { 210 printf(" (console)\n"); 211 #ifdef RASTERCONSOLE 212 fbrcons_init(fb); 213 #endif 214 } else 215 printf("\n"); 216 217 fb_attach(fb, isconsole); 218 219 #if NWSDISPLAY > 0 220 sc->sc_width = fb->fb_type.fb_width; 221 sc->sc_stride = fb->fb_type.fb_width; 222 sc->sc_height = fb->fb_type.fb_height; 223 224 /* setup rasops and so on for wsdisplay */ 225 sc->sc_mode = WSDISPLAYIO_MODE_EMUL; 226 227 vcons_init(&sc->vd, sc, &cgthree_defaultscreen, &cgthree_accessops); 228 sc->vd.init_screen = cgthree_init_screen; 229 230 if(isconsole) { 231 /* we mess with cg3_console_screen only once */ 232 vcons_init_screen(&sc->vd, &cg3_console_screen, 1, 233 &defattr); 234 memset(sc->sc_fb.fb_pixels, (defattr >> 16) & 0xff, 235 sc->sc_stride * sc->sc_height); 236 cg3_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC; 237 238 cgthree_defaultscreen.textops = &ri->ri_ops; 239 cgthree_defaultscreen.capabilities = ri->ri_caps; 240 cgthree_defaultscreen.nrows = ri->ri_rows; 241 cgthree_defaultscreen.ncols = ri->ri_cols; 242 sc->vd.active = &cg3_console_screen; 243 wsdisplay_cnattach(&cgthree_defaultscreen, ri, 0, 0, defattr); 244 vcons_replay_msgbuf(&cg3_console_screen); 245 } else { 246 /* 247 * we're not the console so we just clear the screen and don't 248 * set up any sort of text display 249 */ 250 } 251 252 /* Initialize the default color map. */ 253 cg3_setup_palette(sc); 254 255 aa.scrdata = &cgthree_screenlist; 256 aa.console = isconsole; 257 aa.accessops = &cgthree_accessops; 258 aa.accesscookie = &sc->vd; 259 config_found(sc->sc_dev, &aa, wsemuldisplaydevprint); 260 #else 261 /* Initialize the default color map. */ 262 bt_initcmap(&sc->sc_cmap, 256); 263 cgthreeloadcmap(sc, 0, 256); 264 #endif 265 266 } 267 268 269 int 270 cgthreeopen(dev_t dev, int flags, int mode, struct lwp *l) 271 { 272 int unit = minor(dev); 273 274 if (device_lookup(&cgthree_cd, unit) == NULL) 275 return (ENXIO); 276 return (0); 277 } 278 279 int 280 cgthreeioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l) 281 { 282 struct cgthree_softc *sc = device_lookup_private(&cgthree_cd, 283 minor(dev)); 284 struct fbgattr *fba; 285 int error; 286 287 switch (cmd) { 288 289 case FBIOGTYPE: 290 *(struct fbtype *)data = sc->sc_fb.fb_type; 291 break; 292 293 case FBIOGATTR: 294 fba = (struct fbgattr *)data; 295 fba->real_type = sc->sc_fb.fb_type.fb_type; 296 fba->owner = 0; /* XXX ??? */ 297 fba->fbtype = sc->sc_fb.fb_type; 298 fba->sattr.flags = 0; 299 fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type; 300 fba->sattr.dev_specific[0] = -1; 301 fba->emu_types[0] = sc->sc_fb.fb_type.fb_type; 302 fba->emu_types[1] = -1; 303 break; 304 305 case FBIOGETCMAP: 306 #define p ((struct fbcmap *)data) 307 return (bt_getcmap(p, &sc->sc_cmap, 256, 1)); 308 309 case FBIOPUTCMAP: 310 /* copy to software map */ 311 error = bt_putcmap(p, &sc->sc_cmap, 256, 1); 312 if (error) 313 return (error); 314 /* now blast them into the chip */ 315 /* XXX should use retrace interrupt */ 316 cgthreeloadcmap(sc, p->index, p->count); 317 #undef p 318 break; 319 320 case FBIOGVIDEO: 321 *(int *)data = cgthree_get_video(sc); 322 break; 323 324 case FBIOSVIDEO: 325 cgthree_set_video(sc, *(int *)data); 326 break; 327 328 default: 329 return (ENOTTY); 330 } 331 return (0); 332 } 333 334 /* 335 * Undo the effect of an FBIOSVIDEO that turns the video off. 336 */ 337 static void 338 cgthreeunblank(device_t self) 339 { 340 struct cgthree_softc *sc = device_private(self); 341 342 cgthree_set_video(sc, 1); 343 } 344 345 static void 346 cgthree_set_video(struct cgthree_softc *sc, int enable) 347 { 348 349 if (enable) 350 sc->sc_fbc->fbc_ctrl |= FBC_VENAB; 351 else 352 sc->sc_fbc->fbc_ctrl &= ~FBC_VENAB; 353 } 354 355 static int 356 cgthree_get_video(struct cgthree_softc *sc) 357 { 358 359 return ((sc->sc_fbc->fbc_ctrl & FBC_VENAB) != 0); 360 } 361 362 /* 363 * Load a subset of the current (new) colormap into the Brooktree DAC. 364 */ 365 static void 366 cgthreeloadcmap(struct cgthree_softc *sc, int start, int ncolors) 367 { 368 volatile struct bt_regs *bt; 369 u_int *ip; 370 int count; 371 372 ip = &sc->sc_cmap.cm_chip[BT_D4M3(start)]; /* start/4 * 3 */ 373 count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3; 374 bt = &sc->sc_fbc->fbc_dac; 375 bt->bt_addr = BT_D4M4(start); 376 while (--count >= 0) 377 bt->bt_cmap = *ip++; 378 } 379 380 /* 381 * Return the address that would map the given device at the given 382 * offset, allowing for the given protection, or return -1 for error. 383 * 384 * The cg3 is mapped starting at 256KB, for pseudo-compatibility with 385 * the cg4 (which had an overlay plane in the first 128K and an enable 386 * plane in the next 128K). X11 uses only 256k+ region but tries to 387 * map the whole thing, so we repeatedly map the first 256K to the 388 * first page of the color screen. If someone tries to use the overlay 389 * and enable regions, they will get a surprise.... 390 * 391 * As well, mapping at an offset of 0x04000000 causes the cg3 to be 392 * mapped in flat mode without the cg4 emulation. 393 */ 394 paddr_t 395 cgthreemmap(dev_t dev, off_t off, int prot) 396 { 397 struct cgthree_softc *sc = device_lookup_private(&cgthree_cd, 398 minor(dev)); 399 400 #define START (128*1024 + 128*1024) 401 #define NOOVERLAY (0x04000000) 402 403 if (off & PGOFSET) 404 panic("cgthreemmap"); 405 if (off < 0) 406 return (-1); 407 if ((u_int)off >= NOOVERLAY) 408 off -= NOOVERLAY; 409 else if ((u_int)off >= START) 410 off -= START; 411 else 412 off = 0; 413 414 if (off >= sc->sc_fb.fb_type.fb_size) 415 return (-1); 416 417 return (bus_space_mmap(sc->sc_bustag, 418 sc->sc_paddr, CG3REG_MEM + off, 419 prot, BUS_SPACE_MAP_LINEAR)); 420 } 421 422 #if NWSDISPLAY > 0 423 424 static void 425 cg3_setup_palette(struct cgthree_softc *sc) 426 { 427 int i, j; 428 429 j = 0; 430 for (i = 0; i < 256; i++) { 431 sc->sc_cmap.cm_map[i][0] = rasops_cmap[j]; 432 j++; 433 sc->sc_cmap.cm_map[i][1] = rasops_cmap[j]; 434 j++; 435 sc->sc_cmap.cm_map[i][2] = rasops_cmap[j]; 436 j++; 437 } 438 cgthreeloadcmap(sc, 0, 256); 439 } 440 441 int 442 cgthree_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, 443 struct lwp *l) 444 { 445 /* we'll probably need to add more stuff here */ 446 struct vcons_data *vd = v; 447 struct cgthree_softc *sc = vd->cookie; 448 struct wsdisplay_fbinfo *wdf; 449 struct rasops_info *ri = &sc->sc_fb.fb_rinfo; 450 struct vcons_screen *ms = sc->vd.active; 451 switch (cmd) { 452 case WSDISPLAYIO_GTYPE: 453 *(u_int *)data = WSDISPLAY_TYPE_SUNTCX; 454 return 0; 455 case WSDISPLAYIO_GINFO: 456 wdf = (void *)data; 457 wdf->height = ri->ri_height; 458 wdf->width = ri->ri_width; 459 wdf->depth = ri->ri_depth; 460 wdf->cmsize = 256; 461 return 0; 462 463 case WSDISPLAYIO_GETCMAP: 464 return cgthree_getcmap(sc, 465 (struct wsdisplay_cmap *)data); 466 case WSDISPLAYIO_PUTCMAP: 467 return cgthree_putcmap(sc, 468 (struct wsdisplay_cmap *)data); 469 470 case WSDISPLAYIO_SMODE: 471 { 472 int new_mode = *(int*)data; 473 if (new_mode != sc->sc_mode) 474 { 475 sc->sc_mode = new_mode; 476 if(new_mode == WSDISPLAYIO_MODE_EMUL) 477 { 478 cg3_setup_palette(sc); 479 vcons_redraw_screen(ms); 480 } 481 } 482 } 483 } 484 return EPASSTHROUGH; 485 } 486 487 paddr_t 488 cgthree_mmap(void *v, void *vs, off_t offset, int prot) 489 { 490 /* I'm not at all sure this is the right thing to do */ 491 return cgthreemmap(0, offset, prot); /* assume minor dev 0 for now */ 492 } 493 494 int 495 cgthree_putcmap(struct cgthree_softc *sc, struct wsdisplay_cmap *cm) 496 { 497 u_int index = cm->index; 498 u_int count = cm->count; 499 int error,i; 500 if (index >= 256 || count > 256 || index + count > 256) 501 return EINVAL; 502 503 for (i = 0; i < count; i++) 504 { 505 error = copyin(&cm->red[i], 506 &sc->sc_cmap.cm_map[index + i][0], 1); 507 if (error) 508 return error; 509 error = copyin(&cm->green[i], 510 &sc->sc_cmap.cm_map[index + i][1], 511 1); 512 if (error) 513 return error; 514 error = copyin(&cm->blue[i], 515 &sc->sc_cmap.cm_map[index + i][2], 1); 516 if (error) 517 return error; 518 } 519 cgthreeloadcmap(sc, index, count); 520 521 return 0; 522 } 523 524 int 525 cgthree_getcmap(struct cgthree_softc *sc, struct wsdisplay_cmap *cm) 526 { 527 u_int index = cm->index; 528 u_int count = cm->count; 529 int error,i; 530 531 if (index >= 256 || count > 256 || index + count > 256) 532 return EINVAL; 533 534 for (i = 0; i < count; i++) 535 { 536 error = copyout(&sc->sc_cmap.cm_map[index + i][0], 537 &cm->red[i], 1); 538 if (error) 539 return error; 540 error = copyout(&sc->sc_cmap.cm_map[index + i][1], 541 &cm->green[i], 1); 542 if (error) 543 return error; 544 error = copyout(&sc->sc_cmap.cm_map[index + i][2], 545 &cm->blue[i], 1); 546 if (error) 547 return error; 548 } 549 550 return 0; 551 } 552 553 void 554 cgthree_init_screen(void *cookie, struct vcons_screen *scr, 555 int existing, long *defattr) 556 { 557 struct cgthree_softc *sc = cookie; 558 struct rasops_info *ri = &scr->scr_ri; 559 560 scr->scr_flags |= VCONS_DONT_READ; 561 562 ri->ri_depth = 8; 563 ri->ri_width = sc->sc_width; 564 ri->ri_height = sc->sc_height; 565 ri->ri_stride = sc->sc_stride; 566 ri->ri_flg = RI_CENTER; 567 568 ri->ri_bits = sc->sc_fb.fb_pixels; 569 570 rasops_init(ri, 0, 0); 571 ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_REVERSE; 572 rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight, 573 sc->sc_width / ri->ri_font->fontwidth); 574 575 ri->ri_hw = scr; 576 } 577 578 #endif /* NWSDISPLAY > 0 */ 579