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