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