1 /* $Id: light.c,v 1.5 2007/03/04 06:00:39 christos Exp $ */ 2 3 /* 4 * Copyright (c) 2006 Stephen M. Rumble 5 * Copyright (c) 2003 Ilpo Ruotsalainen 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * 30 * <<Id: LICENSE_GC,v 1.1 2001/10/01 23:24:05 cgd Exp>> 31 */ 32 33 /* 34 * SGI "Light" graphics, a.k.a. "Entry", "Starter", "LG1", and "LG2". 35 * 36 * 1024x768 8bpp at 60Hz. 37 * 38 * This driver supports the boards found in Indigo R3k and R4k machines. 39 * There is a Crimson variant, but the register offsets differ significantly. 40 * 41 * Light's REX chip is the precursor of the REX3 found in "newport", hence 42 * much similarity exists. 43 */ 44 45 #include <sys/cdefs.h> 46 __KERNEL_RCSID(0, "$NetBSD: light.c,v 1.5 2007/03/04 06:00:39 christos Exp $"); 47 48 #include <sys/param.h> 49 #include <sys/systm.h> 50 #include <sys/device.h> 51 #include <sys/malloc.h> 52 53 #include <machine/sysconf.h> 54 55 #include <dev/wscons/wsconsio.h> 56 #include <dev/wscons/wsdisplayvar.h> 57 #include <dev/wsfont/wsfont.h> 58 59 #include <sgimips/gio/giovar.h> 60 #include <sgimips/gio/lightvar.h> 61 #include <sgimips/gio/lightreg.h> 62 63 struct light_softc { 64 struct device sc_dev; 65 66 struct light_devconfig *sc_dc; 67 }; 68 69 struct light_devconfig { 70 uint32_t dc_addr; 71 72 bus_space_tag_t dc_st; 73 bus_space_handle_t dc_sh; 74 75 int dc_boardrev; 76 int dc_font; 77 struct wsdisplay_font *dc_fontdata; 78 }; 79 80 /* always 1024x768x8 */ 81 #define LIGHT_XRES 1024 82 #define LIGHT_YRES 768 83 #define LIGHT_DEPTH 8 84 85 static int light_match(struct device *, struct cfdata *, void *); 86 static void light_attach(struct device *, struct device *, void *); 87 88 CFATTACH_DECL(light, sizeof(struct light_softc), light_match, light_attach, 89 NULL, NULL); 90 91 /* wsdisplay_emulops */ 92 static void light_cursor(void *, int, int, int); 93 static int light_mapchar(void *, int, unsigned int *); 94 static void light_putchar(void *, int, int, u_int, long); 95 static void light_copycols(void *, int, int, int, int); 96 static void light_erasecols(void *, int, int, int, long); 97 static void light_copyrows(void *, int, int, int); 98 static void light_eraserows(void *, int, int, long); 99 static int light_allocattr(void *, int, int, int, long *); 100 101 /* wsdisplay_accessops */ 102 static int light_ioctl(void *, void *, u_long, void *, int, struct lwp *); 103 static paddr_t light_mmap(void *, void *, off_t, int); 104 static int light_alloc_screen(void *, const struct wsscreen_descr *, 105 void **, int *, int *, long *); 106 static void light_free_screen(void *, void *); 107 static int light_show_screen(void *, void *, int, 108 void (*)(void *, int, int), void *); 109 110 static const struct wsdisplay_accessops light_accessops = { 111 .ioctl = light_ioctl, 112 .mmap = light_mmap, 113 .alloc_screen = light_alloc_screen, 114 .free_screen = light_free_screen, 115 .show_screen = light_show_screen, 116 .load_font = NULL, 117 .pollc = NULL, 118 .scroll = NULL 119 }; 120 121 static const struct wsdisplay_emulops light_emulops = { 122 .cursor = light_cursor, 123 .mapchar = light_mapchar, 124 .putchar = light_putchar, 125 .copycols = light_copycols, 126 .erasecols = light_erasecols, 127 .copyrows = light_copyrows, 128 .eraserows = light_eraserows, 129 .allocattr = light_allocattr, 130 .replaceattr = NULL 131 }; 132 133 static const struct wsscreen_descr light_screen = { 134 .name = "1024x768", 135 .ncols = 128, 136 .nrows = 48, 137 .textops = &light_emulops, 138 .fontwidth = 8, 139 .fontheight = 16, 140 .capabilities = WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_REVERSE 141 }; 142 143 const struct wsscreen_descr *_light_screenlist[] = { 144 &light_screen 145 }; 146 147 static const struct wsscreen_list light_screenlist = { 148 sizeof(_light_screenlist) / sizeof(_light_screenlist[0]), 149 _light_screenlist 150 }; 151 152 static struct light_devconfig light_console_dc; 153 static int light_is_console = 0; 154 155 #define LIGHT_ATTR_ENCODE(fg, bg) (((fg << 8) & 0xff00) | (bg * 0x00ff)) 156 #define LIGHT_ATTR_FG(attr) ((attr >> 8) & 0x00ff) 157 #define LIGHT_ATTR_BG(attr) (attr & 0x00ff) 158 159 #define LIGHT_IS_LG1(_rev) ((_rev) < 2) /* else LG2 */ 160 161 /******************************************************************************* 162 * REX routines and helper functions 163 ******************************************************************************/ 164 165 static uint32_t 166 rex_read(struct light_devconfig *dc, uint32_t rset, uint32_t r) 167 { 168 169 return (bus_space_read_4(dc->dc_st, dc->dc_sh, rset + r)); 170 } 171 172 static void 173 rex_write(struct light_devconfig *dc, uint32_t rset, uint32_t r, uint32_t v) 174 { 175 176 bus_space_write_4(dc->dc_st, dc->dc_sh, rset + r, v); 177 } 178 179 static uint8_t 180 rex_vc1_read(struct light_devconfig *dc) 181 { 182 183 rex_write(dc, REX_PAGE1_GO, REX_P1REG_CFGSEL, REX_CFGSEL_VC1_SYSCTL); 184 rex_read(dc, REX_PAGE1_GO, REX_P1REG_VC1_ADDRDATA); 185 return (rex_read(dc, REX_PAGE1_SET, REX_P1REG_VC1_ADDRDATA)); 186 } 187 188 static void 189 rex_vc1_write(struct light_devconfig *dc, uint8_t val) 190 { 191 192 rex_write(dc, REX_PAGE1_GO, REX_P1REG_CFGSEL, REX_CFGSEL_VC1_SYSCTL); 193 rex_write(dc, REX_PAGE1_SET, REX_P1REG_VC1_ADDRDATA, val); 194 rex_write(dc, REX_PAGE1_GO, REX_P1REG_VC1_ADDRDATA, val); 195 } 196 197 static void 198 rex_wait(struct light_devconfig *dc) 199 { 200 201 while (rex_read(dc, REX_PAGE1_SET,REX_P1REG_CFGMODE) & REX_CFGMODE_BUSY) 202 ; 203 } 204 205 static int 206 rex_revision(struct light_devconfig *dc) 207 { 208 209 rex_write(dc, REX_PAGE1_SET, REX_P1REG_CFGSEL, REX_CFGSEL_VC1_LADDR); 210 rex_read(dc, REX_PAGE1_GO, REX_P1REG_WCLOCKREV); 211 return (rex_read(dc, REX_PAGE1_SET, REX_P1REG_WCLOCKREV) & 0x7); 212 } 213 214 static void 215 rex_copy_rect(struct light_devconfig *dc, int from_x, int from_y, int to_x, 216 int to_y, int width, int height) 217 { 218 int dx, dy, ystarti, yendi; 219 220 dx = from_x - to_x; 221 dy = from_y - to_y; 222 223 /* adjust for y. NB: STOPONX, STOPONY are inclusive */ 224 if (to_y > from_y) { 225 ystarti = to_y + height - 1; 226 yendi = to_y; 227 } else { 228 ystarti = to_y; 229 yendi = to_y + height - 1; 230 } 231 232 rex_wait(dc); 233 234 rex_write(dc, REX_PAGE0_SET, REX_P0REG_XSTARTI, to_x); 235 rex_write(dc, REX_PAGE0_SET, REX_P0REG_XENDI, to_x + width); 236 rex_write(dc, REX_PAGE0_SET, REX_P0REG_YSTARTI, ystarti); 237 rex_write(dc, REX_PAGE0_SET, REX_P0REG_YENDI, yendi); 238 rex_write(dc, REX_PAGE0_SET, REX_P0REG_COMMAND, REX_OP_DRAW | 239 REX_LOGICOP_SRC | REX_OP_FLG_LOGICSRC | REX_OP_FLG_QUADMODE | 240 REX_OP_FLG_BLOCK | REX_OP_FLG_STOPONX | REX_OP_FLG_STOPONY); 241 rex_write(dc, REX_PAGE0_GO, REX_P0REG_XYMOVE, 242 ((dx << 16) & 0xffff0000) | (dy & 0x0000ffff)); 243 } 244 245 static void 246 rex_fill_rect(struct light_devconfig *dc, int from_x, int from_y, int to_x, 247 int to_y, long attr) 248 { 249 250 rex_wait(dc); 251 252 rex_write(dc, REX_PAGE0_SET, REX_P0REG_YSTARTI, from_y); 253 rex_write(dc, REX_PAGE0_SET, REX_P0REG_YENDI, to_y); 254 rex_write(dc, REX_PAGE0_SET, REX_P0REG_XSTARTI, from_x); 255 rex_write(dc, REX_PAGE0_SET, REX_P0REG_XENDI, to_x); 256 rex_write(dc, REX_PAGE0_SET, REX_P0REG_COLORREDI, LIGHT_ATTR_BG(attr)); 257 rex_write(dc, REX_PAGE0_SET, REX_P0REG_COMMAND, REX_OP_DRAW | 258 REX_LOGICOP_SRC | REX_OP_FLG_QUADMODE | REX_OP_FLG_BLOCK | 259 REX_OP_FLG_STOPONX | REX_OP_FLG_STOPONY); 260 rex_read(dc, REX_PAGE0_GO, REX_P0REG_COMMAND); 261 } 262 263 /******************************************************************************* 264 * match/attach functions 265 ******************************************************************************/ 266 267 static int 268 light_match(struct device *parent, struct cfdata *self, void *aux) 269 { 270 struct gio_attach_args *ga = aux; 271 272 if (ga->ga_addr != LIGHT_ADDR_0 && ga->ga_addr != LIGHT_ADDR_1) 273 return (0); 274 275 if (platform.badaddr( 276 (void *)(ga->ga_ioh + REX_PAGE1_SET + REX_P1REG_XYOFFSET), 277 sizeof(uint32_t))) 278 return (0); 279 280 if (bus_space_read_4(ga->ga_iot, ga->ga_ioh, 281 REX_PAGE1_SET + REX_P1REG_XYOFFSET) != 0x08000800) 282 return (0); 283 284 return (1); 285 } 286 287 static void 288 light_attach_common(struct light_devconfig *dc, struct gio_attach_args *ga) 289 { 290 291 dc->dc_addr = ga->ga_addr; 292 dc->dc_st = ga->ga_iot; 293 dc->dc_sh = ga->ga_ioh; 294 295 dc->dc_boardrev = rex_revision(dc); 296 297 wsfont_init(); 298 299 dc->dc_font = wsfont_find(NULL, 8, 16, 0, WSDISPLAY_FONTORDER_L2R, 300 WSDISPLAY_FONTORDER_L2R); 301 302 if (dc->dc_font < 0) 303 panic("light_attach_common: no suitable fonts"); 304 305 if (wsfont_lock(dc->dc_font, &dc->dc_fontdata)) 306 panic("light_attach_common: unable to lock font data"); 307 308 rex_vc1_write(dc, rex_vc1_read(dc) & ~(VC1_SYSCTL_CURSOR | 309 VC1_SYSCTL_CURSOR_ON)); 310 rex_fill_rect(dc, 0, 0, LIGHT_XRES - 1, LIGHT_YRES - 1, 0); 311 } 312 313 static void 314 light_attach(struct device *parent, struct device *self, void *aux) 315 { 316 struct gio_attach_args *ga = aux; 317 struct light_softc *sc = (void *)self; 318 struct wsemuldisplaydev_attach_args wa; 319 320 if (light_is_console && ga->ga_addr == light_console_dc.dc_addr) { 321 wa.console = 1; 322 sc->sc_dc = &light_console_dc; 323 } else { 324 wa.console = 0; 325 sc->sc_dc = malloc(sizeof(struct light_devconfig), M_DEVBUF, 326 M_WAITOK | M_ZERO); 327 if (sc->sc_dc == NULL) 328 panic("light_attach: out of memory"); 329 330 light_attach_common(sc->sc_dc, ga); 331 } 332 333 aprint_naive(": Display adapter\n"); 334 335 aprint_normal(": SGI LG%d (board revision %d)\n", 336 LIGHT_IS_LG1(sc->sc_dc->dc_boardrev) ? 1 : 2, 337 sc->sc_dc->dc_boardrev); 338 339 wa.scrdata = &light_screenlist; 340 wa.accessops = &light_accessops; 341 wa.accesscookie = sc->sc_dc; 342 343 config_found(&sc->sc_dev, &wa, wsemuldisplaydevprint); 344 } 345 346 int 347 light_cnattach(struct gio_attach_args *ga) 348 { 349 350 if (!light_match(NULL, NULL, ga)) 351 return (ENXIO); 352 353 light_attach_common(&light_console_dc, ga); 354 355 wsdisplay_cnattach(&light_screen, &light_console_dc, 0, 0, 356 LIGHT_ATTR_ENCODE(WSCOL_WHITE, WSCOL_BLACK)); 357 358 light_is_console = 1; 359 360 return (0); 361 } 362 363 /******************************************************************************* 364 * wsdisplay_emulops 365 ******************************************************************************/ 366 367 static void 368 light_cursor(void *c, int on, int row, int col) 369 { 370 /* XXX */ 371 } 372 373 static int 374 light_mapchar(void *c, int ch, unsigned int *cp) 375 { 376 struct light_devconfig *dc = (void *)c; 377 378 if (dc->dc_fontdata->encoding != WSDISPLAY_FONTENC_ISO) { 379 ch = wsfont_map_unichar(dc->dc_fontdata, ch); 380 381 if (ch < 0) 382 goto fail; 383 } 384 385 if (ch < dc->dc_fontdata->firstchar || 386 ch >= dc->dc_fontdata->firstchar + dc->dc_fontdata->numchars) 387 goto fail; 388 389 *cp = ch; 390 return 5; 391 392 fail: 393 *cp = ' '; 394 return 0; 395 } 396 397 static void 398 light_putchar(void *c, int row, int col, u_int ch, long attr) 399 { 400 struct light_devconfig *dc = c; 401 struct wsdisplay_font *font = dc->dc_fontdata; 402 uint8_t *bitmap; 403 uint32_t pattern; 404 int i, x, y; 405 406 bitmap = (u_int8_t *)font->data + 407 ((ch - font->firstchar) * font->fontheight * font->stride); 408 x = col * font->fontwidth; 409 y = row * font->fontheight; 410 411 rex_wait(dc); 412 413 rex_write(dc, REX_PAGE0_SET, REX_P0REG_YSTARTI, y); 414 rex_write(dc, REX_PAGE0_SET, REX_P0REG_YENDI, y + font->fontheight - 1); 415 rex_write(dc, REX_PAGE0_SET, REX_P0REG_XSTARTI, x); 416 rex_write(dc, REX_PAGE0_SET, REX_P0REG_XENDI, x + font->fontwidth - 1); 417 rex_write(dc, REX_PAGE0_SET, REX_P0REG_COLORREDI, LIGHT_ATTR_FG(attr)); 418 rex_write(dc, REX_PAGE0_SET, REX_P0REG_COLORBACK, LIGHT_ATTR_BG(attr)); 419 rex_write(dc, REX_PAGE0_GO, REX_P0REG_COMMAND, REX_OP_NOP); 420 421 rex_wait(dc); 422 423 rex_write(dc, REX_PAGE0_SET, REX_P0REG_COMMAND, REX_OP_DRAW | 424 REX_LOGICOP_SRC | REX_OP_FLG_ENZPATTERN | REX_OP_FLG_QUADMODE | 425 REX_OP_FLG_XYCONTINUE | REX_OP_FLG_STOPONX | REX_OP_FLG_BLOCK | 426 REX_OP_FLG_LENGTH32 | REX_OP_FLG_ZOPAQUE); 427 428 for (i = 0; i < font->fontheight; i++) { 429 /* XXX assumes font->fontwidth == 8 */ 430 pattern = *bitmap << 24; 431 rex_write(dc, REX_PAGE0_GO, REX_P0REG_ZPATTERN, pattern); 432 bitmap += font->stride; 433 } 434 } 435 436 /* copy set of columns within the same line */ 437 static void 438 light_copycols(void *c, int row, int srccol, int dstcol, int ncols) 439 { 440 struct light_devconfig *dc = c; 441 struct wsdisplay_font *font = dc->dc_fontdata; 442 int from_x, from_y, to_x, to_y, width, height; 443 444 from_x = srccol * font->fontwidth; 445 from_y = row * font->fontheight; 446 to_x = dstcol * font->fontwidth; 447 to_y = from_y; 448 width = ncols * font->fontwidth; 449 height = font->fontheight; 450 451 rex_copy_rect(c, from_x, from_y, to_x, to_y, width, height); 452 } 453 454 /* erase a set of columns in the same line */ 455 static void 456 light_erasecols(void *c, int row, int startcol, int ncols, long attr) 457 { 458 struct light_devconfig *dc = c; 459 struct wsdisplay_font *font = dc->dc_fontdata; 460 int from_x, from_y, to_x, to_y; 461 462 from_x = startcol * font->fontwidth; 463 from_y = row * font->fontheight; 464 to_x = from_x + (ncols * font->fontwidth) - 1; 465 to_y = from_y + font->fontheight - 1; 466 467 rex_fill_rect(c, from_x, from_y, to_x, to_y, attr); 468 } 469 470 /* copy a set of complete rows */ 471 static void 472 light_copyrows(void *c, int srcrow, int dstrow, int nrows) 473 { 474 struct light_devconfig *dc = c; 475 struct wsdisplay_font *font = dc->dc_fontdata; 476 int from_x, from_y, to_x, to_y, width, height; 477 478 from_x = 0; 479 from_y = srcrow * font->fontheight; 480 to_x = 0; 481 to_y = dstrow * font->fontheight; 482 width = LIGHT_XRES; 483 height = nrows * font->fontheight; 484 485 rex_copy_rect(c, from_x, from_y, to_x, to_y, width, height); 486 } 487 488 /* erase a set of complete rows */ 489 static void 490 light_eraserows(void *c, int row, int nrows, long attr) 491 { 492 struct light_devconfig *dc = c; 493 struct wsdisplay_font *font = dc->dc_fontdata; 494 int from_x, from_y, to_x, to_y; 495 496 from_x = 0; 497 from_y = row * font->fontheight; 498 to_x = LIGHT_XRES - 1; 499 to_y = from_y + (nrows * font->fontheight) - 1; 500 501 rex_fill_rect(c, from_x, from_y, to_x, to_y, attr); 502 } 503 504 static int 505 light_allocattr(void *c, int fg, int bg, int flags, long *attr) 506 { 507 508 if (flags & ~(WSATTR_WSCOLORS | WSATTR_HILIT | WSATTR_REVERSE)) 509 return (EINVAL); 510 511 if ((flags & WSATTR_WSCOLORS) == 0) { 512 fg = WSCOL_WHITE; 513 bg = WSCOL_BLACK; 514 } 515 516 if (flags & WSATTR_HILIT) 517 fg += 8; 518 519 if (flags & WSATTR_REVERSE) { 520 int tmp = fg; 521 fg = bg; 522 bg = tmp; 523 } 524 525 *attr = LIGHT_ATTR_ENCODE(fg, bg); 526 return (0); 527 } 528 529 /******************************************************************************* 530 * wsdisplay_accessops 531 ******************************************************************************/ 532 533 static int 534 light_ioctl(void *c, void *vs, u_long cmd, void *data, int flag, 535 struct lwp *l) 536 { 537 struct wsdisplay_fbinfo *fbinfo = (struct wsdisplay_fbinfo *)data; 538 539 switch (cmd) { 540 case WSDISPLAYIO_GINFO: 541 fbinfo->width = LIGHT_XRES; 542 fbinfo->height = LIGHT_YRES; 543 fbinfo->depth = LIGHT_DEPTH; 544 fbinfo->cmsize = 1 << LIGHT_DEPTH; 545 return (0); 546 547 case WSDISPLAYIO_GMODE: 548 *(u_int *)data = WSDISPLAYIO_MODE_EMUL; 549 break; 550 551 case WSDISPLAYIO_GTYPE: 552 *(u_int *)data = WSDISPLAY_TYPE_LIGHT; 553 return (0); 554 555 case WSDISPLAYIO_SVIDEO: 556 /* 557 * Turning off VC1 will stop refreshing the video ram (or so I 558 * suspect). We'll blank the screen after bringing it back up, 559 * since that's nicer than displaying garbage. 560 */ 561 if (*(u_int *)data == WSDISPLAYIO_VIDEO_OFF) 562 rex_vc1_write(c,rex_vc1_read(c) & ~VC1_SYSCTL_VIDEO_ON); 563 else { 564 rex_vc1_write(c, rex_vc1_read(c) | VC1_SYSCTL_VIDEO_ON); 565 rex_fill_rect(c, 0, 0, LIGHT_XRES-1, LIGHT_YRES-1, 0); 566 } 567 return (0); 568 } 569 570 return (EPASSTHROUGH); 571 } 572 573 static paddr_t 574 light_mmap(void *c, void *vs, off_t off, int prot) 575 { 576 struct light_devconfig *dc = c; 577 578 if (off >= 0x7fff) 579 return (-1); 580 581 return (mips_btop(dc->dc_addr + off)); 582 } 583 584 static int 585 light_alloc_screen(void *c, const struct wsscreen_descr *type, void **cookiep, 586 int *curxp, int *curyp, long *attr) 587 { 588 589 return (ENOMEM); 590 } 591 592 static void 593 light_free_screen(void *c, void *cookie) 594 { 595 596 panic("light_free_screen"); 597 } 598 599 static int 600 light_show_screen(void *c, void *cookie, int waitok, 601 void (*cb)(void *, int, int), void *cbarg) 602 { 603 604 return (0); 605 } 606