1 /* $NetBSD: ffb.c,v 1.8 2004/07/19 01:04:35 heas Exp $ */ 2 /* $OpenBSD: creator.c,v 1.20 2002/07/30 19:48:15 jason Exp $ */ 3 4 /* 5 * Copyright (c) 2002 Jason L. Wright (jason@thought.net) 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. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by Jason L. Wright 19 * 4. The name of the author may not be used to endorse or promote products 20 * derived from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #include <sys/cdefs.h> 36 __KERNEL_RCSID(0, "$NetBSD: ffb.c,v 1.8 2004/07/19 01:04:35 heas Exp $"); 37 38 #include <sys/types.h> 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/kernel.h> 42 #include <sys/device.h> 43 #include <sys/conf.h> 44 45 #include <machine/bus.h> 46 #include <machine/autoconf.h> 47 #include <machine/openfirm.h> 48 49 #include <dev/wscons/wsconsio.h> 50 #include <dev/wscons/wsdisplayvar.h> 51 #include <dev/wscons/wscons_raster.h> 52 #include <dev/rasops/rasops.h> 53 54 #include <sparc64/dev/ffbreg.h> 55 #include <sparc64/dev/ffbvar.h> 56 57 struct wsscreen_descr ffb_stdscreen = { 58 "sunffb", 59 0, 0, /* will be filled in -- XXX shouldn't, it's global. */ 60 0, 61 0, 0, 62 WSSCREEN_REVERSE | WSSCREEN_WSCOLORS 63 }; 64 65 const struct wsscreen_descr *ffb_scrlist[] = { 66 &ffb_stdscreen, 67 /* XXX other formats? */ 68 }; 69 70 struct wsscreen_list ffb_screenlist = { 71 sizeof(ffb_scrlist) / sizeof(struct wsscreen_descr *), 72 ffb_scrlist 73 }; 74 75 int ffb_ioctl(void *, u_long, caddr_t, int, struct proc *); 76 int ffb_alloc_screen(void *, const struct wsscreen_descr *, void **, 77 int *, int *, long *); 78 static int ffb_blank(struct ffb_softc *, u_long, u_int *); 79 void ffb_free_screen(void *, void *); 80 int ffb_show_screen(void *, void *, int, void (*cb)(void *, int, int), 81 void *); 82 paddr_t ffb_mmap(void *, off_t, int); 83 void ffb_ras_fifo_wait(struct ffb_softc *, int); 84 void ffb_ras_wait(struct ffb_softc *); 85 void ffb_ras_init(struct ffb_softc *); 86 void ffb_ras_copyrows(void *, int, int, int); 87 void ffb_ras_erasecols(void *, int, int, int, long int); 88 void ffb_ras_eraserows(void *, int, int, long int); 89 void ffb_ras_do_cursor(struct rasops_info *); 90 void ffb_ras_fill(struct ffb_softc *); 91 void ffb_ras_setfg(struct ffb_softc *, int32_t); 92 93 struct wsdisplay_accessops ffb_accessops = { 94 ffb_ioctl, 95 ffb_mmap, 96 ffb_alloc_screen, 97 ffb_free_screen, 98 ffb_show_screen, 99 NULL, /* load font */ 100 NULL, /* pollc */ 101 NULL, /* getwschar */ 102 NULL, /* putwschar */ 103 NULL, /* scroll */ 104 }; 105 106 void 107 ffb_attach(struct ffb_softc *sc) 108 { 109 struct wsemuldisplaydev_attach_args waa; 110 char *model; 111 int btype; 112 int maxrow, maxcol; 113 u_int blank = WSDISPLAYIO_VIDEO_ON; 114 char buf[6+1]; 115 116 printf(":"); 117 118 if (sc->sc_type == FFB_CREATOR) { 119 btype = prom_getpropint(sc->sc_node, "board_type", 0); 120 if ((btype & 7) == 3) 121 printf(" Creator3D"); 122 else 123 printf(" Creator"); 124 } else 125 printf(" Elite3D"); 126 127 model = prom_getpropstring(sc->sc_node, "model"); 128 if (model == NULL || strlen(model) == 0) 129 model = "unknown"; 130 131 sc->sc_depth = 24; 132 sc->sc_linebytes = 8192; 133 sc->sc_height = prom_getpropint(sc->sc_node, "height", 0); 134 sc->sc_width = prom_getpropint(sc->sc_node, "width", 0); 135 136 sc->sc_rasops.ri_depth = 32; 137 sc->sc_rasops.ri_stride = sc->sc_linebytes; 138 sc->sc_rasops.ri_flg = RI_CENTER; 139 sc->sc_rasops.ri_bits = (void *)bus_space_vaddr(sc->sc_bt, 140 sc->sc_pixel_h); 141 142 sc->sc_rasops.ri_width = sc->sc_width; 143 sc->sc_rasops.ri_height = sc->sc_height; 144 sc->sc_rasops.ri_hw = sc; 145 146 maxcol = (prom_getoption("screen-#columns", buf, sizeof buf) == 0) 147 ? strtoul(buf, NULL, 10) 148 : 80; 149 150 maxrow = (prom_getoption("screen-#rows", buf, sizeof buf) != 0) 151 ? strtoul(buf, NULL, 10) 152 : 34; 153 154 rasops_init(&sc->sc_rasops, maxrow, maxcol); 155 156 if ((sc->sc_dv.dv_cfdata->cf_flags & FFB_CFFLAG_NOACCEL) == 0) { 157 sc->sc_rasops.ri_hw = sc; 158 sc->sc_rasops.ri_ops.eraserows = ffb_ras_eraserows; 159 sc->sc_rasops.ri_ops.erasecols = ffb_ras_erasecols; 160 sc->sc_rasops.ri_ops.copyrows = ffb_ras_copyrows; 161 ffb_ras_init(sc); 162 } 163 164 ffb_stdscreen.nrows = sc->sc_rasops.ri_rows; 165 ffb_stdscreen.ncols = sc->sc_rasops.ri_cols; 166 ffb_stdscreen.textops = &sc->sc_rasops.ri_ops; 167 168 /* collect DAC version, as Elite3D cursor enable bit is reversed */ 169 DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_GVERS); 170 sc->sc_dacrev = DAC_READ(sc, FFB_DAC_VALUE) >> 28; 171 172 if (sc->sc_type == FFB_AFB) 173 sc->sc_dacrev = 10; 174 printf(", model %s, dac %u\n", model, sc->sc_dacrev); 175 176 ffb_blank(sc, WSDISPLAYIO_SVIDEO, &blank); 177 178 if (sc->sc_console) { 179 int *ccolp, *crowp; 180 long defattr; 181 182 if (romgetcursoraddr(&crowp, &ccolp)) 183 ccolp = crowp = NULL; 184 if (ccolp != NULL) 185 sc->sc_rasops.ri_ccol = *ccolp; 186 if (crowp != NULL) 187 sc->sc_rasops.ri_crow = *crowp; 188 189 sc->sc_rasops.ri_ops.allocattr(&sc->sc_rasops, 190 0, 0, 0, &defattr); 191 192 wsdisplay_cnattach(&ffb_stdscreen, &sc->sc_rasops, 193 sc->sc_rasops.ri_ccol, sc->sc_rasops.ri_crow, defattr); 194 } 195 196 waa.console = sc->sc_console; 197 waa.scrdata = &ffb_screenlist; 198 waa.accessops = &ffb_accessops; 199 waa.accesscookie = sc; 200 config_found(&sc->sc_dv, &waa, wsemuldisplaydevprint); 201 } 202 203 int 204 ffb_ioctl(v, cmd, data, flags, p) 205 void *v; 206 u_long cmd; 207 caddr_t data; 208 int flags; 209 struct proc *p; 210 { 211 struct ffb_softc *sc = v; 212 struct wsdisplay_fbinfo *wdf; 213 214 #ifdef FFBDEBUG 215 printf("ffb_ioctl: %s cmd _IO%s%s('%c', %lu)\n", 216 sc->sc_dv.dv_xname, 217 (cmd & IOC_IN) ? "W" : "", (cmd & IOC_OUT) ? "R" : "", 218 (char)IOCGROUP(cmd), cmd & 0xff); 219 #endif 220 221 switch (cmd) { 222 case WSDISPLAYIO_GTYPE: 223 *(u_int *)data = WSDISPLAY_TYPE_SUNFFB; 224 break; 225 case WSDISPLAYIO_SMODE: 226 sc->sc_mode = *(u_int *)data; 227 break; 228 case WSDISPLAYIO_GINFO: 229 wdf = (void *)data; 230 wdf->height = sc->sc_height; 231 wdf->width = sc->sc_width; 232 wdf->depth = 32; 233 wdf->cmsize = 256; /* XXX */ 234 break; 235 #ifdef WSDISPLAYIO_LINEBYTES 236 case WSDISPLAYIO_LINEBYTES: 237 *(u_int *)data = sc->sc_linebytes; 238 break; 239 #endif 240 case WSDISPLAYIO_GETCMAP: 241 break;/* XXX */ 242 243 case WSDISPLAYIO_PUTCMAP: 244 break;/* XXX */ 245 246 case WSDISPLAYIO_SVIDEO: 247 case WSDISPLAYIO_GVIDEO: 248 return(ffb_blank(sc, cmd, (u_int *)data)); 249 break; 250 case WSDISPLAYIO_GCURPOS: 251 case WSDISPLAYIO_SCURPOS: 252 case WSDISPLAYIO_GCURMAX: 253 case WSDISPLAYIO_GCURSOR: 254 case WSDISPLAYIO_SCURSOR: 255 default: 256 return -1; /* not supported yet */ 257 } 258 259 return (0); 260 } 261 262 int 263 ffb_alloc_screen(v, type, cookiep, curxp, curyp, attrp) 264 void *v; 265 const struct wsscreen_descr *type; 266 void **cookiep; 267 int *curxp, *curyp; 268 long *attrp; 269 { 270 struct ffb_softc *sc = v; 271 272 if (sc->sc_nscreens > 0) 273 return (ENOMEM); 274 275 *cookiep = &sc->sc_rasops; 276 *curyp = 0; 277 *curxp = 0; 278 279 sc->sc_rasops.ri_ops.allocattr(&sc->sc_rasops, 0, 0, 0, attrp); 280 281 sc->sc_nscreens++; 282 return (0); 283 } 284 285 /* blank/unblank the screen */ 286 static int 287 ffb_blank(struct ffb_softc *sc, u_long cmd, u_int *data) 288 { 289 u_int val; 290 291 DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_GSBLANK); 292 val = DAC_READ(sc, FFB_DAC_VALUE); 293 294 switch (cmd) { 295 case WSDISPLAYIO_GVIDEO: 296 *data = val & 1; 297 return(0); 298 break; 299 case WSDISPLAYIO_SVIDEO: 300 if (*data == WSDISPLAYIO_VIDEO_OFF) 301 val &= ~1; 302 else if (*data == WSDISPLAYIO_VIDEO_ON) 303 val |= 1; 304 else 305 return(EINVAL); 306 break; 307 default: 308 return(EINVAL); 309 } 310 311 DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_GSBLANK); 312 DAC_WRITE(sc, FFB_DAC_VALUE, val); 313 314 return(0); 315 } 316 317 void 318 ffb_free_screen(v, cookie) 319 void *v; 320 void *cookie; 321 { 322 struct ffb_softc *sc = v; 323 324 sc->sc_nscreens--; 325 } 326 327 int 328 ffb_show_screen(v, cookie, waitok, cb, cbarg) 329 void *v; 330 void *cookie; 331 int waitok; 332 void (*cb)(void *, int, int); 333 void *cbarg; 334 { 335 return (0); 336 } 337 338 paddr_t 339 ffb_mmap(vsc, off, prot) 340 void *vsc; 341 off_t off; 342 int prot; 343 { 344 struct ffb_softc *sc = vsc; 345 int i; 346 347 switch (sc->sc_mode) { 348 case WSDISPLAYIO_MODE_MAPPED: 349 for (i = 0; i < sc->sc_nreg; i++) { 350 /* Before this set? */ 351 if (off < sc->sc_addrs[i]) 352 continue; 353 /* After this set? */ 354 if (off >= (sc->sc_addrs[i] + sc->sc_sizes[i])) 355 continue; 356 357 return (bus_space_mmap(sc->sc_bt, sc->sc_addrs[i], 358 off - sc->sc_addrs[i], prot, BUS_SPACE_MAP_LINEAR)); 359 } 360 break; 361 #ifdef WSDISPLAYIO_MODE_DUMBFB 362 case WSDISPLAYIO_MODE_DUMBFB: 363 if (sc->sc_nreg < FFB_REG_DFB24) 364 break; 365 if (off >= 0 && off < sc->sc_sizes[FFB_REG_DFB24]) 366 return (bus_space_mmap(sc->sc_bt, 367 sc->sc_addrs[FFB_REG_DFB24], off, prot, 368 BUS_SPACE_MAP_LINEAR)); 369 break; 370 #endif 371 } 372 373 return (-1); 374 } 375 376 void 377 ffb_ras_fifo_wait(sc, n) 378 struct ffb_softc *sc; 379 int n; 380 { 381 int32_t cache = sc->sc_fifo_cache; 382 383 if (cache < n) { 384 do { 385 cache = FBC_READ(sc, FFB_FBC_UCSR); 386 cache = (cache & FBC_UCSR_FIFO_MASK) - 8; 387 } while (cache < n); 388 } 389 sc->sc_fifo_cache = cache - n; 390 } 391 392 void 393 ffb_ras_wait(sc) 394 struct ffb_softc *sc; 395 { 396 u_int32_t ucsr, r; 397 398 while (1) { 399 ucsr = FBC_READ(sc, FFB_FBC_UCSR); 400 if ((ucsr & (FBC_UCSR_FB_BUSY|FBC_UCSR_RP_BUSY)) == 0) 401 break; 402 r = ucsr & (FBC_UCSR_READ_ERR | FBC_UCSR_FIFO_OVFL); 403 if (r != 0) 404 FBC_WRITE(sc, FFB_FBC_UCSR, r); 405 } 406 } 407 408 void 409 ffb_ras_init(sc) 410 struct ffb_softc *sc; 411 { 412 ffb_ras_fifo_wait(sc, 7); 413 FBC_WRITE(sc, FFB_FBC_PPC, 414 FBC_PPC_VCE_DIS | FBC_PPC_TBE_OPAQUE | 415 FBC_PPC_APE_DIS | FBC_PPC_CS_CONST); 416 FBC_WRITE(sc, FFB_FBC_FBC, 417 FFB_FBC_WB_A | FFB_FBC_RB_A | FFB_FBC_SB_BOTH | 418 FFB_FBC_XE_OFF | FFB_FBC_RGBE_MASK); 419 FBC_WRITE(sc, FFB_FBC_ROP, FBC_ROP_NEW); 420 FBC_WRITE(sc, FFB_FBC_DRAWOP, FBC_DRAWOP_RECTANGLE); 421 FBC_WRITE(sc, FFB_FBC_PMASK, 0xffffffff); 422 FBC_WRITE(sc, FFB_FBC_FONTINC, 0x10000); 423 sc->sc_fg_cache = 0; 424 FBC_WRITE(sc, FFB_FBC_FG, sc->sc_fg_cache); 425 ffb_ras_wait(sc); 426 } 427 428 void 429 ffb_ras_eraserows(cookie, row, n, attr) 430 void *cookie; 431 int row, n; 432 long int attr; 433 { 434 struct rasops_info *ri = cookie; 435 struct ffb_softc *sc = ri->ri_hw; 436 437 if (row < 0) { 438 n += row; 439 row = 0; 440 } 441 if (row + n > ri->ri_rows) 442 n = ri->ri_rows - row; 443 if (n <= 0) 444 return; 445 446 ffb_ras_fill(sc); 447 ffb_ras_setfg(sc, ri->ri_devcmap[(attr >> 16) & 0xf]); 448 ffb_ras_fifo_wait(sc, 4); 449 if ((n == ri->ri_rows) && (ri->ri_flg & RI_FULLCLEAR)) { 450 FBC_WRITE(sc, FFB_FBC_BY, 0); 451 FBC_WRITE(sc, FFB_FBC_BX, 0); 452 FBC_WRITE(sc, FFB_FBC_BH, ri->ri_height); 453 FBC_WRITE(sc, FFB_FBC_BW, ri->ri_width); 454 } else { 455 row *= ri->ri_font->fontheight; 456 FBC_WRITE(sc, FFB_FBC_BY, ri->ri_yorigin + row); 457 FBC_WRITE(sc, FFB_FBC_BX, ri->ri_xorigin); 458 FBC_WRITE(sc, FFB_FBC_BH, n * ri->ri_font->fontheight); 459 FBC_WRITE(sc, FFB_FBC_BW, ri->ri_emuwidth); 460 } 461 ffb_ras_wait(sc); 462 } 463 464 void 465 ffb_ras_erasecols(cookie, row, col, n, attr) 466 void *cookie; 467 int row, col, n; 468 long int attr; 469 { 470 struct rasops_info *ri = cookie; 471 struct ffb_softc *sc = ri->ri_hw; 472 473 if ((row < 0) || (row >= ri->ri_rows)) 474 return; 475 if (col < 0) { 476 n += col; 477 col = 0; 478 } 479 if (col + n > ri->ri_cols) 480 n = ri->ri_cols - col; 481 if (n <= 0) 482 return; 483 n *= ri->ri_font->fontwidth; 484 col *= ri->ri_font->fontwidth; 485 row *= ri->ri_font->fontheight; 486 487 ffb_ras_fill(sc); 488 ffb_ras_setfg(sc, ri->ri_devcmap[(attr >> 16) & 0xf]); 489 ffb_ras_fifo_wait(sc, 4); 490 FBC_WRITE(sc, FFB_FBC_BY, ri->ri_yorigin + row); 491 FBC_WRITE(sc, FFB_FBC_BX, ri->ri_xorigin + col); 492 FBC_WRITE(sc, FFB_FBC_BH, ri->ri_font->fontheight); 493 FBC_WRITE(sc, FFB_FBC_BW, n - 1); 494 ffb_ras_wait(sc); 495 } 496 497 void 498 ffb_ras_fill(sc) 499 struct ffb_softc *sc; 500 { 501 ffb_ras_fifo_wait(sc, 2); 502 FBC_WRITE(sc, FFB_FBC_ROP, FBC_ROP_NEW); 503 FBC_WRITE(sc, FFB_FBC_DRAWOP, FBC_DRAWOP_RECTANGLE); 504 ffb_ras_wait(sc); 505 } 506 507 void 508 ffb_ras_copyrows(cookie, src, dst, n) 509 void *cookie; 510 int src, dst, n; 511 { 512 struct rasops_info *ri = cookie; 513 struct ffb_softc *sc = ri->ri_hw; 514 515 if (dst == src) 516 return; 517 if (src < 0) { 518 n += src; 519 src = 0; 520 } 521 if ((src + n) > ri->ri_rows) 522 n = ri->ri_rows - src; 523 if (dst < 0) { 524 n += dst; 525 dst = 0; 526 } 527 if ((dst + n) > ri->ri_rows) 528 n = ri->ri_rows - dst; 529 if (n <= 0) 530 return; 531 n *= ri->ri_font->fontheight; 532 src *= ri->ri_font->fontheight; 533 dst *= ri->ri_font->fontheight; 534 535 ffb_ras_fifo_wait(sc, 8); 536 FBC_WRITE(sc, FFB_FBC_ROP, FBC_ROP_OLD | (FBC_ROP_OLD << 8)); 537 FBC_WRITE(sc, FFB_FBC_DRAWOP, FBC_DRAWOP_VSCROLL); 538 FBC_WRITE(sc, FFB_FBC_BY, ri->ri_yorigin + src); 539 FBC_WRITE(sc, FFB_FBC_BX, ri->ri_xorigin); 540 FBC_WRITE(sc, FFB_FBC_DY, ri->ri_yorigin + dst); 541 FBC_WRITE(sc, FFB_FBC_DX, ri->ri_xorigin); 542 FBC_WRITE(sc, FFB_FBC_BH, n); 543 FBC_WRITE(sc, FFB_FBC_BW, ri->ri_emuwidth); 544 ffb_ras_wait(sc); 545 } 546 547 void 548 ffb_ras_setfg(sc, fg) 549 struct ffb_softc *sc; 550 int32_t fg; 551 { 552 ffb_ras_fifo_wait(sc, 1); 553 if (fg == sc->sc_fg_cache) 554 return; 555 sc->sc_fg_cache = fg; 556 FBC_WRITE(sc, FFB_FBC_FG, fg); 557 ffb_ras_wait(sc); 558 } 559