1 /* $NetBSD: ite_et.c,v 1.38 2023/01/06 10:28:28 tsutsui 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 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __KERNEL_RCSID(0, "$NetBSD: ite_et.c,v 1.38 2023/01/06 10:28:28 tsutsui Exp $"); 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/conf.h> 34 #include <sys/ioctl.h> 35 #include <sys/malloc.h> 36 #include <sys/device.h> 37 #include <sys/device_impl.h> /* XXX autoconf abuse */ 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(struct grf_softc *); 79 static void view_init(struct ite_softc *); 80 static void view_deinit(struct ite_softc *); 81 static int iteet_ioctl(struct ite_softc *, u_long, void *, int, 82 struct lwp *); 83 static int ite_newsize(struct ite_softc *, struct itewinsize *); 84 static void et_inittextmode(struct ite_softc *, et_sv_reg_t *, int); 85 void et_cursor(struct ite_softc *ip, int flag); 86 void et_clear(struct ite_softc *ip, int sy, int sx, int h, int w); 87 void et_putc(struct ite_softc *ip, int c, int dy, int dx, int mode); 88 void et_scroll(struct ite_softc *ip, int sy, int sx, int count, 89 int dir); 90 91 /* 92 * grfet config stuff 93 */ 94 static void grfetattach(device_t, device_t, void *); 95 static int grfetmatch(device_t, cfdata_t, void *); 96 static int grfetprint(void *, const char *); 97 98 CFATTACH_DECL_NEW(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 static int 107 grfetmatch(device_t parent, cfdata_t cf, void *aux) 108 { 109 static int card_probed = -1; 110 static int did_consinit = 0; 111 grf_auxp_t *grf_auxp = aux; 112 extern const struct cdevsw view_cdevsw; 113 114 if (card_probed <= 0) { 115 if (card_probed == 0) /* Probed but failed */ 116 return 0; 117 card_probed = 0; 118 119 /* 120 * Check if the layers we depend on exist 121 */ 122 if (!(machineid & ATARI_HADES)) 123 return 0; 124 if (!et_probe_card()) 125 return 0; 126 if (grfabs_probe(&et_probe_video) == 0) 127 return 0; 128 viewprobe(); 129 card_probed = 1; /* Probed and found */ 130 } 131 132 if (atari_realconfig == 0) { 133 /* 134 * Early console init. Only match first unit. 135 */ 136 if (did_consinit) 137 return 0; 138 if ((*view_cdevsw.d_open)(cf->cf_unit, 0, 0, NULL)) 139 return 0; 140 cfdata_grf = cf; 141 did_consinit = 1; 142 return 1; 143 } 144 145 /* 146 * Normal config. When we are called directly from the grfbus, 147 * we only match the first unit. The attach function will call us for 148 * the other configured units. 149 */ 150 if (grf_auxp->from_bus_match 151 && ((did_consinit > 1) || !et_probe_card())) 152 return 0; 153 154 if (!grf_auxp->from_bus_match && (grf_auxp->unit != cf->cf_unit)) 155 return 0; 156 157 /* 158 * Final constraint: each grf needs a view.... 159 */ 160 if ((cfdata_grf == NULL) || (did_consinit > 1)) { 161 if ((*view_cdevsw.d_open)(cf->cf_unit, 0, 0, NULL)) 162 return 0; 163 } 164 did_consinit = 2; 165 return 1; 166 } 167 168 /* 169 * attach: initialize the grf-structure and try to attach an ite to us. 170 * note : self is NULL during early console init. 171 */ 172 static void 173 grfetattach(device_t parent, device_t self, void *aux) 174 { 175 static struct grf_softc congrf; 176 static int first_attach = 1; 177 grf_auxp_t *grf_bus_auxp = aux; 178 grf_auxp_t grf_auxp; 179 struct grf_softc *sc; 180 int maj; 181 extern const struct cdevsw grf_cdevsw; 182 183 /* 184 * find our major device number 185 */ 186 maj = cdevsw_lookup_major(&grf_cdevsw); 187 188 /* 189 * Handle exception case: early console init 190 */ 191 if (self == NULL) { 192 struct device itedev; 193 194 memset(&itedev, 0, sizeof(itedev)); 195 itedev.dv_private = &congrf; 196 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, &itedev, &congrf, grfetprint, 209 CFARGS_NONE); 210 return; 211 } 212 213 sc = device_private(self); 214 sc->g_device = self; 215 sc->g_unit = device_unit(self); 216 grfsp[sc->g_unit] = sc; 217 218 if ((cfdata_grf != NULL) && (sc->g_unit == congrf.g_unit)) { 219 /* 220 * We inited earlier just copy the info, take care 221 * not to copy the device struct though. 222 */ 223 memcpy(&sc->g_display, &congrf.g_display, 224 (char *)&sc[1] - (char *)&sc->g_display); 225 } else { 226 sc->g_grfdev = makedev(maj, sc->g_unit); 227 sc->g_itedev = (dev_t)-1; 228 sc->g_flags = GF_ALIVE; 229 sc->g_mode = grf_mode; 230 sc->g_conpri = 0; 231 sc->g_viewdev = sc->g_unit; 232 grfet_iteinit(sc); 233 grf_viewsync(sc); 234 } 235 236 aprint_normal(": %dx%d", sc->g_display.gd_dwidth, 237 sc->g_display.gd_dheight); 238 if (sc->g_display.gd_colors == 2) 239 aprint_normal(" monochrome\n"); 240 else 241 aprint_normal(" colors %d\n", sc->g_display.gd_colors); 242 243 /* 244 * try and attach an ite 245 */ 246 config_found(self, sc /* XXX */, grfetprint, CFARGS_NONE); 247 248 /* 249 * If attaching the first unit, go ahead and 'find' the rest of us 250 */ 251 if (first_attach) { 252 first_attach = 0; 253 grf_auxp.from_bus_match = 0; 254 for (grf_auxp.unit=0; grf_auxp.unit < NGRFET; grf_auxp.unit++) { 255 config_found(parent, (void*)&grf_auxp, 256 grf_bus_auxp->busprint, CFARGS_NONE); 257 } 258 } 259 } 260 261 static int 262 grfetprint(void *aux, const char *pnp) 263 { 264 265 if (pnp) /* XXX */ 266 aprint_normal("ite at %s", pnp); 267 return UNCONF; 268 } 269 270 /* 271 * Init ite portion of grf_softc struct 272 */ 273 static void 274 grfet_iteinit(struct grf_softc *sc) 275 { 276 277 sc->g_itecursor = et_cursor; 278 sc->g_iteputc = et_putc; 279 sc->g_iteclear = et_clear; 280 sc->g_itescroll = et_scroll; 281 sc->g_iteinit = view_init; 282 sc->g_itedeinit = view_deinit; 283 } 284 285 static void 286 view_deinit(struct ite_softc *ip) 287 { 288 ip->flags &= ~ITE_INITED; 289 } 290 291 static void 292 view_init(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 317 ip->priv = cci = malloc(sizeof(*cci), M_DEVBUF, M_WAITOK); 318 if (cci == NULL) 319 panic("No memory for ite-view"); 320 memset(cci, 0, sizeof(*cci)); 321 322 wsz.x = ite_default_x; 323 wsz.y = ite_default_y; 324 wsz.width = ite_default_width; 325 wsz.height = ite_default_height; 326 wsz.depth = ite_default_depth; 327 328 ite_newsize (ip, &wsz); 329 330 view = viewview(ip->grf->g_viewdev); 331 cci->regkva = view->bitmap->regs; 332 333 /* 334 * Only console will be turned on by default.. 335 */ 336 if (ip->flags & ITE_ISCONS) 337 ip->grf->g_mode(ip->grf, GM_GRFON, NULL, 0, 0); 338 339 /* 340 * Activate text-mode settings 341 */ 342 et_save = (save_area_t *)view->save_area; 343 if (et_save == NULL) 344 et_inittextmode(ip, NULL, view->flags & VF_DISPLAY); 345 else { 346 et_inittextmode(ip, &et_save->sv_regs, view->flags&VF_DISPLAY); 347 et_save->fb_size = ip->cols * ip->rows; 348 } 349 } 350 351 static int 352 ite_newsize(struct ite_softc *ip, struct itewinsize *winsz) 353 { 354 struct view_size vs; 355 int error = 0; 356 save_area_t *et_save; 357 view_t *view; 358 extern const struct cdevsw view_cdevsw; 359 360 vs.x = winsz->x; 361 vs.y = winsz->y; 362 vs.width = winsz->width; 363 vs.height = winsz->height; 364 vs.depth = winsz->depth; 365 366 error = (*view_cdevsw.d_ioctl)(ip->grf->g_viewdev, VIOCSSIZE, 367 (void *)&vs, 0, NOLWP); 368 view = viewview(ip->grf->g_viewdev); 369 370 /* 371 * Reinitialize our structs 372 */ 373 ip->cols = view->display.width / ip->font.width; 374 ip->rows = view->display.height / ip->font.height; 375 376 /* 377 * save new values so that future opens use them 378 * this may not be correct when we implement Virtual Consoles 379 */ 380 ite_default_height = view->display.height; 381 ite_default_width = view->display.width; 382 ite_default_x = view->display.x; 383 ite_default_y = view->display.y; 384 ite_default_depth = view->bitmap->depth; 385 386 et_save = (save_area_t *)view->save_area; 387 if (et_save == NULL) 388 et_inittextmode(ip, NULL, view->flags & VF_DISPLAY); 389 else { 390 et_inittextmode(ip, &et_save->sv_regs, 391 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(struct ite_softc *ip, u_long cmd, void * addr, int flag, 401 struct lwp *l) 402 { 403 struct winsize ws; 404 struct itewinsize *is; 405 int error = 0; 406 view_t *view = viewview(ip->grf->g_viewdev); 407 extern const struct cdevsw ite_cdevsw; 408 extern const struct cdevsw view_cdevsw; 409 410 switch (cmd) { 411 case ITEIOCSWINSZ: 412 is = (struct itewinsize *)addr; 413 414 if (ite_newsize(ip, is)) 415 error = ENOMEM; 416 else { 417 view = viewview(ip->grf->g_viewdev); 418 ws.ws_row = ip->rows; 419 ws.ws_col = ip->cols; 420 ws.ws_xpixel = view->display.width; 421 ws.ws_ypixel = view->display.height; 422 ite_reset(ip); 423 /* 424 * XXX tell tty about the change 425 * XXX this is messy, but works 426 */ 427 (*ite_cdevsw.d_ioctl)(ip->grf->g_itedev, TIOCSWINSZ, 428 (void *)&ws, 0, l); 429 } 430 break; 431 case VIOCSCMAP: 432 case VIOCGCMAP: 433 /* 434 * XXX watchout for that NOLWP. its not really the kernel 435 * XXX talking these two commands don't use the proc pointer 436 * XXX though. 437 */ 438 error = (*view_cdevsw.d_ioctl)(ip->grf->g_viewdev, cmd, addr, 439 flag, NOLWP); 440 break; 441 default: 442 error = EPASSTHROUGH; 443 break; 444 } 445 return error; 446 } 447 448 void 449 et_cursor(struct ite_softc *ip, int flag) 450 { 451 volatile u_char *ba; 452 view_t *v; 453 u_long cpos; 454 455 ba = ((ipriv_t*)ip->priv)->regkva; 456 v = viewview(ip->grf->g_viewdev); 457 458 /* 459 * Don't update the cursor when not on display 460 */ 461 if (!(v->flags & VF_DISPLAY)) 462 return; 463 464 switch (flag) { 465 case DRAW_CURSOR: 466 /*WCrt(ba, CRT_ID_CURSOR_START, & ~0x20); */ 467 case MOVE_CURSOR: 468 cpos = RCrt(ba, CRT_ID_START_ADDR_LOW) & 0xff; 469 cpos |= (RCrt(ba, CRT_ID_START_ADDR_HIGH) & 0xff) << 8; 470 cpos += ip->curx + ip->cury * ip->cols; 471 WCrt(ba, CRT_ID_CURSOR_LOC_LOW, cpos & 0xff); 472 WCrt(ba, CRT_ID_CURSOR_LOC_HIGH, (cpos >> 8) & 0xff); 473 WCrt(ba, CTR_ID_EXT_START, (cpos >> (16-2)) & 0x0c); 474 475 ip->cursorx = ip->curx; 476 ip->cursory = ip->cury; 477 break; 478 case ERASE_CURSOR: 479 /*WCrt(ba, CRT_ID_CURSOR_START, | 0x20); */ 480 case START_CURSOROPT: 481 case END_CURSOROPT: 482 default: 483 break; 484 } 485 } 486 487 void 488 et_putc(struct ite_softc *ip, int c, int dy, int dx, int mode) 489 { 490 view_t *v = viewview(ip->grf->g_viewdev); 491 u_char attr; 492 u_short *cp; 493 494 /* 495 * Handle DEC special graphics character by 'ESC ( B' sequence. 496 * Note we assume all font data (fontdata_8x8 and fontdata_8x16) 497 * contain DEC graphics glyph (for 0x5f to 0x7e) at 0x00 to 0x1F. 498 */ 499 if (*ip->GL == CSET_DECGRAPH) { 500 if (ip->font.font_lo == 0 && c >= 0x5f && c <= 0x7e) 501 c -= 0x5f; 502 } 503 504 attr = (unsigned char) ((mode & ATTR_INV) ? (0x70) : (0x07)); 505 if (mode & ATTR_UL) attr |= 0x01; 506 if (mode & ATTR_BOLD) attr |= 0x08; 507 if (mode & ATTR_BLINK) attr |= 0x80; 508 509 cp = (u_short*)v->bitmap->plane; 510 cp[(dy * ip->cols) + dx] = (c << 8) | attr; 511 } 512 513 void 514 et_clear(struct ite_softc *ip, int sy, int sx, int h, int w) 515 { 516 /* et_clear and et_scroll both rely on ite passing arguments 517 * which describe continuous regions. For a VT200 terminal, 518 * this is safe behavior. 519 */ 520 view_t *v = viewview(ip->grf->g_viewdev); 521 u_short *dest; 522 int len; 523 524 dest = (u_short *)v->bitmap->plane + (sy * ip->cols) + sx; 525 for (len = w * h; len-- ;) 526 *dest++ = 0x2007; 527 } 528 529 void 530 et_scroll(struct ite_softc *ip, int sy, int sx, int count, int dir) 531 { 532 view_t *v = viewview(ip->grf->g_viewdev); 533 u_short *fb; 534 u_short *src, *dst; 535 int len; 536 537 fb = (u_short*)v->bitmap->plane + sy * ip->cols; 538 switch (dir) { 539 case SCROLL_UP: 540 src = fb; 541 dst = fb - count * ip->cols; 542 len = (ip->bottom_margin + 1 - sy) * ip->cols; 543 break; 544 case SCROLL_DOWN: 545 src = fb; 546 dst = fb + count * ip->cols; 547 len = (ip->bottom_margin + 1 - (sy + count)) * ip->cols; 548 break; 549 case SCROLL_RIGHT: 550 src = fb + sx; 551 dst = fb + sx + count; 552 len = ip->cols - sx + count; 553 break; 554 case SCROLL_LEFT: 555 src = fb + sx; 556 dst = fb + sx - count; 557 len = ip->cols - sx; 558 break; 559 default: 560 return; 561 } 562 if (src > dst) { 563 while (len--) 564 *dst++ = *src++; 565 } else { 566 src = &src[len]; 567 dst = &dst[len]; 568 while (len--) 569 *--dst = *--src; 570 } 571 } 572 573 static void 574 et_inittextmode(struct ite_softc *ip, et_sv_reg_t *etregs, int loadfont) 575 { 576 volatile u_char *ba; 577 font_info *fd; 578 u_char *fb; 579 u_char *c, *f, tmp; 580 u_short z, y; 581 int s; 582 view_t *v = viewview(ip->grf->g_viewdev); 583 et_sv_reg_t loc_regs; 584 585 if (etregs == NULL) { 586 etregs = &loc_regs; 587 et_hwsave(etregs); 588 } 589 590 ba = ((ipriv_t*)ip->priv)->regkva; 591 fb = v->bitmap->plane; 592 593 #if defined(KFONT_8X8) 594 fd = &font_info_8x8; 595 #else 596 fd = &font_info_8x16; 597 #endif 598 599 if (loadfont) { /* XXX: We should set the colormap */ 600 /* 601 * set colors (B&W) 602 */ 603 vgaw(ba, VDAC_ADDRESS_W, 0); 604 for (z = 0; z < 256; z++) { 605 y = (z & 1) ? ((z > 7) ? 2 : 1) : 0; 606 607 vgaw(ba, VDAC_DATA, etconscolors[y][0]); 608 vgaw(ba, VDAC_DATA, etconscolors[y][1]); 609 vgaw(ba, VDAC_DATA, etconscolors[y][2]); 610 } 611 612 /* 613 * Enter a suitable mode to download the font. This 614 * basically means sequential addressing mode 615 */ 616 s = splhigh(); 617 618 WAttr(ba, 0x20 | ACT_ID_ATTR_MODE_CNTL, 0x0a); 619 WSeq(ba, SEQ_ID_MAP_MASK, 0x04); 620 WSeq(ba, SEQ_ID_MEMORY_MODE, 0x06); 621 WGfx(ba, GCT_ID_READ_MAP_SELECT, 0x02); 622 WGfx(ba, GCT_ID_GRAPHICS_MODE, 0x00); 623 WGfx(ba, GCT_ID_MISC, 0x04); 624 splx(s); 625 626 /* 627 * load text font into beginning of display memory. Each 628 * character cell is 32 bytes long (enough for 4 planes) 629 */ 630 for (z = 0, c = fb; z < 256 * 32; z++) 631 *c++ = 0; 632 633 c = (unsigned char *) (fb) + (32 * fd->font_lo); 634 f = fd->font_p; 635 z = fd->font_lo; 636 for (; z <= fd->font_hi; z++, c += (32 - fd->height)) 637 for (y = 0; y < fd->height; y++) { 638 *c++ = *f++; 639 } 640 } 641 642 /* 643 * Odd/Even addressing 644 */ 645 etregs->seq[SEQ_ID_MAP_MASK] = 0x03; 646 etregs->seq[SEQ_ID_MEMORY_MODE] = 0x03; 647 etregs->grf[GCT_ID_READ_MAP_SELECT] = 0x00; 648 etregs->grf[GCT_ID_GRAPHICS_MODE] = 0x10; 649 etregs->grf[GCT_ID_MISC] = 0x06; 650 651 /* 652 * Font height + underline location 653 */ 654 tmp = etregs->crt[CRT_ID_MAX_ROW_ADDRESS] & 0xe0; 655 etregs->crt[CRT_ID_MAX_ROW_ADDRESS] = tmp | (fd->height - 1); 656 tmp = etregs->crt[CRT_ID_UNDERLINE_LOC] & 0xe0; 657 etregs->crt[CRT_ID_UNDERLINE_LOC] = tmp | (fd->height - 1); 658 659 /* 660 * Cursor setup 661 */ 662 etregs->crt[CRT_ID_CURSOR_START] = 0x00; 663 etregs->crt[CRT_ID_CURSOR_END] = fd->height - 1; 664 etregs->crt[CRT_ID_CURSOR_LOC_HIGH] = 0x00; 665 etregs->crt[CRT_ID_CURSOR_LOC_LOW] = 0x00; 666 667 /* 668 * Enter text mode 669 */ 670 etregs->crt[CRT_ID_MODE_CONTROL] = 0xa3; 671 etregs->attr[ACT_ID_ATTR_MODE_CNTL] = 0x0a; 672 673 #if 1 674 if (loadfont || (etregs == &loc_regs)) 675 #else 676 if (etregs == &loc_regs) 677 #endif 678 et_hwrest(etregs); 679 } 680