jcs's openbsd hax
openbsd
at jcs 639 lines 18 kB view raw
1/* $OpenBSD: bt463.c,v 1.15 2025/06/28 16:04:10 miod Exp $ */ 2/* $NetBSD: bt463.c,v 1.2 2000/06/13 17:21:06 nathanw Exp $ */ 3 4/*- 5 * Copyright (c) 1998 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 10 * NASA Ames Research Center. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34/* 35 * Copyright (c) 1995, 1996 Carnegie-Mellon University. 36 * All rights reserved. 37 * 38 * Author: Chris G. Demetriou 39 * 40 * Permission to use, copy, modify and distribute this software and 41 * its documentation is hereby granted, provided that both the copyright 42 * notice and this permission notice appear in all copies of the 43 * software, derivative works or modified versions, and any portions 44 * thereof, and that both notices appear in supporting documentation. 45 * 46 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 47 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 48 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 49 * 50 * Carnegie Mellon requests users of this software to return to 51 * 52 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 53 * School of Computer Science 54 * Carnegie Mellon University 55 * Pittsburgh PA 15213-3890 56 * 57 * any improvements or extensions that they make and grant Carnegie the 58 * rights to redistribute these changes. 59 */ 60 61 /* This code was derived from and originally located in sys/dev/pci/ 62 * NetBSD: tga_bt463.c,v 1.5 2000/03/04 10:27:59 elric Exp 63 */ 64 65#include <sys/param.h> 66#include <sys/systm.h> 67#include <sys/device.h> 68#include <sys/buf.h> 69#include <sys/kernel.h> 70#include <sys/malloc.h> 71 72#include <dev/pci/pcivar.h> 73#include <dev/pci/tgareg.h> 74#include <dev/pci/tgavar.h> 75#include <dev/ic/bt463reg.h> 76#include <dev/ic/bt463var.h> 77 78#include <dev/wscons/wsconsio.h> 79 80/* 81 * Functions exported via the RAMDAC configuration table. 82 */ 83void bt463_init(struct ramdac_cookie *); 84int bt463_set_cmap(struct ramdac_cookie *, 85 struct wsdisplay_cmap *); 86int bt463_get_cmap(struct ramdac_cookie *, 87 struct wsdisplay_cmap *); 88int bt463_set_cursor(struct ramdac_cookie *, 89 struct wsdisplay_cursor *); 90int bt463_get_cursor(struct ramdac_cookie *, 91 struct wsdisplay_cursor *); 92int bt463_set_curpos(struct ramdac_cookie *, 93 struct wsdisplay_curpos *); 94int bt463_get_curpos(struct ramdac_cookie *, 95 struct wsdisplay_curpos *); 96int bt463_get_curmax(struct ramdac_cookie *, 97 struct wsdisplay_curpos *); 98int bt463_check_curcmap(struct ramdac_cookie *, 99 struct wsdisplay_cursor *cursorp); 100void bt463_set_curcmap(struct ramdac_cookie *, 101 struct wsdisplay_cursor *cursorp); 102int bt463_get_curcmap(struct ramdac_cookie *, 103 struct wsdisplay_cursor *cursorp); 104 105#ifdef BT463_DEBUG 106int bt463_store(void *); 107int bt463_debug(void *); 108int bt463_readback(void *); 109void bt463_copyback(void *); 110#endif 111 112struct ramdac_funcs bt463_funcsstruct = { 113 "Bt463", 114 bt463_register, 115 bt463_init, 116 bt463_set_cmap, 117 bt463_get_cmap, 118 bt463_set_cursor, 119 bt463_get_cursor, 120 bt463_set_curpos, 121 bt463_get_curpos, 122 bt463_get_curmax, 123 bt463_check_curcmap, 124 bt463_set_curcmap, 125 bt463_get_curcmap, 126 NULL, 127}; 128 129/* 130 * Private data. 131 */ 132struct bt463data { 133 void *cookie; /* This is what is passed 134 * around, and is probably 135 * struct tga_devconfig * 136 */ 137 138 int (*ramdac_sched_update)(void *, void (*)(void *)); 139 void (*ramdac_wr)(void *, u_int, u_int8_t); 140 u_int8_t (*ramdac_rd)(void *, u_int); 141 142 int changed; /* what changed; see below */ 143 char curcmap_r[2]; /* cursor colormap */ 144 char curcmap_g[2]; 145 char curcmap_b[2]; 146 char cmap_r[BT463_NCMAP_ENTRIES]; /* colormap */ 147 char cmap_g[BT463_NCMAP_ENTRIES]; 148 char cmap_b[BT463_NCMAP_ENTRIES]; 149 int window_type[16]; /* 16 24-bit window type table entries */ 150}; 151 152/* When we're doing console initialization, there's no 153 * way to get our cookie back to the video card's softc 154 * before we call sched_update. So we stash it here, 155 * and bt463_update will look for it here first. 156 */ 157static struct bt463data *console_data; 158 159 160#define BTWREG(data, addr, val) do { bt463_wraddr((data), (addr)); \ 161 (data)->ramdac_wr((data)->cookie, BT463_REG_IREG_DATA, (val)); } while (0) 162#define BTWNREG(data, val) (data)->ramdac_wr((data)->cookie, \ 163 BT463_REG_IREG_DATA, (val)) 164#define BTRREG(data, addr) (bt463_wraddr((data), (addr)), \ 165 (data)->ramdac_rd((data)->cookie, BT463_REG_IREG_DATA)) 166#define BTRNREG(data) ((data)->ramdac_rd((data)->cookie, BT463_REG_IREG_DATA)) 167 168#define DATA_CURCMAP_CHANGED 0x01 /* cursor colormap changed */ 169#define DATA_CMAP_CHANGED 0x02 /* colormap changed */ 170#define DATA_WTYPE_CHANGED 0x04 /* window type table changed */ 171#define DATA_ALL_CHANGED 0x07 172 173/* 174 * Internal functions. 175 */ 176inline void bt463_wraddr(struct bt463data *, u_int16_t); 177 178void bt463_update(void *); 179 180 181/*****************************************************************************/ 182 183/* 184 * Functions exported via the RAMDAC configuration table. 185 */ 186 187struct ramdac_funcs * 188bt463_funcs(void) 189{ 190 return &bt463_funcsstruct; 191} 192 193struct ramdac_cookie * 194bt463_register(void *v, int (*sched_update)(void *, void (*)(void *)), 195 void (*wr)(void *, u_int, u_int8_t), u_int8_t (*rd)(void *, u_int)) 196{ 197 struct bt463data *data; 198 /* 199 * XXX -- comment out of date. rcd. 200 * If we should allocate a new private info struct, do so. 201 * Otherwise, use the one we have (if it's there), or 202 * use the temporary one on the stack. 203 */ 204 data = malloc(sizeof *data, M_DEVBUF, M_WAITOK); 205 /* XXX -- if !data */ 206 data->cookie = v; 207 data->ramdac_sched_update = sched_update; 208 data->ramdac_wr = wr; 209 data->ramdac_rd = rd; 210 return (struct ramdac_cookie *)data; 211} 212 213/* 214 * This function exists solely to provide a means to init 215 * the RAMDAC without first registering. It is useful for 216 * initializing the console early on. 217 */ 218void 219bt463_cninit(void *v, int (*sched_update)(void *, void (*)(void *)), 220 void (*wr)(void *, u_int, u_int8_t), u_int8_t (*rd)(void *, u_int)) 221{ 222 struct bt463data tmp, *data = &tmp; 223 data->cookie = v; 224 data->ramdac_sched_update = sched_update; 225 data->ramdac_wr = wr; 226 data->ramdac_rd = rd; 227 /* Set up console_data so that when bt463_update is called back, 228 * it can use the right information. 229 */ 230 console_data = data; 231 bt463_init((struct ramdac_cookie *)data); 232 console_data = NULL; 233} 234 235void 236bt463_init(struct ramdac_cookie *rc) 237{ 238 struct bt463data *data = (struct bt463data *)rc; 239 240 int i; 241 242 /* 243 * Init the BT463 for normal operation. 244 */ 245 246 247 /* 248 * Setup: 249 * reg 0: 4:1 multiplexing, 25/75 blink. 250 * reg 1: Overlay mapping: mapped to common palette, 251 * 14 window type entries, 24-plane configuration mode, 252 * 4 overlay planes, underlays disabled, no cursor. 253 * reg 2: sync-on-green enabled, pedestal enabled. 254 */ 255 256 BTWREG(data, BT463_IREG_COMMAND_0, 0x40); 257 BTWREG(data, BT463_IREG_COMMAND_1, 0x48); 258 BTWREG(data, BT463_IREG_COMMAND_2, 0xC0); 259 260 /* 261 * Initialize the read mask. 262 */ 263 bt463_wraddr(data, BT463_IREG_READ_MASK_P0_P7); 264 for (i = 0; i < 4; i++) 265 BTWNREG(data, 0xff); 266 267 /* 268 * Initialize the blink mask. 269 */ 270 bt463_wraddr(data, BT463_IREG_BLINK_MASK_P0_P7); 271 for (i = 0; i < 4; i++) 272 BTWNREG(data, 0); 273 274 275 /* 276 * Clear test register 277 */ 278 BTWREG(data, BT463_IREG_TEST, 0); 279 280 /* 281 * Initialize the RAMDAC info struct to hold all of our 282 * data, and fill it in. 283 */ 284 data->changed = DATA_ALL_CHANGED; 285 286 /* initial cursor colormap: 0 is black, 1 is white */ 287 data->curcmap_r[0] = data->curcmap_g[0] = data->curcmap_b[0] = 0; 288 data->curcmap_r[1] = data->curcmap_g[1] = data->curcmap_b[1] = 0xff; 289 290 /* Initial colormap: 0 is black, everything else is white */ 291 data->cmap_r[0] = data->cmap_g[0] = data->cmap_b[0] = 0; 292 for (i = 1; i < 256; i++) { 293 data->cmap_r[i] = rasops_cmap[3*i + 0]; 294 data->cmap_g[i] = rasops_cmap[3*i + 1]; 295 data->cmap_b[i] = rasops_cmap[3*i + 2]; 296 } 297 298 /* Initialize the window type table: 299 * 300 * Entry 0: 24-plane truecolor, overlays enabled, bypassed. 301 * 302 * Lookup table bypass: yes ( 1 << 23 & 0x800000) 800000 303 * Colormap address: 0x000 (0x000 << 17 & 0x7e0000) 0 304 * Overlay mask: 0xf ( 0xf << 13 & 0x01e000) 1e000 305 * Overlay location: P<27:24> ( 0 << 12 & 0x001000) 0 306 * Display mode: Truecolor ( 0 << 9 & 0x000e00) 000 307 * Number of planes: 8 ( 8 << 5 & 0x0001e0) 100 308 * Plane shift: 0 ( 0 << 0 & 0x00001f) 0 309 * -------- 310 * 0x81e100 311 */ 312 data->window_type[0] = 0x81e100; 313 314 /* Entry 1: 8-plane pseudocolor in the bottom 8 bits, 315 * overlays enabled, colormap starting at 0. 316 * 317 * Lookup table bypass: no ( 0 << 23 & 0x800000) 0 318 * Colormap address: 0x000 (0x000 << 17 & 0x7e0000) 0 319 * Overlay mask: 0xf ( 0xf << 13 & 0x01e000) 0x1e000 320 * Overlay location: P<27:24> ( 0 << 12 & 0x001000) 0 321 * Display mode: Pseudocolor ( 1 << 9 & 0x000e00) 0x200 322 * Number of planes: 8 ( 8 << 5 & 0x0001e0) 0x100 323 * Plane shift: 16 ( 0x10 << 0 & 0x00001f) 10 324 * -------- 325 * 0x01e310 326 */ 327 data->window_type[1] = 0x01e310; 328 329 /* The colormap interface to the world only supports one colormap, 330 * so having an entry for the 'alternate' colormap in the bt463 331 * probably isn't useful. 332 */ 333 334 /* Fill the remaining table entries with clones of entry 0 until we 335 * figure out a better use for them. 336 */ 337 338 for (i = 2; i < BT463_NWTYPE_ENTRIES; i++) { 339 data->window_type[i] = 0x81e100; 340 } 341 342 data->ramdac_sched_update(data->cookie, bt463_update); 343 344} 345 346int 347bt463_set_cmap(struct ramdac_cookie *rc, struct wsdisplay_cmap *cmapp) 348{ 349 struct bt463data *data = (struct bt463data *)rc; 350 u_int count, index; 351 int s, error; 352 353 index = cmapp->index; 354 count = cmapp->count; 355 356 if (index >= BT463_NCMAP_ENTRIES || count > BT463_NCMAP_ENTRIES - index) 357 return (EINVAL); 358 359 s = spltty(); 360 361 if ((error = copyin(cmapp->red, &data->cmap_r[index], count)) != 0) { 362 splx(s); 363 return (error); 364 } 365 if ((error = copyin(cmapp->green, &data->cmap_g[index], count)) != 0) { 366 splx(s); 367 return (error); 368 } 369 if ((error = copyin(cmapp->blue, &data->cmap_b[index], count)) != 0) { 370 splx(s); 371 return (error); 372 } 373 374 data->changed |= DATA_CMAP_CHANGED; 375 376 data->ramdac_sched_update(data->cookie, bt463_update); 377 splx(s); 378 379 return (0); 380} 381 382int 383bt463_get_cmap(struct ramdac_cookie *rc, struct wsdisplay_cmap *cmapp) 384{ 385 struct bt463data *data = (struct bt463data *)rc; 386 u_int count, index; 387 int error; 388 389 count = cmapp->count; 390 index = cmapp->index; 391 392 if (index >= BT463_NCMAP_ENTRIES || count > BT463_NCMAP_ENTRIES - index) 393 return (EINVAL); 394 395 error = copyout(&data->cmap_r[index], cmapp->red, count); 396 if (error) 397 return (error); 398 error = copyout(&data->cmap_g[index], cmapp->green, count); 399 if (error) 400 return (error); 401 error = copyout(&data->cmap_b[index], cmapp->blue, count); 402 return (error); 403} 404 405int 406bt463_check_curcmap(struct ramdac_cookie *rc, struct wsdisplay_cursor *cursorp) 407{ 408 u_int index, count; 409 u_int8_t spare[2]; 410 int error; 411 412 index = cursorp->cmap.index; 413 count = cursorp->cmap.count; 414 415 if (index >= 2 || count > 2 - index) 416 return (EINVAL); 417 418 if ((error = copyin(&cursorp->cmap.red, &spare, count)) != 0) 419 return (error); 420 if ((error = copyin(&cursorp->cmap.green, &spare, count)) != 0) 421 return (error); 422 if ((error = copyin(&cursorp->cmap.blue, &spare, count)) != 0) 423 return (error); 424 425 return (0); 426} 427 428void 429bt463_set_curcmap(struct ramdac_cookie *rc, struct wsdisplay_cursor *cursorp) 430{ 431 struct bt463data *data = (struct bt463data *)rc; 432 int count, index; 433 434 /* can't fail; parameters have already been checked. */ 435 count = cursorp->cmap.count; 436 index = cursorp->cmap.index; 437 copyin(cursorp->cmap.red, &data->curcmap_r[index], count); 438 copyin(cursorp->cmap.green, &data->curcmap_g[index], count); 439 copyin(cursorp->cmap.blue, &data->curcmap_b[index], count); 440 data->changed |= DATA_CURCMAP_CHANGED; 441 data->ramdac_sched_update(data->cookie, bt463_update); 442} 443 444int 445bt463_get_curcmap(struct ramdac_cookie *rc, struct wsdisplay_cursor *cursorp) 446{ 447 struct bt463data *data = (struct bt463data *)rc; 448 int error; 449 450 cursorp->cmap.index = 0; /* DOCMAP */ 451 cursorp->cmap.count = 2; 452 if (cursorp->cmap.red != NULL) { 453 error = copyout(data->curcmap_r, cursorp->cmap.red, 2); 454 if (error) 455 return (error); 456 } 457 if (cursorp->cmap.green != NULL) { 458 error = copyout(data->curcmap_g, cursorp->cmap.green, 2); 459 if (error) 460 return (error); 461 } 462 if (cursorp->cmap.blue != NULL) { 463 error = copyout(data->curcmap_b, cursorp->cmap.blue, 2); 464 if (error) 465 return (error); 466 } 467 return (0); 468} 469 470 471/*****************************************************************************/ 472 473/* 474 * Internal functions. 475 */ 476 477#ifdef BT463_DEBUG 478int 479bt463_store(void *v) 480{ 481 struct bt463data *data = (struct bt463data *)v; 482 483 data->changed = DATA_ALL_CHANGED; 484 data->ramdac_sched_update(data->cookie, bt463_update); 485 printf("Scheduled bt463 store\n"); 486 487 return 0; 488} 489 490 491int 492bt463_readback(void *v) 493{ 494 struct bt463data *data = (struct bt463data *)v; 495 496 data->ramdac_sched_update(data->cookie, bt463_copyback); 497 printf("Scheduled bt463 copyback\n"); 498 return 0; 499} 500 501int 502bt463_debug(void *v) 503{ 504 struct bt463data *data = (struct bt463data *)v; 505 int i; 506 u_int8_t val; 507 508 printf("BT463 main regs:\n"); 509 for (i = 0x200; i < 0x20F; i ++) { 510 val = BTRREG(data, i); 511 printf(" $%04x %02x\n", i, val); 512 } 513 514 printf("BT463 revision register:\n"); 515 val = BTRREG(data, 0x220); 516 printf(" $%04x %02x\n", 0x220, val); 517 518 printf("BT463 window type table (from softc):\n"); 519 520 for (i = 0; i < BT463_NWTYPE_ENTRIES; i++) { 521 printf("%02x %06x\n", i, data->window_type[i]); 522 } 523 524 return 0; 525} 526 527void 528bt463_copyback(void *p) 529{ 530 struct bt463data *data = (struct bt463data *)p; 531 int i; 532 533 for (i = 0; i < BT463_NWTYPE_ENTRIES; i++) { 534 bt463_wraddr(data, BT463_IREG_WINDOW_TYPE_TABLE + i); 535 data->window_type[i] = (BTRNREG(data) & 0xff); /* B0-7 */ 536 data->window_type[i] |= (BTRNREG(data) & 0xff) << 8; /* B8-15 */ 537 data->window_type[i] |= (BTRNREG(data) & 0xff) << 16; /* B16-23 */ 538 } 539} 540#endif 541 542inline void 543bt463_wraddr(struct bt463data *data, u_int16_t ireg) 544{ 545 data->ramdac_wr(data->cookie, BT463_REG_ADDR_LOW, ireg & 0xff); 546 data->ramdac_wr(data->cookie, BT463_REG_ADDR_HIGH, (ireg >> 8) & 0xff); 547} 548 549void 550bt463_update(void *p) 551{ 552 struct bt463data *data = (struct bt463data *)p; 553 int i, v; 554 555 if (console_data != NULL) { 556 /* The cookie passed in from sched_update is incorrect. Use the 557 * right one. 558 */ 559 data = console_data; 560 } 561 562 v = data->changed; 563 564 /* The Bt463 won't accept window type data except during a blanking 565 * interval, so we do this early in the interrupt. 566 * Blanking the screen might also be a good idea, but it can cause 567 * unpleasant flashing and is hard to do from this side of the 568 * ramdac interface. 569 */ 570 if (v & DATA_WTYPE_CHANGED) { 571 /* spit out the window type data */ 572 for (i = 0; i < BT463_NWTYPE_ENTRIES; i++) { 573 bt463_wraddr(data, BT463_IREG_WINDOW_TYPE_TABLE + i); 574 BTWNREG(data, (data->window_type[i]) & 0xff); /* B0-7 */ 575 BTWNREG(data, (data->window_type[i] >> 8) & 0xff); /* B8-15 */ 576 BTWNREG(data, (data->window_type[i] >> 16) & 0xff); /* B16-23 */ 577 } 578 } 579 580 if (v & DATA_CURCMAP_CHANGED) { 581 bt463_wraddr(data, BT463_IREG_CURSOR_COLOR_0); 582 /* spit out the cursor data */ 583 for (i = 0; i < 2; i++) { 584 BTWNREG(data, data->curcmap_r[i]); 585 BTWNREG(data, data->curcmap_g[i]); 586 BTWNREG(data, data->curcmap_b[i]); 587 } 588 } 589 590 if (v & DATA_CMAP_CHANGED) { 591 bt463_wraddr(data, BT463_IREG_CPALETTE_RAM); 592 /* spit out the colormap data */ 593 for (i = 0; i < BT463_NCMAP_ENTRIES; i++) { 594 data->ramdac_wr(data->cookie, BT463_REG_CMAP_DATA, 595 data->cmap_r[i]); 596 data->ramdac_wr(data->cookie, BT463_REG_CMAP_DATA, 597 data->cmap_g[i]); 598 data->ramdac_wr(data->cookie, BT463_REG_CMAP_DATA, 599 data->cmap_b[i]); 600 } 601 } 602 603 data->changed = 0; 604} 605 606int 607bt463_set_cursor(struct ramdac_cookie *rc, struct wsdisplay_cursor *cur) 608{ 609 struct bt463data *data = (struct bt463data *)rc; 610 return tga_builtin_set_cursor(data->cookie, cur); 611} 612 613int 614bt463_get_cursor(struct ramdac_cookie *rc, struct wsdisplay_cursor *cur) 615{ 616 struct bt463data *data = (struct bt463data *)rc; 617 return tga_builtin_get_cursor(data->cookie, cur); 618} 619 620int 621bt463_set_curpos(struct ramdac_cookie *rc, struct wsdisplay_curpos *cur) 622{ 623 struct bt463data *data = (struct bt463data *)rc; 624 return tga_builtin_set_curpos(data->cookie, cur); 625} 626 627int 628bt463_get_curpos(struct ramdac_cookie *rc, struct wsdisplay_curpos *cur) 629{ 630 struct bt463data *data = (struct bt463data *)rc; 631 return tga_builtin_get_curpos(data->cookie, cur); 632} 633 634int 635bt463_get_curmax(struct ramdac_cookie *rc, struct wsdisplay_curpos *cur) 636{ 637 struct bt463data *data = (struct bt463data *)rc; 638 return tga_builtin_get_curmax(data->cookie, cur); 639}