1 /* $NetBSD: tcx.c,v 1.58 2018/01/24 05:35:58 riastradh Exp $ */ 2 3 /* 4 * Copyright (c) 1996, 1998, 2009 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 and Michael Lorenz. 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 (TCX) 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: tcx.c,v 1.58 2018/01/24 05:35:58 riastradh 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 #ifdef DEBUG 54 #include <sys/proc.h> 55 #include <sys/syslog.h> 56 #endif 57 58 #include <sys/bus.h> 59 #include <machine/autoconf.h> 60 61 #include <dev/sun/fbio.h> 62 #include <dev/sun/fbvar.h> 63 #include <dev/sun/btreg.h> 64 #include <dev/sun/btvar.h> 65 66 #include <dev/sbus/sbusvar.h> 67 #include <dev/sbus/tcxreg.h> 68 69 #include <dev/wscons/wsdisplayvar.h> 70 #include <dev/wscons/wsconsio.h> 71 #include <dev/wsfont/wsfont.h> 72 #include <dev/rasops/rasops.h> 73 74 #include <dev/wscons/wsdisplay_vconsvar.h> 75 76 #include "opt_wsemul.h" 77 78 #include "ioconf.h" 79 80 /* per-display variables */ 81 struct tcx_softc { 82 device_t sc_dev; /* base device */ 83 struct fbdevice sc_fb; /* frame buffer device */ 84 bus_space_tag_t sc_bustag; 85 struct openprom_addr sc_physaddr[TCX_NREG];/* phys addr of h/w */ 86 87 bus_space_handle_t sc_bt; /* Brooktree registers */ 88 bus_space_handle_t sc_thc; /* THC registers */ 89 uint8_t *sc_fbaddr; /* framebuffer */ 90 volatile uint64_t *sc_rblit; /* blitspace */ 91 volatile uint64_t *sc_rstip; /* stipple space */ 92 93 short sc_8bit; /* true if 8-bit hardware */ 94 short sc_blanked; /* true if blanked */ 95 uint32_t sc_fbsize; /* size of the 8bit fb */ 96 u_char sc_cmap_red[256]; 97 u_char sc_cmap_green[256]; 98 u_char sc_cmap_blue[256]; 99 int sc_mode, sc_bg; 100 int sc_cursor_x, sc_cursor_y; 101 int sc_hotspot_x, sc_hotspot_y; 102 struct vcons_data vd; 103 }; 104 105 static struct vcons_screen tcx_console_screen; 106 107 extern const u_char rasops_cmap[768]; 108 109 struct wsscreen_descr tcx_defscreendesc = { 110 "default", 111 0, 0, 112 NULL, 113 8, 16, 114 WSSCREEN_WSCOLORS, 115 }; 116 117 const struct wsscreen_descr *_tcx_scrlist[] = { 118 &tcx_defscreendesc, 119 /* XXX other formats, graphics screen? */ 120 }; 121 122 struct wsscreen_list tcx_screenlist = { 123 sizeof(_tcx_scrlist) / sizeof(struct wsscreen_descr *), 124 _tcx_scrlist 125 }; 126 127 /* autoconfiguration driver */ 128 static void tcxattach(device_t, device_t, void *); 129 static int tcxmatch(device_t, cfdata_t, void *); 130 static void tcx_unblank(device_t); 131 132 CFATTACH_DECL_NEW(tcx, sizeof(struct tcx_softc), 133 tcxmatch, tcxattach, NULL, NULL); 134 135 dev_type_open(tcxopen); 136 dev_type_close(tcxclose); 137 dev_type_ioctl(tcxioctl); 138 dev_type_mmap(tcxmmap); 139 140 const struct cdevsw tcx_cdevsw = { 141 .d_open = tcxopen, 142 .d_close = tcxclose, 143 .d_read = noread, 144 .d_write = nowrite, 145 .d_ioctl = tcxioctl, 146 .d_stop = nostop, 147 .d_tty = notty, 148 .d_poll = nopoll, 149 .d_mmap = tcxmmap, 150 .d_kqfilter = nokqfilter, 151 .d_discard = nodiscard, 152 .d_flag = 0 153 }; 154 155 /* frame buffer generic driver */ 156 static struct fbdriver tcx_fbdriver = { 157 tcx_unblank, tcxopen, tcxclose, tcxioctl, nopoll, tcxmmap, 158 nokqfilter 159 }; 160 161 static void tcx_reset(struct tcx_softc *); 162 static void tcx_loadcmap(struct tcx_softc *, int, int); 163 164 static int tcx_ioctl(void *, void *, u_long, void *, int, struct lwp *); 165 static paddr_t tcx_mmap(void *, void *, off_t, int); 166 167 static void tcx_init_cmap(struct tcx_softc *); 168 static void tcx_init_screen(void *, struct vcons_screen *, int, long *); 169 static void tcx_clearscreen(struct tcx_softc *, int); 170 static void tcx_copyrows(void *, int, int, int); 171 static void tcx_eraserows(void *, int, int, long); 172 static void tcx_putchar(void *, int, int, u_int, long); 173 static void tcx_set_video(struct tcx_softc *, int); 174 static int tcx_do_cursor(struct tcx_softc *, struct wsdisplay_cursor *); 175 static void tcx_set_cursor(struct tcx_softc *); 176 177 struct wsdisplay_accessops tcx_accessops = { 178 tcx_ioctl, 179 tcx_mmap, 180 NULL, /* vcons_alloc_screen */ 181 NULL, /* vcons_free_screen */ 182 NULL, /* vcons_show_screen */ 183 NULL, /* load_font */ 184 NULL, /* polls */ 185 NULL, /* scroll */ 186 }; 187 188 #define OBPNAME "SUNW,tcx" 189 190 /* 191 * Match a tcx. 192 */ 193 int 194 tcxmatch(device_t parent, cfdata_t cf, void *aux) 195 { 196 struct sbus_attach_args *sa = aux; 197 198 if (strcmp(sa->sa_name, OBPNAME) == 0) 199 return 100; /* beat genfb */ 200 return 0; 201 } 202 203 /* 204 * Attach a display. 205 */ 206 void 207 tcxattach(device_t parent, device_t self, void *args) 208 { 209 struct tcx_softc *sc = device_private(self); 210 struct sbus_attach_args *sa = args; 211 struct wsemuldisplaydev_attach_args aa; 212 struct rasops_info *ri; 213 unsigned long defattr; 214 int node; 215 struct fbdevice *fb = &sc->sc_fb; 216 bus_space_handle_t bh; 217 int isconsole; 218 uint32_t confreg; 219 220 sc->sc_dev = self; 221 sc->sc_bustag = sa->sa_bustag; 222 node = sa->sa_node; 223 224 sc->sc_cursor_x = 0x7fff; 225 sc->sc_cursor_y = 0x7fff; 226 sc->sc_hotspot_x = 0; 227 sc->sc_hotspot_y = 0; 228 229 fb->fb_driver = &tcx_fbdriver; 230 fb->fb_device = sc->sc_dev; 231 /* Mask out invalid flags from the user. */ 232 fb->fb_flags = device_cfdata(sc->sc_dev)->cf_flags & FB_USERMASK; 233 /* 234 * The onboard framebuffer on the SS4 supports only 8-bit mode; 235 * it can be distinguished from the S24 card for the SS5 by the 236 * presence of the "tcx-8-bit" attribute on the SS4 version. 237 */ 238 sc->sc_8bit = node_has_property(node, "tcx-8-bit"); 239 fb->fb_type.fb_depth = 8; 240 fb_setsize_obp(fb, fb->fb_type.fb_depth, 1152, 900, node); 241 242 /* 243 * actual FB size ( of the 8bit region ) 244 * no reason to restrict userland mappings to the visible VRAM 245 */ 246 if (sc->sc_8bit) { 247 aprint_normal(" (8-bit only TCX)\n"); 248 /* at least the SS4 can have 2MB with a VSIMM */ 249 sc->sc_fbsize = 0x100000 * prom_getpropint(node, "vram", 1); 250 } else { 251 aprint_normal(" (S24)\n"); 252 /* all S24 I know of have 4MB, non-expandable */ 253 sc->sc_fbsize = 0x100000; 254 } 255 256 fb->fb_type.fb_cmsize = 256; 257 fb->fb_type.fb_size = sc->sc_fbsize; /* later code assumes 8bit */ 258 aprint_normal_dev(self, "%s, %d x %d\n", OBPNAME, 259 fb->fb_type.fb_width, 260 fb->fb_type.fb_height); 261 262 fb->fb_type.fb_type = FBTYPE_TCXCOLOR; 263 264 if (sa->sa_nreg != TCX_NREG) { 265 aprint_error("\n"); 266 aprint_error_dev(self, "only %d register sets\n", 267 sa->sa_nreg); 268 return; 269 } 270 if (sa->sa_reg[TCX_REG_STIP].oa_size < 0x1000) { 271 aprint_error("\n"); 272 aprint_error_dev(self, "STIP register too small (0x%x)\n", 273 sa->sa_reg[TCX_REG_STIP].oa_size); 274 return; 275 } 276 277 memcpy(sc->sc_physaddr, sa->sa_reg, 278 sa->sa_nreg * sizeof(struct openprom_addr)); 279 280 /* Map the register banks we care about */ 281 if (sbus_bus_map(sa->sa_bustag, 282 sc->sc_physaddr[TCX_REG_THC].oa_space, 283 sc->sc_physaddr[TCX_REG_THC].oa_base, 284 0x1000, 285 BUS_SPACE_MAP_LINEAR, &sc->sc_thc) != 0) { 286 aprint_error_dev(self, 287 "tcxattach: cannot map thc registers\n"); 288 return; 289 } 290 291 if (sbus_bus_map(sa->sa_bustag, 292 sc->sc_physaddr[TCX_REG_CMAP].oa_space, 293 sc->sc_physaddr[TCX_REG_CMAP].oa_base, 294 0x1000, 295 BUS_SPACE_MAP_LINEAR, &sc->sc_bt) != 0) { 296 aprint_error_dev(self, "tcxattach: cannot map DAC registers\n"); 297 return; 298 } 299 300 /* map the 8bit dumb FB for the console */ 301 if (sbus_bus_map(sa->sa_bustag, 302 sc->sc_physaddr[TCX_REG_DFB8].oa_space, 303 sc->sc_physaddr[TCX_REG_DFB8].oa_base, 304 sc->sc_fbsize, 305 BUS_SPACE_MAP_LINEAR, 306 &bh) != 0) { 307 aprint_error_dev(self, "tcxattach: cannot map framebuffer\n"); 308 return; 309 } 310 sc->sc_fbaddr = bus_space_vaddr(sa->sa_bustag, bh); 311 312 /* 313 * 8bit tcx has the RSTIP and RBLIT ranges set to size 0. 314 * On Real Hardware they work anyway ( on my SS4 at least ) but 315 * emulators may not be so forgiving. 316 */ 317 if (sc->sc_8bit) { 318 /* BLIT space */ 319 if (sbus_bus_map(sa->sa_bustag, 320 sc->sc_physaddr[TCX_REG_BLIT].oa_space, 321 sc->sc_physaddr[TCX_REG_BLIT].oa_base, 322 sc->sc_fbsize << 3, 323 BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_LARGE, 324 &bh) != 0) { 325 aprint_error_dev(self, 326 "tcxattach: cannot map BLIT space\n"); 327 return; 328 } 329 sc->sc_rblit = bus_space_vaddr(sa->sa_bustag, bh); 330 331 /* STIP space */ 332 if (sbus_bus_map(sa->sa_bustag, 333 sc->sc_physaddr[TCX_REG_STIP].oa_space, 334 sc->sc_physaddr[TCX_REG_STIP].oa_base, 335 sc->sc_fbsize << 3, 336 BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_LARGE, 337 &bh) != 0) { 338 aprint_error_dev(self, 339 "tcxattach: cannot map STIP space\n"); 340 return; 341 } 342 sc->sc_rstip = bus_space_vaddr(sa->sa_bustag, bh); 343 } else { 344 /* RBLIT space */ 345 if (sbus_bus_map(sa->sa_bustag, 346 sc->sc_physaddr[TCX_REG_RBLIT].oa_space, 347 sc->sc_physaddr[TCX_REG_RBLIT].oa_base, 348 sc->sc_fbsize << 3, 349 BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_LARGE, 350 &bh) != 0) { 351 aprint_error_dev(self, 352 "tcxattach: cannot map RBLIT space\n"); 353 return; 354 } 355 sc->sc_rblit = bus_space_vaddr(sa->sa_bustag, bh); 356 357 /* RSTIP space */ 358 if (sbus_bus_map(sa->sa_bustag, 359 sc->sc_physaddr[TCX_REG_RSTIP].oa_space, 360 sc->sc_physaddr[TCX_REG_RSTIP].oa_base, 361 sc->sc_fbsize << 3, 362 BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_LARGE, 363 &bh) != 0) { 364 aprint_error_dev(self, 365 "tcxattach: cannot map RSTIP space\n"); 366 return; 367 } 368 sc->sc_rstip = bus_space_vaddr(sa->sa_bustag, bh); 369 } 370 isconsole = fb_is_console(node); 371 372 confreg = bus_space_read_4(sa->sa_bustag, sc->sc_thc, THC_CONFIG); 373 aprint_normal_dev(self, "id %d, rev %d, sense %d\n", 374 (confreg & THC_CFG_FBID) >> THC_CFG_FBID_SHIFT, 375 (confreg & THC_CFG_REV) >> THC_CFG_REV_SHIFT, 376 (confreg & THC_CFG_SENSE) >> THC_CFG_SENSE_SHIFT 377 ); 378 379 /* reset cursor & frame buffer controls */ 380 tcx_reset(sc); 381 382 if (!sc->sc_8bit) 383 tcx_set_cursor(sc); 384 385 /* enable video */ 386 confreg = bus_space_read_4(sa->sa_bustag, sc->sc_thc, THC_MISC); 387 confreg |= THC_MISC_VIDEN; 388 bus_space_write_4(sa->sa_bustag, sc->sc_thc, THC_MISC, confreg); 389 390 if (isconsole) { 391 aprint_error_dev(self, "(console)\n"); 392 } 393 394 fb_attach(&sc->sc_fb, isconsole); 395 396 sc->sc_mode = WSDISPLAYIO_MODE_EMUL; 397 wsfont_init(); 398 399 vcons_init(&sc->vd, sc, &tcx_defscreendesc, &tcx_accessops); 400 sc->vd.init_screen = tcx_init_screen; 401 402 vcons_init_screen(&sc->vd, &tcx_console_screen, 1, &defattr); 403 tcx_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC; 404 405 ri = &tcx_console_screen.scr_ri; 406 407 sc->sc_bg = ri->ri_devcmap[(defattr >> 16) & 0xff]; 408 tcx_clearscreen(sc, 0); 409 tcx_init_cmap(sc); 410 411 tcx_defscreendesc.nrows = ri->ri_rows; 412 tcx_defscreendesc.ncols = ri->ri_cols; 413 tcx_defscreendesc.textops = &ri->ri_ops; 414 tcx_defscreendesc.capabilities = ri->ri_caps; 415 416 if(isconsole) { 417 wsdisplay_cnattach(&tcx_defscreendesc, ri, 0, 0, defattr); 418 vcons_replay_msgbuf(&tcx_console_screen); 419 } 420 421 aa.console = isconsole; 422 aa.scrdata = &tcx_screenlist; 423 aa.accessops = &tcx_accessops; 424 aa.accesscookie = &sc->vd; 425 426 config_found(self, &aa, wsemuldisplaydevprint); 427 /* 428 * we need to do this again - something overwrites a handful 429 * palette registers and we end up with white in reg. 0 430 */ 431 tcx_loadcmap(sc, 0, 256); 432 } 433 434 int 435 tcxopen(dev_t dev, int flags, int mode, struct lwp *l) 436 { 437 return (0); 438 } 439 440 int 441 tcxclose(dev_t dev, int flags, int mode, struct lwp *l) 442 { 443 struct tcx_softc *sc = device_lookup_private(&tcx_cd, minor(dev)); 444 445 tcx_reset(sc); 446 /* we may want to clear and redraw the console here */ 447 return (0); 448 } 449 450 int 451 tcxioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l) 452 { 453 struct tcx_softc *sc = device_lookup_private(&tcx_cd, minor(dev)); 454 455 switch (cmd) { 456 457 case FBIOGTYPE: 458 *(struct fbtype *)data = sc->sc_fb.fb_type; 459 break; 460 461 case FBIOGATTR: 462 #define fba ((struct fbgattr *)data) 463 fba->real_type = sc->sc_fb.fb_type.fb_type; 464 fba->owner = 0; /* XXX ??? */ 465 fba->fbtype = sc->sc_fb.fb_type; 466 fba->sattr.flags = 0; 467 fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type; 468 fba->sattr.dev_specific[0] = -1; 469 fba->emu_types[0] = sc->sc_fb.fb_type.fb_type; 470 fba->emu_types[1] = FBTYPE_SUN3COLOR; 471 fba->emu_types[2] = -1; 472 #undef fba 473 break; 474 475 case FBIOGETCMAP: 476 #define p ((struct fbcmap *)data) 477 if (p->index > 256 || p->count > 256 - p->index) 478 return EINVAL; 479 if (copyout(&sc->sc_cmap_red[p->index], p->red, p->count) != 0) 480 return EINVAL; 481 if (copyout(&sc->sc_cmap_green[p->index], p->green, p->count) 482 != 0) 483 return EINVAL; 484 if (copyout(&sc->sc_cmap_blue[p->index], p->blue, p->count) 485 != 0) 486 return EINVAL; 487 return 0; 488 489 case FBIOPUTCMAP: 490 /* copy to software map */ 491 if (p->index > 256 || p->count > 256 - p->index) 492 return EINVAL; 493 if (copyin(p->red, &sc->sc_cmap_red[p->index], p->count) != 0) 494 return EINVAL; 495 if (copyin(p->green, &sc->sc_cmap_green[p->index], p->count) 496 != 0) 497 return EINVAL; 498 if (copyin(p->blue, &sc->sc_cmap_blue[p->index], p->count) != 0) 499 return EINVAL; 500 tcx_loadcmap(sc, p->index, p->count); 501 #undef p 502 break; 503 case FBIOGVIDEO: 504 *(int *)data = sc->sc_blanked; 505 break; 506 507 case FBIOSVIDEO: 508 tcx_set_video(sc, *(int *)data); 509 break; 510 511 default: 512 #ifdef DEBUG 513 log(LOG_NOTICE, "tcxioctl(0x%lx) (%s[%d])\n", cmd, 514 l->l_proc->p_comm, l->l_proc->p_pid); 515 #endif 516 return (ENOTTY); 517 } 518 return (0); 519 } 520 521 /* 522 * Clean up hardware state (e.g., after bootup or after X crashes). 523 */ 524 static void 525 tcx_reset(struct tcx_softc *sc) 526 { 527 uint32_t reg; 528 529 reg = bus_space_read_4(sc->sc_bustag, sc->sc_thc, THC_MISC); 530 reg |= THC_MISC_CURSRES; 531 bus_space_write_4(sc->sc_bustag, sc->sc_thc, THC_MISC, reg); 532 } 533 534 static void 535 tcx_init_cmap(struct tcx_softc *sc) 536 { 537 int i, j; 538 539 /* Initialize the default color map. */ 540 j = 0; 541 for (i = 0; i < 256; i++) { 542 543 sc->sc_cmap_red[i] = rasops_cmap[j]; 544 sc->sc_cmap_green[i] = rasops_cmap[j + 1]; 545 sc->sc_cmap_blue[i] = rasops_cmap[j + 2]; 546 j += 3; 547 } 548 tcx_loadcmap(sc, 0, 256); 549 } 550 551 static void 552 tcx_loadcmap(struct tcx_softc *sc, int start, int ncolors) 553 { 554 int i; 555 556 for (i = 0; i < ncolors; i++) { 557 bus_space_write_4(sc->sc_bustag, sc->sc_bt, DAC_ADDRESS, 558 (start + i) << 24); 559 bus_space_write_4(sc->sc_bustag, sc->sc_bt, DAC_FB_LUT, 560 sc->sc_cmap_red[i + start] << 24); 561 bus_space_write_4(sc->sc_bustag, sc->sc_bt, DAC_FB_LUT, 562 sc->sc_cmap_green[i + start] << 24); 563 bus_space_write_4(sc->sc_bustag, sc->sc_bt, DAC_FB_LUT, 564 sc->sc_cmap_blue[i + start] << 24); 565 } 566 bus_space_write_4(sc->sc_bustag, sc->sc_bt, DAC_ADDRESS, 0); 567 } 568 569 static void 570 tcx_unblank(device_t dev) 571 { 572 struct tcx_softc *sc = device_private(dev); 573 574 if (sc->sc_blanked) { 575 uint32_t reg; 576 sc->sc_blanked = 0; 577 reg = bus_space_read_4(sc->sc_bustag, sc->sc_thc, THC_MISC); 578 reg &= ~THC_MISC_VSYNC_DISABLE; 579 reg &= ~THC_MISC_HSYNC_DISABLE; 580 reg |= THC_MISC_VIDEN; 581 bus_space_write_4(sc->sc_bustag, sc->sc_thc, THC_MISC, reg); 582 } 583 } 584 585 static void 586 tcx_set_video(struct tcx_softc *sc, int unblank) 587 { 588 uint32_t reg; 589 if (unblank) { 590 sc->sc_blanked = 0; 591 reg = bus_space_read_4(sc->sc_bustag, sc->sc_thc, THC_MISC); 592 reg &= ~THC_MISC_VSYNC_DISABLE; 593 reg &= ~THC_MISC_HSYNC_DISABLE; 594 reg |= THC_MISC_VIDEN; 595 bus_space_write_4(sc->sc_bustag, sc->sc_thc, THC_MISC, reg); 596 } else { 597 sc->sc_blanked = 1; 598 reg = bus_space_read_4(sc->sc_bustag, sc->sc_thc, THC_MISC); 599 reg |= THC_MISC_VSYNC_DISABLE; 600 reg |= THC_MISC_HSYNC_DISABLE; 601 reg &= ~THC_MISC_VIDEN; 602 bus_space_write_4(sc->sc_bustag, sc->sc_thc, THC_MISC, reg); 603 } 604 } 605 606 /* 607 * Base addresses at which users can mmap() the various pieces of a tcx. 608 */ 609 #define TCX_USER_RAM 0x00000000 610 #define TCX_USER_RAM24 0x01000000 611 #define TCX_USER_RAM_COMPAT 0x04000000 /* cg3 emulation */ 612 #define TCX_USER_STIP 0x10000000 613 #define TCX_USER_BLIT 0x20000000 614 #define TCX_USER_RDFB32 0x28000000 615 #define TCX_USER_RSTIP 0x30000000 616 #define TCX_USER_RBLIT 0x38000000 617 #define TCX_USER_TEC 0x70001000 618 #define TCX_USER_BTREGS 0x70002000 619 #define TCX_USER_THC 0x70004000 620 #define TCX_USER_DHC 0x70008000 621 #define TCX_USER_ALT 0x7000a000 622 #define TCX_USER_UART 0x7000c000 623 #define TCX_USER_VRT 0x7000e000 624 #define TCX_USER_ROM 0x70010000 625 626 struct mmo { 627 u_int mo_uaddr; /* user (virtual) address */ 628 u_int mo_size; /* size, or 0 for video ram size */ 629 u_int mo_bank; /* register bank number */ 630 }; 631 632 /* 633 * Return the address that would map the given device at the given 634 * offset, allowing for the given protection, or return -1 for error. 635 * 636 * XXX needs testing against `demanding' applications (e.g., aviator) 637 */ 638 paddr_t 639 tcxmmap(dev_t dev, off_t off, int prot) 640 { 641 struct tcx_softc *sc = device_lookup_private(&tcx_cd, minor(dev)); 642 struct openprom_addr *rr = sc->sc_physaddr; 643 struct mmo *mo, *mo_end; 644 u_int u, sz; 645 static struct mmo mmo[] = { 646 { TCX_USER_RAM, 0, TCX_REG_DFB8 }, 647 { TCX_USER_RAM24, 0, TCX_REG_DFB24 }, 648 { TCX_USER_RAM_COMPAT, 0, TCX_REG_DFB8 }, 649 650 { TCX_USER_STIP, 1, TCX_REG_STIP }, 651 { TCX_USER_BLIT, 1, TCX_REG_BLIT }, 652 { TCX_USER_RDFB32, 0, TCX_REG_RDFB32 }, 653 { TCX_USER_RSTIP, 1, TCX_REG_RSTIP }, 654 { TCX_USER_RBLIT, 1, TCX_REG_RBLIT }, 655 { TCX_USER_TEC, 1, TCX_REG_TEC }, 656 { TCX_USER_BTREGS, 8192 /* XXX */, TCX_REG_CMAP }, 657 { TCX_USER_THC, 0x2000, TCX_REG_THC }, 658 { TCX_USER_DHC, 1, TCX_REG_DHC }, 659 { TCX_USER_ALT, 1, TCX_REG_ALT }, 660 { TCX_USER_ROM, 65536, TCX_REG_ROM }, 661 }; 662 #define NMMO (sizeof mmo / sizeof *mmo) 663 664 if (off & PGOFSET) 665 panic("tcxmmap"); 666 667 /* 668 * Entries with size 0 map video RAM (i.e., the size in fb data). 669 * Entries that map 32-bit deep regions are adjusted for their 670 * depth (fb_size gives the size of the 8-bit-deep region). 671 * 672 * Since we work in pages, the fact that the map offset table's 673 * sizes are sometimes bizarre (e.g., 1) is effectively ignored: 674 * one byte is as good as one page. 675 */ 676 677 mo = mmo; 678 mo_end = &mmo[NMMO]; 679 680 for (; mo < mo_end; mo++) { 681 if ((u_int)off < mo->mo_uaddr) 682 continue; 683 684 u = off - mo->mo_uaddr; 685 sz = mo->mo_size; 686 687 if (sz == 0) { 688 sz = sc->sc_fb.fb_type.fb_size; 689 /* 690 * check for the 32-bit-deep regions and adjust 691 * accordingly 692 */ 693 if (mo->mo_uaddr == TCX_USER_RAM24 || 694 mo->mo_uaddr == TCX_USER_RDFB32) { 695 if (sc->sc_8bit) { 696 /* 697 * not present on 8-bit hardware 698 */ 699 continue; 700 } 701 sz *= 4; 702 } 703 } 704 if (sz == 1) 705 sz = rr[mo->mo_bank].oa_size; 706 707 if (u < sz) { 708 return (bus_space_mmap(sc->sc_bustag, 709 BUS_ADDR(rr[mo->mo_bank].oa_space, 710 rr[mo->mo_bank].oa_base), 711 u, 712 prot, 713 BUS_SPACE_MAP_LINEAR)); 714 } 715 } 716 return (-1); 717 } 718 719 int 720 tcx_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, 721 struct lwp *l) 722 { 723 struct vcons_data *vd = v; 724 struct tcx_softc *sc = vd->cookie; 725 struct wsdisplay_fbinfo *wdf; 726 struct vcons_screen *ms = vd->active; 727 728 switch (cmd) { 729 case WSDISPLAYIO_GTYPE: 730 *(u_int *)data = WSDISPLAY_TYPE_SUNTCX; 731 return 0; 732 733 case FBIOGVIDEO: 734 case WSDISPLAYIO_GVIDEO: 735 *(int *)data = !sc->sc_blanked; 736 return 0; 737 738 case WSDISPLAYIO_SVIDEO: 739 case FBIOSVIDEO: 740 tcx_set_video(sc, *(int *)data); 741 return 0; 742 743 case WSDISPLAYIO_GINFO: 744 wdf = (void *)data; 745 wdf->height = ms->scr_ri.ri_height; 746 wdf->width = ms->scr_ri.ri_width; 747 if (sc->sc_8bit) { 748 wdf->depth = 8; 749 } else { 750 wdf->depth = 32; 751 } 752 wdf->cmsize = 256; 753 return 0; 754 case WSDISPLAYIO_LINEBYTES: 755 { 756 int *ret = (int *)data; 757 *ret = sc->sc_8bit ? ms->scr_ri.ri_width : 758 ms->scr_ri.ri_width << 2; 759 } 760 return 0; 761 #if 0 762 case WSDISPLAYIO_GETCMAP: 763 return tcx_getcmap(sc, (struct wsdisplay_cmap *)data); 764 765 case WSDISPLAYIO_PUTCMAP: 766 return tcx_putcmap(sc, (struct wsdisplay_cmap *)data); 767 #endif 768 case WSDISPLAYIO_SMODE: 769 { 770 int new_mode = *(int*)data; 771 if (new_mode != sc->sc_mode) 772 { 773 sc->sc_mode = new_mode; 774 if (new_mode == WSDISPLAYIO_MODE_EMUL) 775 { 776 tcx_init_cmap(sc); 777 tcx_clearscreen(sc, 0); 778 vcons_redraw_screen(ms); 779 } else if (!sc->sc_8bit) 780 tcx_clearscreen(sc, 3); 781 } 782 } 783 return 0; 784 785 case WSDISPLAYIO_GCURPOS: 786 if (sc->sc_8bit) { 787 return EOPNOTSUPP; 788 } else { 789 struct wsdisplay_curpos *cp = (void *)data; 790 791 cp->x = sc->sc_cursor_x; 792 cp->y = sc->sc_cursor_y; 793 } 794 return 0; 795 796 case WSDISPLAYIO_SCURPOS: 797 if (sc->sc_8bit) { 798 return EOPNOTSUPP; 799 } else { 800 struct wsdisplay_curpos *cp = (void *)data; 801 802 sc->sc_cursor_x = cp->x; 803 sc->sc_cursor_y = cp->y; 804 tcx_set_cursor(sc); 805 } 806 return 0; 807 808 case WSDISPLAYIO_GCURMAX: 809 if (sc->sc_8bit) { 810 return EOPNOTSUPP; 811 } else { 812 struct wsdisplay_curpos *cp = (void *)data; 813 814 cp->x = 32; 815 cp->y = 32; 816 } 817 return 0; 818 819 case WSDISPLAYIO_SCURSOR: 820 if (sc->sc_8bit) { 821 return EOPNOTSUPP; 822 } else { 823 struct wsdisplay_cursor *cursor = (void *)data; 824 825 return tcx_do_cursor(sc, cursor); 826 } 827 } 828 return EPASSTHROUGH; 829 } 830 831 static paddr_t 832 tcx_mmap(void *v, void *vs, off_t offset, int prot) 833 { 834 struct vcons_data *vd = v; 835 struct tcx_softc *sc = vd->cookie; 836 837 /* 'regular' framebuffer mmap()ing */ 838 if (sc->sc_8bit) { 839 /* on 8Bit boards hand over the 8 bit aperture */ 840 if (offset > sc->sc_fbsize) 841 return -1; 842 return bus_space_mmap(sc->sc_bustag, 843 sc->sc_physaddr[TCX_REG_DFB8].oa_base + offset, 0, prot, 844 BUS_SPACE_MAP_LINEAR); 845 } else { 846 /* ... but if we have a 24bit aperture we use it */ 847 if (offset > sc->sc_fbsize * 4) 848 return -1; 849 return bus_space_mmap(sc->sc_bustag, 850 sc->sc_physaddr[TCX_REG_DFB24].oa_base + offset, 0, prot, 851 BUS_SPACE_MAP_LINEAR); 852 } 853 return -1; 854 } 855 856 static void 857 tcx_init_screen(void *cookie, struct vcons_screen *scr, 858 int existing, long *defattr) 859 { 860 struct tcx_softc *sc = cookie; 861 struct rasops_info *ri = &scr->scr_ri; 862 863 ri->ri_depth = 8; 864 ri->ri_width = sc->sc_fb.fb_type.fb_width; 865 ri->ri_height = sc->sc_fb.fb_type.fb_height; 866 ri->ri_stride = sc->sc_fb.fb_linebytes; 867 ri->ri_flg = RI_CENTER | RI_FULLCLEAR; 868 869 ri->ri_bits = sc->sc_fbaddr; 870 871 rasops_init(ri, 0, 0); 872 ri->ri_caps = WSSCREEN_WSCOLORS; 873 rasops_reconfig(ri, ri->ri_height / ri->ri_font->fontheight, 874 ri->ri_width / ri->ri_font->fontwidth); 875 876 /* enable acceleration */ 877 ri->ri_ops.copyrows = tcx_copyrows; 878 ri->ri_ops.eraserows = tcx_eraserows; 879 ri->ri_ops.putchar = tcx_putchar; 880 #if 0 881 ri->ri_ops.cursor = tcx_cursor; 882 ri->ri_ops.copycols = tcx_copycols; 883 ri->ri_ops.erasecols = tcx_erasecols; 884 #endif 885 } 886 887 static void 888 tcx_clearscreen(struct tcx_softc *sc, int spc) 889 { 890 /* ROP in the upper 4bit is necessary, tcx actually uses it */ 891 volatile uint64_t bg = 0x30000000ffffffffLL; 892 volatile uint64_t spc64; 893 int i, len; 894 895 spc64 = ((spc & 3) << 24); 896 bg |= (spc64 << 32); 897 898 len = sc->sc_fb.fb_type.fb_width * sc->sc_fb.fb_type.fb_height; 899 for (i = 0; i < len; i += 32) 900 sc->sc_rstip[i] = bg; 901 } 902 903 static void 904 tcx_copyrows(void *cookie, int srcrow, int dstrow, int nrows) 905 { 906 struct rasops_info *ri = cookie; 907 struct vcons_screen *scr = ri->ri_hw; 908 struct tcx_softc *sc = scr->scr_cookie; 909 int i, last, first, len, dest, leftover; 910 911 i = ri->ri_width * ri->ri_font->fontheight * nrows; 912 len = i & 0xffffe0; 913 leftover = i & 0x1f; 914 if (srcrow < dstrow) { 915 /* we must go bottom to top */ 916 first = ri->ri_width * 917 (ri->ri_font->fontheight * srcrow + ri->ri_yorigin); 918 last = first + len; 919 dest = ri->ri_width * 920 (ri->ri_font->fontheight * dstrow + ri->ri_yorigin) + len; 921 if (leftover > 0) { 922 sc->sc_rblit[dest + 32] = 923 (uint64_t)((leftover - 1) << 24) | 924 (uint64_t)(i + 32); 925 } 926 for (i = last; i >= first; i -= 32) { 927 sc->sc_rblit[dest] = 0x300000001f000000LL | (uint64_t)i; 928 dest -= 32; 929 } 930 } else { 931 /* top to bottom */ 932 first = ri->ri_width * 933 (ri->ri_font->fontheight * srcrow + ri->ri_yorigin); 934 dest = ri->ri_width * 935 (ri->ri_font->fontheight * dstrow + ri->ri_yorigin); 936 last = first + len; 937 for (i = first; i <= last; i+= 32) { 938 sc->sc_rblit[dest] = 0x300000001f000000LL | (uint64_t)i; 939 dest += 32; 940 } 941 if (leftover > 0) { 942 sc->sc_rblit[dest] = 943 (uint64_t)((leftover - 1) << 24) | (uint64_t)i; 944 } 945 } 946 } 947 948 static void 949 tcx_eraserows(void *cookie, int start, int nrows, long attr) 950 { 951 struct rasops_info *ri = cookie; 952 struct vcons_screen *scr = ri->ri_hw; 953 struct tcx_softc *sc = scr->scr_cookie; 954 volatile uint64_t temp; 955 int i, last, first, len, leftover; 956 957 i = ri->ri_width * ri->ri_font->fontheight * nrows; 958 len = i & 0xffffe0; 959 leftover = i & 0x1f; 960 first = ri->ri_width * 961 (ri->ri_font->fontheight * start + ri->ri_yorigin); 962 last = first + len; 963 temp = 0x30000000ffffffffLL | 964 ((uint64_t)((ri->ri_devcmap[(attr >> 16) & 0xff]) & 0xff) << 32); 965 966 for (i = first; i < last; i+= 32) 967 sc->sc_rstip[i] = temp; 968 969 if (leftover > 0) { 970 temp &= 0xffffffffffffffffLL << (32 - leftover); 971 sc->sc_rstip[i] = temp; 972 } 973 } 974 /* 975 * The stipple engine is 100% retarded. All drawing operations have to start 976 * at 32 pixel boundaries so we'll have to deal with characters being split. 977 */ 978 979 static void 980 tcx_putchar(void *cookie, int row, int col, u_int c, long attr) 981 { 982 struct rasops_info *ri = cookie; 983 struct wsdisplay_font *font = PICK_FONT(ri, c); 984 struct vcons_screen *scr = ri->ri_hw; 985 struct tcx_softc *sc = scr->scr_cookie; 986 volatile uint64_t bg, fg, temp, mask; 987 int addr, i, uc, shift; 988 uint32_t fmask; 989 uint8_t *cdata; 990 uint16_t *wdata; 991 992 addr = ri->ri_xorigin + col * font->fontwidth + 993 (ri->ri_yorigin + row * font->fontheight) * ri->ri_width; 994 995 /* check if the character is crossing a 32 pixel boundary */ 996 if ((addr & 0xffffe0) == 997 ((addr + font->fontwidth - 1) & 0xffffe0)) { 998 /* phew, not split */ 999 shift = addr & 0x1f; 1000 addr &= 0xffffe0; 1001 fmask = 0xffffffff >> (32 - font->fontwidth); 1002 fmask = fmask << (32 - font->fontwidth - shift); 1003 mask = fmask; 1004 bg = 0x3000000000000000LL | 1005 ((uint64_t)ri->ri_devcmap[(attr >> 16) & 0xff] & 1006 0xff) << 32; 1007 bg |= mask; 1008 temp = 0x3000000000000000LL | 1009 ((uint64_t)ri->ri_devcmap[(attr >> 24) & 0xff] & 0xff) << 1010 32; 1011 uc = c - font->firstchar; 1012 cdata = (uint8_t *)font->data + uc * ri->ri_fontscale; 1013 1014 if (font->fontwidth < 9) { 1015 /* byte by byte */ 1016 for (i = 0; i < font->fontheight; i++) { 1017 sc->sc_rstip[addr] = bg; 1018 if (*cdata != 0) { 1019 if (shift > 24) { 1020 fg = (uint64_t)*cdata >> 1021 (shift - 24); 1022 } else { 1023 fg = (uint64_t)*cdata << 1024 (24 - shift); 1025 } 1026 sc->sc_rstip[addr] = fg | temp; 1027 } 1028 cdata++; 1029 addr += ri->ri_width; 1030 } 1031 } else if (font->fontwidth < 17) { 1032 /* short by short */ 1033 wdata = (uint16_t *)cdata; 1034 for (i = 0; i < font->fontheight; i++) { 1035 sc->sc_rstip[addr] = bg; 1036 if (*wdata != 0) { 1037 if (shift > 16) { 1038 fg = temp | (uint64_t)*wdata >> 1039 (shift - 16); 1040 } else { 1041 fg = temp | (uint64_t)*wdata << 1042 (16 - shift); 1043 } 1044 sc->sc_rstip[addr] = fg; 1045 } 1046 wdata++; 1047 addr += ri->ri_width; 1048 } 1049 } 1050 } else { 1051 /* and now the split case ( man this hardware is dumb ) */ 1052 volatile uint64_t bgr, maskr, fgr; 1053 uint32_t bork; 1054 1055 shift = addr & 0x1f; 1056 addr &= 0xffffe0; 1057 mask = 0xffffffff >> shift; 1058 maskr = (uint64_t)(0xffffffffUL << 1059 (32 - (font->fontwidth + shift - 32))); 1060 bg = 0x3000000000000000LL | 1061 ((uint64_t)ri->ri_devcmap[(attr >> 16) & 0xff] & 1062 0xff) << 32; 1063 bgr = bg | maskr; 1064 bg |= mask; 1065 temp = 0x3000000000000000LL | 1066 ((uint64_t)ri->ri_devcmap[(attr >> 24) & 0xff] & 0xff) << 1067 32; 1068 1069 uc = c - font->firstchar; 1070 cdata = (uint8_t *)font->data + uc * ri->ri_fontscale; 1071 1072 if (font->fontwidth < 9) { 1073 /* byte by byte */ 1074 for (i = 0; i < font->fontheight; i++) { 1075 sc->sc_rstip[addr] = bg; 1076 sc->sc_rstip[addr + 32] = bgr; 1077 bork = *cdata; 1078 if (bork != 0) { 1079 fg = (uint64_t)bork >> (shift - 24); 1080 sc->sc_rstip[addr] = fg | temp; 1081 fgr = (uint64_t)(bork << (52 - shift)); 1082 sc->sc_rstip[addr] = fgr | temp; 1083 } 1084 cdata++; 1085 addr += ri->ri_width; 1086 } 1087 } else if (font->fontwidth < 17) { 1088 /* short by short */ 1089 wdata = (uint16_t *)cdata; 1090 for (i = 0; i < font->fontheight; i++) { 1091 sc->sc_rstip[addr] = bg; 1092 sc->sc_rstip[addr + 32] = bgr; 1093 bork = *wdata; 1094 if (bork != 0) { 1095 fg = (uint64_t)bork >> (shift - 16); 1096 sc->sc_rstip[addr] = fg | temp; 1097 fgr = (uint64_t)(bork << (48 - shift)); 1098 sc->sc_rstip[addr + 32] = fgr | temp; 1099 } 1100 wdata++; 1101 addr += ri->ri_width; 1102 } 1103 } 1104 1105 } 1106 } 1107 1108 static int 1109 tcx_do_cursor(struct tcx_softc *sc, struct wsdisplay_cursor *cur) 1110 { 1111 if (sc->sc_8bit) { 1112 /* hw cursor is not implemented on tcx */ 1113 return -1; 1114 } 1115 if (cur->which & WSDISPLAY_CURSOR_DOCUR) { 1116 1117 if (cur->enable) { 1118 tcx_set_cursor(sc); 1119 } else { 1120 /* move the cursor out of sight */ 1121 bus_space_write_4(sc->sc_bustag, sc->sc_thc, 1122 THC_CURSOR_POS, 0x7fff7fff); 1123 } 1124 } 1125 if (cur->which & WSDISPLAY_CURSOR_DOHOT) { 1126 1127 sc->sc_hotspot_x = cur->hot.x; 1128 sc->sc_hotspot_y = cur->hot.y; 1129 tcx_set_cursor(sc); 1130 } 1131 if (cur->which & WSDISPLAY_CURSOR_DOPOS) { 1132 1133 sc->sc_cursor_x = cur->pos.x; 1134 sc->sc_cursor_y = cur->pos.y; 1135 tcx_set_cursor(sc); 1136 } 1137 if (cur->which & WSDISPLAY_CURSOR_DOCMAP) { 1138 #if 0 1139 /* 1140 * apparently we're not writing in the right register here - if we do 1141 * this the screen goes all funky 1142 */ 1143 int i; 1144 1145 for (i = 0; i < cur->cmap.count; i++) { 1146 bus_space_write_4(sc->sc_bustag, sc->sc_bt, DAC_ADDRESS, 1147 (cur->cmap.index + i + 2) << 24); 1148 bus_space_write_4(sc->sc_bustag, sc->sc_bt, 1149 DAC_CURSOR_LUT, cur->cmap.red[i] << 24); 1150 bus_space_write_4(sc->sc_bustag, sc->sc_bt, 1151 DAC_CURSOR_LUT, cur->cmap.green[i] << 24); 1152 bus_space_write_4(sc->sc_bustag, sc->sc_bt, 1153 DAC_CURSOR_LUT, cur->cmap.blue[i] << 24); 1154 } 1155 #endif 1156 } 1157 if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) { 1158 int i; 1159 uint32_t temp, poof; 1160 1161 for (i = 0; i < 128; i += 4) { 1162 memcpy(&temp, &cur->mask[i], 4); 1163 printf("%08x -> ", temp); 1164 poof = ((temp & 0x80808080) >> 7) | 1165 ((temp & 0x40404040) >> 5) | 1166 ((temp & 0x20202020) >> 3) | 1167 ((temp & 0x10101010) >> 1) | 1168 ((temp & 0x08080808) << 1) | 1169 ((temp & 0x04040404) << 3) | 1170 ((temp & 0x02020202) << 5) | 1171 ((temp & 0x01010101) << 7); 1172 printf("%08x\n", poof); 1173 bus_space_write_4(sc->sc_bustag, sc->sc_thc, 1174 THC_CURSOR_1 + i, poof); 1175 memcpy(&temp, &cur->image[i], 4); 1176 poof = ((temp & 0x80808080) >> 7) | 1177 ((temp & 0x40404040) >> 5) | 1178 ((temp & 0x20202020) >> 3) | 1179 ((temp & 0x10101010) >> 1) | 1180 ((temp & 0x08080808) << 1) | 1181 ((temp & 0x04040404) << 3) | 1182 ((temp & 0x02020202) << 5) | 1183 ((temp & 0x01010101) << 7); 1184 bus_space_write_4(sc->sc_bustag, sc->sc_thc, 1185 THC_CURSOR_0 + i, poof); 1186 } 1187 } 1188 return 0; 1189 } 1190 1191 static void 1192 tcx_set_cursor(struct tcx_softc *sc) 1193 { 1194 uint32_t reg; 1195 1196 reg = (sc->sc_cursor_x - sc->sc_hotspot_x) << 16 | 1197 ((sc->sc_cursor_y - sc->sc_hotspot_y) & 0xffff); 1198 bus_space_write_4(sc->sc_bustag, sc->sc_thc, THC_CURSOR_POS, reg); 1199 } 1200 1201