1 /* $NetBSD: ite_et.c,v 1.16 2003/01/01 00:48:34 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 1996 Leo Weppelman. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Leo Weppelman. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/conf.h> 35 #include <sys/ioctl.h> 36 #include <sys/malloc.h> 37 #include <sys/device.h> 38 #include <dev/cons.h> 39 40 #include <machine/cpu.h> 41 42 #include <atari/atari/device.h> 43 44 #include <atari/dev/itevar.h> 45 #include <atari/dev/iteioctl.h> 46 #include <atari/dev/grfioctl.h> 47 #include <atari/dev/grf_etreg.h> 48 #include <atari/dev/grfabs_reg.h> 49 #include <atari/dev/grfabs_et.h> 50 #include <atari/dev/grfvar.h> 51 #include <atari/dev/font.h> 52 #include <atari/dev/viewioctl.h> 53 #include <atari/dev/viewvar.h> 54 55 #include "grfet.h" 56 57 /* 58 * This is what ip->priv points to; 59 * it contains local variables for custom-chip ites. 60 */ 61 struct ite_priv { 62 volatile u_char *regkva; 63 }; 64 65 typedef struct ite_priv ipriv_t; 66 67 static ipriv_t con_ipriv; 68 69 /* Console colors */ 70 static u_char etconscolors[3][3] = { /* background, foreground, hilite */ 71 {0x0, 0x0, 0x0}, {0x30, 0x30, 0x30}, { 0x3f, 0x3f, 0x3f} 72 }; 73 74 /* XXX: Shouldn't these be in font.h???? */ 75 extern font_info font_info_8x8; 76 extern font_info font_info_8x16; 77 78 static void grfet_iteinit __P((struct grf_softc *)); 79 static void view_init __P((struct ite_softc *)); 80 static void view_deinit __P((struct ite_softc *)); 81 static int iteet_ioctl __P((struct ite_softc *, u_long, caddr_t, int, 82 struct proc *)); 83 static int ite_newsize __P((struct ite_softc *, struct itewinsize *)); 84 static void et_inittextmode __P((struct ite_softc *, et_sv_reg_t *, int)); 85 void et_cursor __P((struct ite_softc *ip, int flag)); 86 void et_clear __P((struct ite_softc *ip, int sy, int sx, int h, int w)); 87 void et_putc __P((struct ite_softc *ip, int c, int dy, int dx, int mode)); 88 void et_scroll __P((struct ite_softc *ip, int sy, int sx, int count, 89 int dir)); 90 91 /* 92 * grfet config stuff 93 */ 94 void grfetattach __P((struct device *, struct device *, void *)); 95 int grfetmatch __P((struct device *, struct cfdata *, void *)); 96 int grfetprint __P((void *, const char *)); 97 98 CFATTACH_DECL(grfet, sizeof(struct grf_softc), 99 grfetmatch, grfetattach, NULL, NULL); 100 101 /* 102 * only used in console init. 103 */ 104 static struct cfdata *cfdata_grf = NULL; 105 106 int 107 grfetmatch(pdp, cfp, auxp) 108 struct device *pdp; 109 struct cfdata *cfp; 110 void *auxp; 111 { 112 static int card_probed = -1; 113 static int did_consinit = 0; 114 grf_auxp_t *grf_auxp = auxp; 115 extern const struct cdevsw view_cdevsw; 116 117 if (card_probed <= 0) { 118 if (card_probed == 0) /* Probed but failed */ 119 return 0; 120 card_probed = 0; 121 122 /* 123 * Check if the layers we depend on exist 124 */ 125 if(!(machineid & ATARI_HADES)) 126 return 0; 127 if (!et_probe_card()) 128 return 0; 129 if (grfabs_probe(&et_probe_video) == 0) 130 return 0; 131 viewprobe(); 132 card_probed = 1; /* Probed and found */ 133 } 134 135 if (atari_realconfig == 0) { 136 /* 137 * Early console init. Only match first unit. 138 */ 139 if (did_consinit) 140 return 0; 141 if ((*view_cdevsw.d_open)(cfp->cf_unit, 0, 0, NULL)) 142 return 0; 143 cfdata_grf = cfp; 144 did_consinit = 1; 145 return 1; 146 } 147 148 /* 149 * Normal config. When we are called directly from the grfbus, 150 * we only match the first unit. The attach function will call us for 151 * the other configured units. 152 */ 153 if (grf_auxp->from_bus_match 154 && ((did_consinit > 1) || !et_probe_card())) 155 return 0; 156 157 if (!grf_auxp->from_bus_match && (grf_auxp->unit != cfp->cf_unit)) 158 return 0; 159 160 /* 161 * Final constraint: each grf needs a view.... 162 */ 163 if((cfdata_grf == NULL) || (did_consinit > 1)) { 164 if((*view_cdevsw.d_open)(cfp->cf_unit, 0, 0, NULL)) 165 return 0; 166 } 167 did_consinit = 2; 168 return 1; 169 } 170 171 /* 172 * attach: initialize the grf-structure and try to attach an ite to us. 173 * note : dp is NULL during early console init. 174 */ 175 void 176 grfetattach(pdp, dp, auxp) 177 struct device *pdp, *dp; 178 void *auxp; 179 { 180 static struct grf_softc congrf; 181 static int first_attach = 1; 182 grf_auxp_t *grf_bus_auxp = auxp; 183 grf_auxp_t grf_auxp; 184 struct grf_softc *gp; 185 int maj; 186 extern const struct cdevsw grf_cdevsw; 187 188 /* 189 * find our major device number 190 */ 191 maj = cdevsw_lookup_major(&grf_cdevsw); 192 193 /* 194 * Handle exeption case: early console init 195 */ 196 if(dp == NULL) { 197 congrf.g_unit = cfdata_grf->cf_unit; 198 congrf.g_grfdev = makedev(maj, congrf.g_unit); 199 congrf.g_itedev = (dev_t)-1; 200 congrf.g_flags = GF_ALIVE; 201 congrf.g_mode = grf_mode; 202 congrf.g_conpri = CN_INTERNAL; 203 congrf.g_viewdev = congrf.g_unit; 204 grfet_iteinit(&congrf); 205 grf_viewsync(&congrf); 206 207 /* Attach console ite */ 208 atari_config_found(cfdata_grf, NULL, &congrf, grfetprint); 209 return; 210 } 211 212 gp = (struct grf_softc *)dp; 213 gp->g_unit = gp->g_device.dv_unit; 214 grfsp[gp->g_unit] = gp; 215 216 if((cfdata_grf != NULL) && (gp->g_unit == congrf.g_unit)) { 217 /* 218 * We inited earlier just copy the info, take care 219 * not to copy the device struct though. 220 */ 221 bcopy(&congrf.g_display, &gp->g_display, 222 (char *)&gp[1] - (char *)&gp->g_display); 223 } 224 else { 225 gp->g_grfdev = makedev(maj, gp->g_unit); 226 gp->g_itedev = (dev_t)-1; 227 gp->g_flags = GF_ALIVE; 228 gp->g_mode = grf_mode; 229 gp->g_conpri = 0; 230 gp->g_viewdev = gp->g_unit; 231 grfet_iteinit(gp); 232 grf_viewsync(gp); 233 } 234 235 printf(": %dx%d", gp->g_display.gd_dwidth, gp->g_display.gd_dheight); 236 if(gp->g_display.gd_colors == 2) 237 printf(" monochrome\n"); 238 else printf(" colors %d\n", gp->g_display.gd_colors); 239 240 /* 241 * try and attach an ite 242 */ 243 config_found(dp, gp, grfetprint); 244 245 /* 246 * If attaching the first unit, go ahead and 'find' the rest of us 247 */ 248 if (first_attach) { 249 first_attach = 0; 250 grf_auxp.from_bus_match = 0; 251 for (grf_auxp.unit=0; grf_auxp.unit < NGRFET; grf_auxp.unit++) { 252 config_found(pdp, (void*)&grf_auxp, grf_bus_auxp->busprint); 253 } 254 } 255 } 256 257 int 258 grfetprint(auxp, pnp) 259 void *auxp; 260 const char *pnp; 261 { 262 if(pnp) /* XXX */ 263 aprint_normal("ite at %s", pnp); 264 return(UNCONF); 265 } 266 267 /* 268 * Init ite portion of grf_softc struct 269 */ 270 static void 271 grfet_iteinit(gp) 272 struct grf_softc *gp; 273 { 274 275 gp->g_itecursor = et_cursor; 276 gp->g_iteputc = et_putc; 277 gp->g_iteclear = et_clear; 278 gp->g_itescroll = et_scroll; 279 gp->g_iteinit = view_init; 280 gp->g_itedeinit = view_deinit; 281 } 282 283 static void 284 view_deinit(ip) 285 struct ite_softc *ip; 286 { 287 ip->flags &= ~ITE_INITED; 288 } 289 290 static void 291 view_init(ip) 292 register struct ite_softc *ip; 293 { 294 struct itewinsize wsz; 295 ipriv_t *cci; 296 view_t *view; 297 save_area_t *et_save; 298 299 if((cci = ip->priv) != NULL) 300 return; 301 302 ip->itexx_ioctl = iteet_ioctl; 303 304 #if defined(KFONT_8X8) 305 ip->font = font_info_8x8; 306 #else 307 ip->font = font_info_8x16; 308 #endif 309 310 /* Find the correct set of rendering routines for this font. */ 311 if(ip->font.width != 8) 312 panic("kernel font size not supported"); 313 314 if(!atari_realconfig) 315 ip->priv = cci = &con_ipriv; 316 else ip->priv = cci = (ipriv_t*)malloc(sizeof(*cci), M_DEVBUF,M_WAITOK); 317 if(cci == NULL) 318 panic("No memory for ite-view"); 319 bzero(cci, sizeof(*cci)); 320 321 wsz.x = ite_default_x; 322 wsz.y = ite_default_y; 323 wsz.width = ite_default_width; 324 wsz.height = ite_default_height; 325 wsz.depth = ite_default_depth; 326 327 ite_newsize (ip, &wsz); 328 329 view = viewview(ip->grf->g_viewdev); 330 cci->regkva = view->bitmap->regs; 331 332 /* 333 * Only console will be turned on by default.. 334 */ 335 if(ip->flags & ITE_ISCONS) 336 ip->grf->g_mode(ip->grf, GM_GRFON, NULL, 0, 0); 337 338 /* 339 * Activate text-mode settings 340 */ 341 et_save = (save_area_t *)view->save_area; 342 if (et_save == NULL) 343 et_inittextmode(ip, NULL, view->flags & VF_DISPLAY); 344 else { 345 et_inittextmode(ip, &et_save->sv_regs, view->flags&VF_DISPLAY); 346 et_save->fb_size = ip->cols * ip->rows; 347 } 348 } 349 350 static int 351 ite_newsize(ip, winsz) 352 struct ite_softc *ip; 353 struct itewinsize *winsz; 354 { 355 struct view_size vs; 356 int error = 0; 357 save_area_t *et_save; 358 view_t *view; 359 extern const struct cdevsw view_cdevsw; 360 361 vs.x = winsz->x; 362 vs.y = winsz->y; 363 vs.width = winsz->width; 364 vs.height = winsz->height; 365 vs.depth = winsz->depth; 366 367 error = (*view_cdevsw.d_ioctl)(ip->grf->g_viewdev, VIOCSSIZE, 368 (caddr_t)&vs, 0, NOPROC); 369 view = viewview(ip->grf->g_viewdev); 370 371 /* 372 * Reinitialize our structs 373 */ 374 ip->cols = view->display.width / ip->font.width; 375 ip->rows = view->display.height / ip->font.height; 376 377 /* 378 * save new values so that future opens use them 379 * this may not be correct when we implement Virtual Consoles 380 */ 381 ite_default_height = view->display.height; 382 ite_default_width = view->display.width; 383 ite_default_x = view->display.x; 384 ite_default_y = view->display.y; 385 ite_default_depth = view->bitmap->depth; 386 387 et_save = (save_area_t *)view->save_area; 388 if (et_save == NULL) 389 et_inittextmode(ip, NULL, view->flags & VF_DISPLAY); 390 else { 391 et_inittextmode(ip, &et_save->sv_regs, view->flags & VF_DISPLAY); 392 et_save->fb_size = ip->cols * ip->rows; 393 } 394 et_clear(ip, 0, 0, ip->rows, ip->cols); 395 396 return(error); 397 } 398 399 int 400 iteet_ioctl(ip, cmd, addr, flag, p) 401 struct ite_softc *ip; 402 u_long cmd; 403 caddr_t addr; 404 int flag; 405 struct proc *p; 406 { 407 struct winsize ws; 408 struct itewinsize *is; 409 int error = 0; 410 view_t *view = viewview(ip->grf->g_viewdev); 411 extern const struct cdevsw ite_cdevsw; 412 extern const struct cdevsw view_cdevsw; 413 414 switch (cmd) { 415 case ITEIOCSWINSZ: 416 is = (struct itewinsize *)addr; 417 418 if(ite_newsize(ip, is)) 419 error = ENOMEM; 420 else { 421 view = viewview(ip->grf->g_viewdev); 422 ws.ws_row = ip->rows; 423 ws.ws_col = ip->cols; 424 ws.ws_xpixel = view->display.width; 425 ws.ws_ypixel = view->display.height; 426 ite_reset(ip); 427 /* 428 * XXX tell tty about the change 429 * XXX this is messy, but works 430 */ 431 (*ite_cdevsw.d_ioctl)(ip->grf->g_itedev, TIOCSWINSZ, 432 (caddr_t)&ws, 0, p); 433 } 434 break; 435 case VIOCSCMAP: 436 case VIOCGCMAP: 437 /* 438 * XXX watchout for that NOPROC. its not really the kernel 439 * XXX talking these two commands don't use the proc pointer 440 * XXX though. 441 */ 442 error = (*view_cdevsw.d_ioctl)(ip->grf->g_viewdev, cmd, addr, 443 flag, NOPROC); 444 break; 445 default: 446 error = EPASSTHROUGH; 447 break; 448 } 449 return (error); 450 } 451 452 void 453 et_cursor(ip, flag) 454 struct ite_softc *ip; 455 int flag; 456 { 457 volatile u_char *ba; 458 view_t *v; 459 u_long cpos; 460 461 ba = ((ipriv_t*)ip->priv)->regkva; 462 v = viewview(ip->grf->g_viewdev); 463 464 /* 465 * Don't update the cursor when not on display 466 */ 467 if (!(v->flags & VF_DISPLAY)) 468 return; 469 470 switch (flag) { 471 case DRAW_CURSOR: 472 /*WCrt(ba, CRT_ID_CURSOR_START, & ~0x20); */ 473 case MOVE_CURSOR: 474 cpos = RCrt(ba, CRT_ID_START_ADDR_LOW) & 0xff; 475 cpos |= (RCrt(ba, CRT_ID_START_ADDR_HIGH) & 0xff) << 8; 476 cpos += ip->curx + ip->cury * ip->cols; 477 WCrt(ba, CRT_ID_CURSOR_LOC_LOW, cpos & 0xff); 478 WCrt(ba, CRT_ID_CURSOR_LOC_HIGH, (cpos >> 8) & 0xff); 479 WCrt(ba, CTR_ID_EXT_START, (cpos >> (16-2)) & 0x0c); 480 481 ip->cursorx = ip->curx; 482 ip->cursory = ip->cury; 483 break; 484 case ERASE_CURSOR: 485 /*WCrt(ba, CRT_ID_CURSOR_START, | 0x20); */ 486 case START_CURSOROPT: 487 case END_CURSOROPT: 488 default: 489 break; 490 } 491 } 492 493 void 494 et_putc(ip, c, dy, dx, mode) 495 struct ite_softc *ip; 496 int c; 497 int dy; 498 int dx; 499 int mode; 500 { 501 view_t *v = viewview(ip->grf->g_viewdev); 502 u_char attr; 503 u_short *cp; 504 505 attr = (unsigned char) ((mode & ATTR_INV) ? (0x70) : (0x07)); 506 if (mode & ATTR_UL) attr |= 0x01; 507 if (mode & ATTR_BOLD) attr |= 0x08; 508 if (mode & ATTR_BLINK) attr |= 0x80; 509 510 cp = (u_short*)v->bitmap->plane; 511 cp[(dy * ip->cols) + dx] = (c << 8) | attr; 512 } 513 514 void 515 et_clear(ip, sy, sx, h, w) 516 struct ite_softc *ip; 517 int sy; 518 int sx; 519 int h; 520 int w; 521 { 522 /* et_clear and et_scroll both rely on ite passing arguments 523 * which describe continuous regions. For a VT200 terminal, 524 * this is safe behavior. 525 */ 526 view_t *v = viewview(ip->grf->g_viewdev); 527 u_short *dest; 528 int len; 529 530 dest = (u_short *)v->bitmap->plane + (sy * ip->cols) + sx; 531 for(len = w * h; len-- ;) 532 *dest++ = 0x2007; 533 } 534 535 void 536 et_scroll(ip, sy, sx, count, dir) 537 struct ite_softc *ip; 538 int sy; 539 int sx; 540 int count; 541 int dir; 542 { 543 view_t *v = viewview(ip->grf->g_viewdev); 544 u_short *fb; 545 u_short *src, *dst; 546 int len; 547 548 fb = (u_short*)v->bitmap->plane + sy * ip->cols; 549 switch (dir) { 550 case SCROLL_UP: 551 src = fb; 552 dst = fb - count * ip->cols; 553 len = (ip->bottom_margin + 1 - sy) * ip->cols; 554 break; 555 case SCROLL_DOWN: 556 src = fb; 557 dst = fb + count * ip->cols; 558 len = (ip->bottom_margin + 1 - (sy + count)) * ip->cols; 559 break; 560 case SCROLL_RIGHT: 561 src = fb + sx; 562 dst = fb + sx + count; 563 len = ip->cols - sx + count; 564 break; 565 case SCROLL_LEFT: 566 src = fb + sx; 567 dst = fb + sx - count; 568 len = ip->cols - sx; 569 break; 570 default: 571 return; 572 } 573 if (src > dst) { 574 while (len--) 575 *dst++ = *src++; 576 } 577 else { 578 src = &src[len]; 579 dst = &dst[len]; 580 while (len--) 581 *--dst = *--src; 582 } 583 } 584 585 static void 586 et_inittextmode(ip, etregs, loadfont) 587 struct ite_softc *ip; 588 et_sv_reg_t *etregs; 589 int loadfont; 590 { 591 volatile u_char *ba; 592 font_info *fd; 593 u_char *fb; 594 u_char *c, *f, tmp; 595 u_short z, y; 596 int s; 597 view_t *v = viewview(ip->grf->g_viewdev); 598 et_sv_reg_t loc_regs; 599 600 if (etregs == NULL) { 601 etregs = &loc_regs; 602 et_hwsave(etregs); 603 } 604 605 ba = ((ipriv_t*)ip->priv)->regkva; 606 fb = v->bitmap->plane; 607 608 #if defined(KFONT_8X8) 609 fd = &font_info_8x8; 610 #else 611 fd = &font_info_8x16; 612 #endif 613 614 if (loadfont) { /* XXX: We should set the colormap */ 615 /* 616 * set colors (B&W) 617 */ 618 vgaw(ba, VDAC_ADDRESS_W, 0); 619 for (z = 0; z < 256; z++) { 620 y = (z & 1) ? ((z > 7) ? 2 : 1) : 0; 621 622 vgaw(ba, VDAC_DATA, etconscolors[y][0]); 623 vgaw(ba, VDAC_DATA, etconscolors[y][1]); 624 vgaw(ba, VDAC_DATA, etconscolors[y][2]); 625 } 626 627 /* 628 * Enter a suitable mode to download the font. This 629 * basically means sequential addressing mode 630 */ 631 s = splhigh(); 632 633 WAttr(ba, 0x20 | ACT_ID_ATTR_MODE_CNTL, 0x0a); 634 WSeq(ba, SEQ_ID_MAP_MASK, 0x04); 635 WSeq(ba, SEQ_ID_MEMORY_MODE, 0x06); 636 WGfx(ba, GCT_ID_READ_MAP_SELECT, 0x02); 637 WGfx(ba, GCT_ID_GRAPHICS_MODE, 0x00); 638 WGfx(ba, GCT_ID_MISC, 0x04); 639 splx(s); 640 641 /* 642 * load text font into beginning of display memory. Each 643 * character cell is 32 bytes long (enough for 4 planes) 644 */ 645 for (z = 0, c = fb; z < 256 * 32; z++) 646 *c++ = 0; 647 648 c = (unsigned char *) (fb) + (32 * fd->font_lo); 649 f = fd->font_p; 650 z = fd->font_lo; 651 for (; z <= fd->font_hi; z++, c += (32 - fd->height)) 652 for (y = 0; y < fd->height; y++) { 653 *c++ = *f++; 654 } 655 } 656 657 /* 658 * Odd/Even addressing 659 */ 660 etregs->seq[SEQ_ID_MAP_MASK] = 0x03; 661 etregs->seq[SEQ_ID_MEMORY_MODE] = 0x03; 662 etregs->grf[GCT_ID_READ_MAP_SELECT] = 0x00; 663 etregs->grf[GCT_ID_GRAPHICS_MODE] = 0x10; 664 etregs->grf[GCT_ID_MISC] = 0x06; 665 666 /* 667 * Font height + underline location 668 */ 669 tmp = etregs->crt[CRT_ID_MAX_ROW_ADDRESS] & 0xe0; 670 etregs->crt[CRT_ID_MAX_ROW_ADDRESS] = tmp | (fd->height - 1); 671 tmp = etregs->crt[CRT_ID_UNDERLINE_LOC] & 0xe0; 672 etregs->crt[CRT_ID_UNDERLINE_LOC] = tmp | (fd->height - 1); 673 674 /* 675 * Cursor setup 676 */ 677 etregs->crt[CRT_ID_CURSOR_START] = 0x00; 678 etregs->crt[CRT_ID_CURSOR_END] = fd->height - 1; 679 etregs->crt[CRT_ID_CURSOR_LOC_HIGH] = 0x00; 680 etregs->crt[CRT_ID_CURSOR_LOC_LOW] = 0x00; 681 682 /* 683 * Enter text mode 684 */ 685 etregs->crt[CRT_ID_MODE_CONTROL] = 0xa3; 686 etregs->attr[ACT_ID_ATTR_MODE_CNTL] = 0x0a; 687 688 #if 1 689 if (loadfont || (etregs == &loc_regs)) 690 #else 691 if (etregs == &loc_regs) 692 #endif 693 et_hwrest(etregs); 694 } 695