1 /* $NetBSD: ffb.c,v 1.71 2026/05/20 08:24:54 macallan 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 (at) 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.71 2026/05/20 08:24:54 macallan 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 #include <sys/ioctl.h> 45 #include <sys/mman.h> 46 47 #include <sys/bus.h> 48 #include <machine/autoconf.h> 49 #include <machine/openfirm.h> 50 #include <machine/vmparam.h> 51 52 #include <dev/wscons/wsconsio.h> 53 #include <dev/sun/fbio.h> 54 #include <dev/sun/fbvar.h> 55 56 #include <dev/wsfont/wsfont.h> 57 #include <dev/wscons/wsdisplay_vconsvar.h> 58 59 #include <prop/proplib.h> 60 61 #include <dev/i2c/i2cvar.h> 62 #include <dev/i2c/i2c_bitbang.h> 63 #include <dev/i2c/ddcvar.h> 64 65 #include <sparc64/dev/ffbreg.h> 66 #include <sparc64/dev/ffbvar.h> 67 68 #include "opt_wsdisplay_compat.h" 69 #include "opt_ffb.h" 70 71 #ifndef WS_DEFAULT_BG 72 /* Sun -> background should be white */ 73 #define WS_DEFAULT_BG 0xf 74 #endif 75 76 #ifdef FFB_SYNC 77 #define SYNC ffb_ras_wait(sc) 78 #else 79 #define SYNC 80 #endif 81 82 /* Debugging */ 83 #if !defined FFB_DEBUG 84 #define FFB_DEBUG 0 85 #endif 86 #define DPRINTF(x) if (ffb_debug) printf x 87 /* Patchable */ 88 extern int ffb_debug; 89 #if FFB_DEBUG > 0 90 int ffb_debug = 1; 91 #else 92 int ffb_debug = 0; 93 #endif 94 95 extern struct cfdriver ffb_cd; 96 97 struct wsscreen_descr ffb_stdscreen = { 98 "sunffb", 99 0, 0, /* will be filled in -- XXX shouldn't, it's global. */ 100 0, 101 0, 0, 102 WSSCREEN_REVERSE | WSSCREEN_WSCOLORS | WSSCREEN_UNDERLINE | 103 WSSCREEN_HILIT | WSSCREEN_RESIZE, 104 NULL /* modecookie */ 105 }; 106 107 const struct wsscreen_descr *ffb_scrlist[] = { 108 &ffb_stdscreen, 109 /* XXX other formats? */ 110 }; 111 112 struct wsscreen_list ffb_screenlist = { 113 sizeof(ffb_scrlist) / sizeof(struct wsscreen_descr *), 114 ffb_scrlist 115 }; 116 117 static struct vcons_screen ffb_console_screen; 118 119 int ffb_ioctl(void *, void *, u_long, void *, int, struct lwp *); 120 static int ffb_blank(struct ffb_softc *, u_long, u_int *); 121 paddr_t ffb_mmap(void *, void *, off_t, int); 122 void ffb_ras_fifo_wait(struct ffb_softc *, int); 123 void ffb_ras_wait(struct ffb_softc *); 124 void ffb_ras_init(struct ffb_softc *); 125 void ffb_ras_copyrows(void *, int, int, int); 126 void ffb_ras_erasecols(void *, int, int, int, long int); 127 void ffb_ras_eraserows(void *, int, int, long int); 128 void ffb_ras_fill(struct ffb_softc *); 129 void ffb_ras_invert(struct ffb_softc *); 130 static void ffb_ras_setfg(struct ffb_softc *, int32_t); 131 static void ffb_ras_setbg(struct ffb_softc *, int32_t); 132 133 void ffb_clearscreen(struct ffb_softc *); 134 void ffb_init_screen(void *, struct vcons_screen *, int, 135 long *); 136 int ffb_allocattr(void *, int, int, int, long *); 137 void ffb_putchar_mono(void *, int, int, u_int, long); 138 void ffb_putchar_aa(void *, int, int, u_int, long); 139 void ffb_cursor(void *, int, int, int); 140 141 /* frame buffer generic driver */ 142 static void ffbfb_unblank(device_t); 143 dev_type_open(ffbfb_open); 144 dev_type_close(ffbfb_close); 145 dev_type_ioctl(ffbfb_ioctl); 146 dev_type_mmap(ffbfb_mmap); 147 148 static struct fbdriver ffb_fbdriver = { 149 ffbfb_unblank, ffbfb_open, ffbfb_close, ffbfb_ioctl, nopoll, 150 ffbfb_mmap, nokqfilter 151 }; 152 153 struct wsdisplay_accessops ffb_accessops = { 154 .ioctl = ffb_ioctl, 155 .mmap = ffb_mmap, 156 }; 157 158 /* I2C glue */ 159 static int ffb_i2c_send_start(void *, int); 160 static int ffb_i2c_send_stop(void *, int); 161 static int ffb_i2c_initiate_xfer(void *, i2c_addr_t, int); 162 static int ffb_i2c_read_byte(void *, uint8_t *, int); 163 static int ffb_i2c_write_byte(void *, uint8_t, int); 164 165 /* I2C bitbang glue */ 166 static void ffb_i2cbb_set_bits(void *, uint32_t); 167 static void ffb_i2cbb_set_dir(void *, uint32_t); 168 static uint32_t ffb_i2cbb_read(void *); 169 170 static const struct i2c_bitbang_ops ffb_i2cbb_ops = { 171 ffb_i2cbb_set_bits, 172 ffb_i2cbb_set_dir, 173 ffb_i2cbb_read, 174 { 175 FFB_DAC_CFG_MPDATA_SDA, 176 FFB_DAC_CFG_MPDATA_SCL, 177 0, 178 0 179 } 180 }; 181 182 void ffb_attach_i2c(struct ffb_softc *); 183 184 /* Video mode setting */ 185 int ffb_tgc_disable(struct ffb_softc *); 186 void ffb_get_pclk(int, uint32_t *, int *); 187 int ffb_set_vmode(struct ffb_softc *, struct videomode *, int, int *, int *); 188 189 190 void 191 ffb_attach(device_t self) 192 { 193 struct ffb_softc *sc = device_private(self); 194 struct wsemuldisplaydev_attach_args waa; 195 struct rasops_info *ri; 196 long defattr; 197 const char *model, *out_dev; 198 int btype; 199 uint32_t dac; 200 int maxrow; 201 u_int blank = WSDISPLAYIO_VIDEO_ON; 202 char buf[6+1]; 203 int i, try_edid; 204 prop_data_t data; 205 206 printf(":"); 207 208 if (sc->sc_type == FFB_CREATOR) { 209 btype = prom_getpropint(sc->sc_node, "board_type", 0); 210 if ((btype & 7) == 3) 211 printf(" Creator3D"); 212 else 213 printf(" Creator"); 214 } else { 215 printf(" Elite3D"); 216 btype = 0; 217 } 218 219 model = prom_getpropstring(sc->sc_node, "model"); 220 if (model == NULL || strlen(model) == 0) 221 model = "unknown"; 222 223 sc->sc_depth = 24; 224 sc->sc_linebytes = 8192; 225 /* We might alter these during EDID mode setting */ 226 sc->sc_height = prom_getpropint(sc->sc_node, "height", 0); 227 sc->sc_width = prom_getpropint(sc->sc_node, "width", 0); 228 229 sc->sc_locked = 0; 230 sc->sc_mode = WSDISPLAYIO_MODE_EMUL; 231 232 maxrow = (prom_getoption("screen-#rows", buf, sizeof buf) != 0) 233 ? strtoul(buf, NULL, 10) 234 : 34; 235 236 /* collect DAC version, as Elite3D cursor enable bit is reversed */ 237 DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_DEVID); 238 dac = DAC_READ(sc, FFB_DAC_VALUE); 239 sc->sc_dacrev = (dac >> 28) & 0xf; 240 241 if (sc->sc_type == FFB_AFB) { 242 sc->sc_dacrev = 10; 243 sc->sc_needredraw = 0; 244 } else { 245 /* see what kind of DAC we have */ 246 int pnum = (dac & 0x0ffff000) >> 12; 247 if (pnum == 0x236e) { 248 sc->sc_needredraw = 0; 249 } else { 250 sc->sc_needredraw = 1; 251 } 252 } 253 printf(", model %s, dac %u\n", model, sc->sc_dacrev); 254 if (sc->sc_needredraw) 255 printf("%s: found old DAC, enabling redraw on unblank\n", 256 device_xname(sc->sc_dev)); 257 258 /* Check if a console resolution "<device>:r<res>" is set. */ 259 if (sc->sc_console) { 260 out_dev = prom_getpropstring(sc->sc_node, "output-device"); 261 if (out_dev != NULL && strlen(out_dev) != 0 && 262 strstr(out_dev, ":r") != NULL) 263 try_edid = 0; 264 else 265 try_edid = 1; 266 } else 267 try_edid = 1; 268 269 #if FFB_DEBUG > 0 270 DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_TGC); 271 printf("tgc: %08x\n", DAC_READ(sc, FFB_DAC_VALUE)); 272 DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_DAC_CTRL); 273 printf("dcl: %08x\n", DAC_READ(sc, FFB_DAC_VALUE)); 274 #endif 275 ffb_attach_i2c(sc); 276 277 /* Need to set asynchronous blank during DDC write/read */ 278 DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_USR_CTRL); 279 dac = DAC_READ(sc, FFB_DAC_VALUE); 280 DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_USR_CTRL); 281 DAC_WRITE(sc, FFB_DAC_VALUE, dac | FFB_DAC_USR_CTRL_BLANK); 282 283 /* Some monitors don't respond first time */ 284 i = 0; 285 while (sc->sc_edid_data[1] == 0 && i++ < 3) 286 ddc_read_edid(&sc->sc_i2c, sc->sc_edid_data, EDID_DATA_LEN); 287 288 /* Remove asynchronous blank */ 289 DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_USR_CTRL); 290 DAC_WRITE(sc, FFB_DAC_VALUE, dac); 291 292 if (edid_parse(&sc->sc_edid_data[0], &sc->sc_edid_info) != -1) { 293 sort_modes(sc->sc_edid_info.edid_modes, 294 &sc->sc_edid_info.edid_preferred_mode, 295 sc->sc_edid_info.edid_nmodes); 296 DPRINTF(("%s: EDID data:\n ", device_xname(sc->sc_dev))); 297 for (i = 0; i < EDID_DATA_LEN; i++) { 298 if (i && !(i % 32)) 299 DPRINTF(("\n ")); 300 if (i && !(i % 4)) 301 DPRINTF((" ")); 302 DPRINTF(("%02x", sc->sc_edid_data[i])); 303 } 304 DPRINTF(("\n")); 305 if (ffb_debug) 306 edid_print(&sc->sc_edid_info); 307 308 data = prop_data_create_copy(sc->sc_edid_data, EDID_DATA_LEN); 309 prop_dictionary_set(device_properties(self), "EDID", data); 310 prop_object_release(data); 311 312 if (try_edid) 313 for (i = 0; i < sc->sc_edid_info.edid_nmodes; i++) { 314 if (ffb_set_vmode(sc, 315 &(sc->sc_edid_info.edid_modes[i]), btype, 316 &(sc->sc_width), &(sc->sc_height))) 317 break; 318 } 319 } else { 320 DPRINTF(("%s: No EDID data.\n", device_xname(sc->sc_dev))); 321 } 322 323 ffb_ras_init(sc); 324 325 ffb_blank(sc, WSDISPLAYIO_SVIDEO, &blank); 326 327 sc->sc_accel = ((device_cfdata(sc->sc_dev)->cf_flags & 328 FFB_CFFLAG_NOACCEL) == 0); 329 330 wsfont_init(); 331 332 vcons_init(&sc->vd, sc, &ffb_stdscreen, &ffb_accessops); 333 sc->vd.init_screen = ffb_init_screen; 334 ri = &ffb_console_screen.scr_ri; 335 336 /* we mess with ffb_console_screen only once */ 337 if (sc->sc_console) { 338 ffb_console_screen.scr_flags = VCONS_SCREEN_IS_STATIC; 339 vcons_init_screen(&sc->vd, &ffb_console_screen, 1, &defattr); 340 SCREEN_VISIBLE((&ffb_console_screen)); 341 /* 342 * XXX we shouldn't use a global variable for the console 343 * screen 344 */ 345 sc->vd.active = &ffb_console_screen; 346 } else { 347 if (ffb_console_screen.scr_ri.ri_rows == 0) { 348 /* do some minimal setup to avoid weirdnesses later */ 349 vcons_init_screen(&sc->vd, &ffb_console_screen, 1, &defattr); 350 } else 351 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr); 352 } 353 354 ffb_stdscreen.nrows = ri->ri_rows; 355 ffb_stdscreen.ncols = ri->ri_cols; 356 ffb_stdscreen.textops = &ri->ri_ops; 357 358 sc->sc_fb.fb_driver = &ffb_fbdriver; 359 sc->sc_fb.fb_type.fb_cmsize = 0; 360 sc->sc_fb.fb_type.fb_size = maxrow * sc->sc_linebytes; 361 sc->sc_fb.fb_type.fb_type = FBTYPE_CREATOR; 362 sc->sc_fb.fb_type.fb_width = sc->sc_width; 363 sc->sc_fb.fb_type.fb_depth = sc->sc_depth; 364 sc->sc_fb.fb_type.fb_height = sc->sc_height; 365 sc->sc_fb.fb_device = sc->sc_dev; 366 fb_attach(&sc->sc_fb, sc->sc_console); 367 368 ffb_clearscreen(sc); 369 370 if (sc->sc_console) { 371 wsdisplay_cnattach(&ffb_stdscreen, ri, 0, 0, defattr); 372 vcons_replay_msgbuf(&ffb_console_screen); 373 } 374 375 waa.console = sc->sc_console; 376 waa.scrdata = &ffb_screenlist; 377 waa.accessops = &ffb_accessops; 378 waa.accesscookie = &sc->vd; 379 config_found(sc->sc_dev, &waa, wsemuldisplaydevprint, CFARGS_NONE); 380 } 381 382 void 383 ffb_attach_i2c(struct ffb_softc *sc) 384 { 385 386 /* Fill in the i2c tag */ 387 iic_tag_init(&sc->sc_i2c); 388 sc->sc_i2c.ic_cookie = sc; 389 sc->sc_i2c.ic_send_start = ffb_i2c_send_start; 390 sc->sc_i2c.ic_send_stop = ffb_i2c_send_stop; 391 sc->sc_i2c.ic_initiate_xfer = ffb_i2c_initiate_xfer; 392 sc->sc_i2c.ic_read_byte = ffb_i2c_read_byte; 393 sc->sc_i2c.ic_write_byte = ffb_i2c_write_byte; 394 } 395 396 int 397 ffb_ioctl(void *v, void *vs, u_long cmd, void *data, int flags, struct lwp *l) 398 { 399 struct vcons_data *vd = v; 400 struct ffb_softc *sc = vd->cookie; 401 struct wsdisplay_fbinfo *wdf; 402 struct vcons_screen *ms = vd->active; 403 404 DPRINTF(("ffb_ioctl: %s cmd _IO%s%s('%c', %lu)\n", 405 device_xname(sc->sc_dev), 406 (cmd & IOC_IN) ? "W" : "", (cmd & IOC_OUT) ? "R" : "", 407 (char)IOCGROUP(cmd), cmd & 0xff)); 408 409 switch (cmd) { 410 case FBIOGTYPE: 411 *(struct fbtype *)data = sc->sc_fb.fb_type; 412 break; 413 case FBIOGATTR: 414 #define fba ((struct fbgattr *)data) 415 fba->real_type = sc->sc_fb.fb_type.fb_type; 416 fba->owner = 0; /* XXX ??? */ 417 fba->fbtype = sc->sc_fb.fb_type; 418 fba->sattr.flags = 0; 419 fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type; 420 fba->sattr.dev_specific[0] = -1; 421 fba->emu_types[0] = sc->sc_fb.fb_type.fb_type; 422 fba->emu_types[1] = -1; 423 #undef fba 424 break; 425 426 case FBIOGETCMAP: 427 case FBIOPUTCMAP: 428 return EIO; 429 430 case FBIOGVIDEO: 431 case FBIOSVIDEO: 432 return ffb_blank(sc, cmd == FBIOGVIDEO? 433 WSDISPLAYIO_GVIDEO : WSDISPLAYIO_SVIDEO, 434 (u_int *)data); 435 break; 436 case FBIOGCURSOR: 437 case FBIOSCURSOR: 438 /* the console driver is not using the hardware cursor */ 439 break; 440 case FBIOGCURPOS: 441 printf("%s: FBIOGCURPOS not implemented\n", 442 device_xname(sc->sc_dev)); 443 return EIO; 444 case FBIOSCURPOS: 445 printf("%s: FBIOSCURPOS not implemented\n", 446 device_xname(sc->sc_dev)); 447 return EIO; 448 case FBIOGCURMAX: 449 printf("%s: FBIOGCURMAX not implemented\n", 450 device_xname(sc->sc_dev)); 451 return EIO; 452 453 case WSDISPLAYIO_GTYPE: 454 *(u_int *)data = WSDISPLAY_TYPE_SUNFFB; 455 break; 456 case WSDISPLAYIO_SMODE: 457 { 458 if (sc->sc_mode != *(u_int *)data) { 459 sc->sc_mode = *(u_int *)data; 460 if ((sc->sc_mode == WSDISPLAYIO_MODE_EMUL) && 461 (sc->sc_locked == 0)) { 462 ffb_ras_init(sc); 463 vcons_redraw_screen(ms); 464 } else { 465 ffb_ras_wait(sc); 466 } 467 } 468 } 469 break; 470 case WSDISPLAYIO_GINFO: 471 wdf = (void *)data; 472 wdf->height = sc->sc_height; 473 wdf->width = sc->sc_width; 474 wdf->depth = 32; 475 wdf->cmsize = 256; /* XXX */ 476 break; 477 #ifdef WSDISPLAYIO_LINEBYTES 478 case WSDISPLAYIO_LINEBYTES: 479 *(u_int *)data = sc->sc_linebytes; 480 break; 481 #endif 482 case WSDISPLAYIO_GETCMAP: 483 break;/* XXX */ 484 485 case WSDISPLAYIO_PUTCMAP: 486 break;/* XXX */ 487 488 case WSDISPLAYIO_SVIDEO: 489 case WSDISPLAYIO_GVIDEO: 490 return(ffb_blank(sc, cmd, (u_int *)data)); 491 break; 492 493 case WSDISPLAYIO_GCURPOS: 494 case WSDISPLAYIO_SCURPOS: 495 case WSDISPLAYIO_GCURMAX: 496 case WSDISPLAYIO_GCURSOR: 497 case WSDISPLAYIO_SCURSOR: 498 return EIO; /* not supported yet */ 499 break; 500 501 case WSDISPLAYIO_GET_EDID: { 502 struct wsdisplayio_edid_info *d = data; 503 return wsdisplayio_get_edid(sc->sc_dev, d); 504 } 505 506 case WSDISPLAYIO_GET_FBINFO: { 507 struct wsdisplayio_fbinfo *fbi = data; 508 return wsdisplayio_get_fbinfo(&ms->scr_ri, fbi); 509 } 510 511 default: 512 return EPASSTHROUGH; 513 } 514 515 return (0); 516 } 517 518 /* blank/unblank the screen */ 519 static int 520 ffb_blank(struct ffb_softc *sc, u_long cmd, u_int *data) 521 { 522 struct vcons_screen *ms = sc->vd.active; 523 u_int val; 524 525 DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_TGC); 526 val = DAC_READ(sc, FFB_DAC_VALUE); 527 528 switch (cmd) { 529 case WSDISPLAYIO_GVIDEO: 530 *data = val & 1; 531 return(0); 532 break; 533 case WSDISPLAYIO_SVIDEO: 534 if (*data == WSDISPLAYIO_VIDEO_OFF) 535 val &= ~1; 536 else if (*data == WSDISPLAYIO_VIDEO_ON) 537 val |= 1; 538 else 539 return(EINVAL); 540 break; 541 default: 542 return(EINVAL); 543 } 544 545 DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_TGC); 546 DAC_WRITE(sc, FFB_DAC_VALUE, val); 547 548 if ((val & 1) && sc->sc_needredraw) { 549 if (ms != NULL) { 550 if ((sc->sc_mode == WSDISPLAYIO_MODE_EMUL) && 551 (sc->sc_locked == 0)) { 552 ffb_ras_init(sc); 553 vcons_redraw_screen(ms); 554 } 555 } 556 } 557 558 return(0); 559 } 560 561 paddr_t 562 ffb_mmap(void *vsc, void *vs, off_t off, int prot) 563 { 564 struct vcons_data *vd = vsc; 565 struct ffb_softc *sc = vd->cookie; 566 int i; 567 568 switch (sc->sc_mode) { 569 case WSDISPLAYIO_MODE_MAPPED: 570 for (i = 0; i < sc->sc_nreg; i++) { 571 /* Before this set? */ 572 if (off < sc->sc_addrs[i]) 573 continue; 574 /* After this set? */ 575 if (off >= (sc->sc_addrs[i] + sc->sc_sizes[i])) 576 continue; 577 578 return (bus_space_mmap(sc->sc_bt, sc->sc_addrs[i], 579 off - sc->sc_addrs[i], prot, BUS_SPACE_MAP_LINEAR)); 580 } 581 break; 582 #ifdef WSDISPLAYIO_MODE_DUMBFB 583 case WSDISPLAYIO_MODE_DUMBFB: 584 if (sc->sc_nreg < FFB_REG_DFB24) 585 break; 586 if (off >= 0 && off < sc->sc_sizes[FFB_REG_DFB24]) 587 return (bus_space_mmap(sc->sc_bt, 588 sc->sc_addrs[FFB_REG_DFB24], off, prot, 589 BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE)); 590 break; 591 #endif 592 } 593 return (-1); 594 } 595 596 void 597 ffb_ras_fifo_wait(struct ffb_softc *sc, int n) 598 { 599 int32_t cache = sc->sc_fifo_cache; 600 601 if (cache < n) { 602 do { 603 cache = FBC_READ(sc, FFB_FBC_UCSR); 604 cache = (cache & FBC_UCSR_FIFO_MASK) - 8; 605 } while (cache < n); 606 } 607 sc->sc_fifo_cache = cache - n; 608 } 609 610 void 611 ffb_ras_wait(struct ffb_softc *sc) 612 { 613 uint32_t ucsr, r; 614 615 while (1) { 616 ucsr = FBC_READ(sc, FFB_FBC_UCSR); 617 if ((ucsr & (FBC_UCSR_FB_BUSY|FBC_UCSR_RP_BUSY)) == 0) 618 break; 619 r = ucsr & (FBC_UCSR_READ_ERR | FBC_UCSR_FIFO_OVFL); 620 if (r != 0) 621 FBC_WRITE(sc, FFB_FBC_UCSR, r); 622 } 623 } 624 625 void 626 ffb_ras_init(struct ffb_softc *sc) 627 { 628 uint32_t fbc; 629 630 if (sc->sc_width > 1280) { 631 DPRINTF(("ffb_ras_init: high resolution.\n")); 632 fbc = FFB_FBC_WM_COMBINED | FFB_FBC_WE_FORCEON | 633 FFB_FBC_ZE_OFF | FFB_FBC_YE_OFF | FFB_FBC_XE_ON; 634 } else { 635 DPRINTF(("ffb_ras_init: standard resolution.\n")); 636 fbc = FFB_FBC_XE_OFF; 637 } 638 ffb_ras_fifo_wait(sc, 7); 639 DPRINTF(("WID: %08x\n", FBC_READ(sc, FFB_FBC_WID))); 640 FBC_WRITE(sc, FFB_FBC_WID, 0x0); 641 FBC_WRITE(sc, FFB_FBC_PPC, 642 FBC_PPC_VCE_DIS | FBC_PPC_TBE_OPAQUE | FBC_PPC_ACE_DIS | 643 FBC_PPC_APE_DIS | FBC_PPC_DCE_DIS | FBC_PPC_CS_CONST | 644 FBC_PPC_ABE_DIS | FBC_PPC_XS_WID); 645 646 fbc |= FFB_FBC_WB_A | FFB_FBC_RB_A | FFB_FBC_SB_BOTH | 647 FFB_FBC_RGBE_MASK; 648 DPRINTF(("%s: fbc is %08x\n", __func__, fbc)); 649 FBC_WRITE(sc, FFB_FBC_FBC, fbc); 650 FBC_WRITE(sc, FFB_FBC_ROP, FBC_ROP_NEW); 651 FBC_WRITE(sc, FFB_FBC_DRAWOP, FBC_DRAWOP_RECTANGLE); 652 FBC_WRITE(sc, FFB_FBC_PMASK, 0xffffffff); 653 FBC_WRITE(sc, FFB_FBC_FONTINC, 0x10000); 654 ffb_ras_fifo_wait(sc, 5); 655 sc->sc_fg_cache = 0; 656 FBC_WRITE(sc, FFB_FBC_FG, sc->sc_fg_cache); 657 sc->sc_bg_cache = 0; 658 FBC_WRITE(sc, FFB_FBC_BG, sc->sc_bg_cache); 659 FBC_WRITE(sc, FFB_FBC_BLENDC, FFB_BLENDC_FORCE_ONE | 660 FFB_BLENDC_DF_ONE_M_A | 661 FFB_BLENDC_SF_A); 662 FBC_WRITE(sc, FFB_FBC_BLENDC1, 0); 663 FBC_WRITE(sc, FFB_FBC_BLENDC2, 0); 664 ffb_ras_wait(sc); 665 } 666 667 void 668 ffb_ras_eraserows(void *cookie, int row, int n, long attr) 669 { 670 struct rasops_info *ri = cookie; 671 struct vcons_screen *scr = ri->ri_hw; 672 struct ffb_softc *sc = scr->scr_cookie; 673 674 if (row < 0) { 675 n += row; 676 row = 0; 677 } 678 if (row + n > ri->ri_rows) 679 n = ri->ri_rows - row; 680 if (n <= 0) 681 return; 682 683 ffb_ras_fill(sc); 684 ffb_ras_setfg(sc, ri->ri_devcmap[(attr >> 16) & 0xf]); 685 ffb_ras_fifo_wait(sc, 4); 686 if (row == 0 && n == ri->ri_rows) { 687 FBC_WRITE(sc, FFB_FBC_BY, 0); 688 FBC_WRITE(sc, FFB_FBC_BX, 0); 689 FBC_WRITE(sc, FFB_FBC_BH, ri->ri_height); 690 FBC_WRITE(sc, FFB_FBC_BW, ri->ri_width); 691 ri->ri_flg &= ~RI_CURSOR; 692 } else { 693 row *= ri->ri_font->fontheight; 694 FBC_WRITE(sc, FFB_FBC_BY, ri->ri_yorigin + row); 695 FBC_WRITE(sc, FFB_FBC_BX, ri->ri_xorigin); 696 FBC_WRITE(sc, FFB_FBC_BH, n * ri->ri_font->fontheight); 697 FBC_WRITE(sc, FFB_FBC_BW, ri->ri_emuwidth); 698 } 699 SYNC; 700 } 701 702 void 703 ffb_ras_erasecols(void *cookie, int row, int col, int n, long attr) 704 { 705 struct rasops_info *ri = cookie; 706 struct vcons_screen *scr = ri->ri_hw; 707 struct ffb_softc *sc = scr->scr_cookie; 708 709 if ((row < 0) || (row >= ri->ri_rows)) 710 return; 711 if (col < 0) { 712 n += col; 713 col = 0; 714 } 715 if (col + n > ri->ri_cols) 716 n = ri->ri_cols - col; 717 if (n <= 0) 718 return; 719 720 n *= ri->ri_font->fontwidth; 721 col *= ri->ri_font->fontwidth; 722 row *= ri->ri_font->fontheight; 723 724 ffb_ras_fill(sc); 725 ffb_ras_setfg(sc, ri->ri_devcmap[(attr >> 16) & 0xf]); 726 ffb_ras_fifo_wait(sc, 4); 727 FBC_WRITE(sc, FFB_FBC_BY, ri->ri_yorigin + row); 728 FBC_WRITE(sc, FFB_FBC_BX, ri->ri_xorigin + col); 729 FBC_WRITE(sc, FFB_FBC_BH, ri->ri_font->fontheight); 730 FBC_WRITE(sc, FFB_FBC_BW, n); 731 SYNC; 732 } 733 734 void 735 ffb_ras_fill(struct ffb_softc *sc) 736 { 737 ffb_ras_fifo_wait(sc, 3); 738 FBC_WRITE(sc, FFB_FBC_PPC, 739 FBC_PPC_VCE_DIS | FBC_PPC_TBE_OPAQUE | FBC_PPC_ACE_DIS | 740 FBC_PPC_APE_DIS | FBC_PPC_DCE_DIS | FBC_PPC_CS_CONST | 741 FBC_PPC_ABE_DIS | FBC_PPC_XS_WID); 742 FBC_WRITE(sc, FFB_FBC_ROP, FBC_ROP_NEW); 743 FBC_WRITE(sc, FFB_FBC_DRAWOP, FBC_DRAWOP_RECTANGLE); 744 SYNC; 745 } 746 747 void 748 ffb_ras_invert(struct ffb_softc *sc) 749 { 750 ffb_ras_fifo_wait(sc, 3); 751 FBC_WRITE(sc, FFB_FBC_PPC, 752 FBC_PPC_VCE_DIS | FBC_PPC_TBE_OPAQUE | FBC_PPC_ACE_DIS | 753 FBC_PPC_APE_DIS | FBC_PPC_DCE_DIS | FBC_PPC_CS_CONST | 754 FBC_PPC_ABE_DIS | FBC_PPC_XS_WID); 755 FBC_WRITE(sc, FFB_FBC_ROP, FBC_ROP_INVERT); 756 FBC_WRITE(sc, FFB_FBC_DRAWOP, FBC_DRAWOP_RECTANGLE); 757 SYNC; 758 } 759 760 void 761 ffb_ras_copyrows(void *cookie, int src, int dst, int n) 762 { 763 struct rasops_info *ri = cookie; 764 struct vcons_screen *scr = ri->ri_hw; 765 struct ffb_softc *sc = scr->scr_cookie; 766 767 if (dst == src) 768 return; 769 if (src < 0) { 770 n += src; 771 src = 0; 772 } 773 if ((src + n) > ri->ri_rows) 774 n = ri->ri_rows - src; 775 if (dst < 0) { 776 n += dst; 777 dst = 0; 778 } 779 if ((dst + n) > ri->ri_rows) 780 n = ri->ri_rows - dst; 781 if (n <= 0) 782 return; 783 n *= ri->ri_font->fontheight; 784 src *= ri->ri_font->fontheight; 785 dst *= ri->ri_font->fontheight; 786 787 ffb_ras_fifo_wait(sc, 9); 788 FBC_WRITE(sc, FFB_FBC_PPC, 789 FBC_PPC_VCE_DIS | FBC_PPC_TBE_OPAQUE | FBC_PPC_ACE_DIS | 790 FBC_PPC_APE_DIS | FBC_PPC_DCE_DIS | FBC_PPC_CS_CONST | 791 FBC_PPC_ABE_DIS | FBC_PPC_XS_WID); 792 FBC_WRITE(sc, FFB_FBC_ROP, FBC_ROP_OLD | (FBC_ROP_OLD << 8)); 793 FBC_WRITE(sc, FFB_FBC_DRAWOP, FBC_DRAWOP_VSCROLL); 794 FBC_WRITE(sc, FFB_FBC_BY, ri->ri_yorigin + src); 795 FBC_WRITE(sc, FFB_FBC_BX, ri->ri_xorigin); 796 FBC_WRITE(sc, FFB_FBC_DY, ri->ri_yorigin + dst); 797 FBC_WRITE(sc, FFB_FBC_DX, ri->ri_xorigin); 798 FBC_WRITE(sc, FFB_FBC_BH, n); 799 FBC_WRITE(sc, FFB_FBC_BW, ri->ri_emuwidth); 800 SYNC; 801 } 802 803 static void 804 ffb_ras_setfg(struct ffb_softc *sc, int32_t fg) 805 { 806 ffb_ras_fifo_wait(sc, 1); 807 if (fg == sc->sc_fg_cache) 808 return; 809 sc->sc_fg_cache = fg; 810 FBC_WRITE(sc, FFB_FBC_FG, fg); 811 SYNC; 812 } 813 814 static void 815 ffb_ras_setbg(struct ffb_softc *sc, int32_t bg) 816 { 817 ffb_ras_fifo_wait(sc, 1); 818 if (bg == sc->sc_bg_cache) 819 return; 820 sc->sc_bg_cache = bg; 821 FBC_WRITE(sc, FFB_FBC_BG, bg); 822 SYNC; 823 } 824 825 /* frame buffer generic driver support functions */ 826 static void 827 ffbfb_unblank(device_t dev) 828 { 829 struct ffb_softc *sc = device_private(dev); 830 struct vcons_screen *ms = sc->vd.active; 831 u_int on = 1; 832 int redraw = 0; 833 834 ffb_ras_init(sc); 835 if (sc->sc_locked) { 836 sc->sc_locked = 0; 837 redraw = 1; 838 } 839 840 ffb_blank(sc, WSDISPLAYIO_SVIDEO, &on); 841 #if 0 842 if ((sc->vd.active != &ffb_console_screen) && 843 (ffb_console_screen.scr_flags & VCONS_SCREEN_IS_STATIC)) { 844 /* 845 * force-switch to the console screen. 846 * Caveat: the higher layer will think we're still on the 847 * other screen 848 */ 849 SCREEN_INVISIBLE(sc->vd.active); 850 sc->vd.active = &ffb_console_screen; 851 SCREEN_VISIBLE(sc->vd.active); 852 ms = sc->vd.active; 853 redraw = 1; 854 } 855 #endif 856 if (redraw) { 857 vcons_redraw_screen(ms); 858 } 859 } 860 861 int 862 ffbfb_open(dev_t dev, int flags, int mode, struct lwp *l) 863 { 864 struct ffb_softc *sc; 865 866 sc = device_lookup_private(&ffb_cd, minor(dev)); 867 if (sc == NULL) 868 return ENXIO; 869 870 sc->sc_locked = 1; 871 return 0; 872 } 873 874 int 875 ffbfb_close(dev_t dev, int flags, int mode, struct lwp *l) 876 { 877 struct ffb_softc *sc = device_lookup_private(&ffb_cd, minor(dev)); 878 struct vcons_screen *ms = sc->vd.active; 879 880 sc->sc_locked = 0; 881 if (ms != NULL) { 882 if ((sc->sc_mode == WSDISPLAYIO_MODE_EMUL) && 883 (sc->sc_locked == 0)) { 884 ffb_ras_init(sc); 885 vcons_redraw_screen(ms); 886 } 887 } 888 return 0; 889 } 890 891 int 892 ffbfb_ioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l) 893 { 894 struct ffb_softc *sc = device_lookup_private(&ffb_cd, minor(dev)); 895 896 return ffb_ioctl(&sc->vd, NULL, cmd, data, flags, l); 897 } 898 899 paddr_t 900 ffbfb_mmap(dev_t dev, off_t off, int prot) 901 { 902 struct ffb_softc *sc = device_lookup_private(&ffb_cd, minor(dev)); 903 uint64_t size; 904 int i, reg; 905 off_t o; 906 907 /* 908 * off is a magic cookie (see xfree86/drivers/sunffb/ffb.h), 909 * which we map to an index into the "reg" property, and use 910 * our copy of the firmware data as arguments for the real 911 * mapping. 912 */ 913 static struct { unsigned long voff; int reg; long flags; } map[] = { 914 { 0x00000000, FFB_REG_SFB8R, BUS_SPACE_MAP_PREFETCHABLE }, 915 { 0x00400000, FFB_REG_SFB8G, BUS_SPACE_MAP_PREFETCHABLE }, 916 { 0x00800000, FFB_REG_SFB8B, BUS_SPACE_MAP_PREFETCHABLE }, 917 { 0x00c00000, FFB_REG_SFB8X, BUS_SPACE_MAP_PREFETCHABLE }, 918 { 0x01000000, FFB_REG_SFB32, BUS_SPACE_MAP_PREFETCHABLE }, 919 { 0x02000000, FFB_REG_SFB64, BUS_SPACE_MAP_PREFETCHABLE }, 920 { 0x04000000, FFB_REG_FBC, 0 }, 921 { 0x04004000, FFB_REG_DFB8R, BUS_SPACE_MAP_PREFETCHABLE }, 922 { 0x04404000, FFB_REG_DFB8G, BUS_SPACE_MAP_PREFETCHABLE }, 923 { 0x04804000, FFB_REG_DFB8B, BUS_SPACE_MAP_PREFETCHABLE }, 924 { 0x04c04000, FFB_REG_DFB8X, BUS_SPACE_MAP_PREFETCHABLE }, 925 { 0x05004000, FFB_REG_DFB24, BUS_SPACE_MAP_PREFETCHABLE }, 926 { 0x06004000, FFB_REG_DFB32, BUS_SPACE_MAP_PREFETCHABLE }, 927 { 0x07004000, FFB_REG_DFB422A, BUS_SPACE_MAP_PREFETCHABLE }, 928 { 0x0bc06000, FFB_REG_DAC, 0 }, 929 { 0x0bc08000, FFB_REG_PROM, 0 }, 930 { 0x0bc18000, 0, 0 } 931 }; 932 933 /* special value "FFB_EXP_VOFF" - not backed by any "reg" entry */ 934 if (off == 0x0bc18000) 935 return bus_space_mmap(sc->sc_bt, sc->sc_addrs[FFB_REG_PROM], 936 0x00200000, prot, BUS_SPACE_MAP_LINEAR); 937 938 /* 939 * FFB_VOFF_FBC_KREGS - used by afbinit to upload firmware. We should 940 * probably mmap them only on afb boards 941 */ 942 if ((off >= 0x0bc04000) && (off < 0x0bc06000)) 943 return bus_space_mmap(sc->sc_bt, sc->sc_addrs[FFB_REG_PROM], 944 0x00610000 + (off - 0x0bc04000), prot, 945 BUS_SPACE_MAP_LINEAR); 946 947 #define NELEMS(arr) (sizeof(arr)/sizeof((arr)[0])) 948 949 /* the map is ordered by voff */ 950 for (i = 0; i < NELEMS(map)-1; i++) { 951 reg = map[i].reg; 952 /* the number of entries in reg seems to vary */ 953 if (reg < sc->sc_nreg) { 954 size = uimin((map[i + 1].voff - map[i].voff), 955 sc->sc_sizes[reg]); 956 if ((off >= map[i].voff) && 957 (off < (map[i].voff + size))) { 958 o = off - map[i].voff; 959 return bus_space_mmap(sc->sc_bt, 960 sc->sc_addrs[reg], o, prot, 961 BUS_SPACE_MAP_LINEAR | map[i].flags); 962 } 963 } 964 } 965 966 return -1; 967 } 968 969 void 970 ffb_clearscreen(struct ffb_softc *sc) 971 { 972 struct rasops_info *ri = &ffb_console_screen.scr_ri; 973 ffb_ras_fill(sc); 974 ffb_ras_setfg(sc, ri->ri_devcmap[WS_DEFAULT_BG]); 975 ffb_ras_fifo_wait(sc, 4); 976 FBC_WRITE(sc, FFB_FBC_BY, 0); 977 FBC_WRITE(sc, FFB_FBC_BX, 0); 978 FBC_WRITE(sc, FFB_FBC_BH, sc->sc_height); 979 FBC_WRITE(sc, FFB_FBC_BW, sc->sc_width); 980 } 981 982 void 983 ffb_cursor(void *cookie, int on, int row, int col) 984 { 985 struct rasops_info *ri = cookie; 986 struct vcons_screen *scr; 987 struct ffb_softc *sc; 988 int x, y, wi, he; 989 990 if (cookie != NULL) { 991 scr = ri->ri_hw; 992 sc = scr->scr_cookie; 993 wi = ri->ri_font->fontwidth; 994 he = ri->ri_font->fontheight; 995 996 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) { 997 if (ri->ri_flg & RI_CURSOR) { 998 999 /* remove cursor */ 1000 x = ri->ri_ccol * wi + ri->ri_xorigin; 1001 y = ri->ri_crow * he + ri->ri_yorigin; 1002 1003 ffb_ras_invert(sc); 1004 ffb_ras_fifo_wait(sc, 4); 1005 FBC_WRITE(sc, FFB_FBC_BY, y); 1006 FBC_WRITE(sc, FFB_FBC_BX, x); 1007 FBC_WRITE(sc, FFB_FBC_BH, he); 1008 FBC_WRITE(sc, FFB_FBC_BW, wi); 1009 1010 ri->ri_flg &= ~RI_CURSOR; 1011 } 1012 ri->ri_crow = row; 1013 ri->ri_ccol = col; 1014 if (on) 1015 { 1016 x = ri->ri_ccol * wi + ri->ri_xorigin; 1017 y = ri->ri_crow * he + ri->ri_yorigin; 1018 1019 ffb_ras_invert(sc); 1020 ffb_ras_fifo_wait(sc, 4); 1021 FBC_WRITE(sc, FFB_FBC_BY, y); 1022 FBC_WRITE(sc, FFB_FBC_BX, x); 1023 FBC_WRITE(sc, FFB_FBC_BH, he); 1024 FBC_WRITE(sc, FFB_FBC_BW, wi); 1025 1026 ri->ri_flg |= RI_CURSOR; 1027 } 1028 } else { 1029 ri->ri_crow = row; 1030 ri->ri_ccol = col; 1031 ri->ri_flg &= ~RI_CURSOR; 1032 } 1033 } 1034 } 1035 1036 /* mono bitmap font */ 1037 void 1038 ffb_putchar_mono(void *cookie, int row, int col, u_int c, long attr) 1039 { 1040 struct rasops_info *ri = cookie; 1041 struct vcons_screen *scr = ri->ri_hw; 1042 struct wsdisplay_font *font = PICK_FONT(ri, c); 1043 struct ffb_softc *sc = scr->scr_cookie; 1044 void *data; 1045 uint32_t fg, bg; 1046 int i; 1047 int x, y, wi, he; 1048 1049 if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL) 1050 return; 1051 1052 wi = font->fontwidth; 1053 he = font->fontheight; 1054 1055 if (!CHAR_IN_FONT(c, font)) 1056 return; 1057 1058 bg = ri->ri_devcmap[(attr >> 16) & 0xf]; 1059 fg = ri->ri_devcmap[(attr >> 24) & 0xf]; 1060 x = ri->ri_xorigin + col * wi; 1061 y = ri->ri_yorigin + row * he; 1062 1063 data = WSFONT_GLYPH(c, font); 1064 1065 ffb_ras_setbg(sc, bg); 1066 ffb_ras_setfg(sc, fg); 1067 ffb_ras_fifo_wait(sc, 4); 1068 FBC_WRITE(sc, FFB_FBC_ROP, FBC_ROP_NEW); 1069 FBC_WRITE(sc, FFB_FBC_FONTXY, (y << 16) | x); 1070 FBC_WRITE(sc, FFB_FBC_FONTW, wi); 1071 FBC_WRITE(sc, FFB_FBC_PPC, 1072 FBC_PPC_VCE_DIS | FBC_PPC_TBE_OPAQUE | FBC_PPC_ACE_DIS | 1073 FBC_PPC_APE_DIS | FBC_PPC_DCE_DIS | FBC_PPC_CS_CONST | 1074 FBC_PPC_ABE_DIS | FBC_PPC_XS_WID); 1075 1076 if (attr & WSATTR_HILIT) { 1077 switch (font->stride) { 1078 case 1: { 1079 uint8_t *data8 = data; 1080 uint32_t reg; 1081 for (i = 0; i < he; i++) { 1082 reg = *data8; 1083 reg |= reg >> 1; 1084 FBC_WRITE(sc, FFB_FBC_FONT, reg << 24); 1085 data8++; 1086 } 1087 break; 1088 } 1089 case 2: { 1090 uint16_t *data16 = data; 1091 uint32_t reg; 1092 for (i = 0; i < he; i++) { 1093 reg = *data16; 1094 reg |= reg >> 1; 1095 FBC_WRITE(sc, FFB_FBC_FONT, reg << 16); 1096 data16++; 1097 } 1098 break; 1099 } 1100 } 1101 } else { 1102 switch (font->stride) { 1103 case 1: { 1104 uint8_t *data8 = data; 1105 uint32_t reg; 1106 for (i = 0; i < he; i++) { 1107 reg = *data8; 1108 FBC_WRITE(sc, FFB_FBC_FONT, reg << 24); 1109 data8++; 1110 } 1111 break; 1112 } 1113 case 2: { 1114 uint16_t *data16 = data; 1115 uint32_t reg; 1116 for (i = 0; i < he; i++) { 1117 reg = *data16; 1118 FBC_WRITE(sc, FFB_FBC_FONT, reg << 16); 1119 data16++; 1120 } 1121 break; 1122 } 1123 } 1124 } 1125 if (attr & WSATTR_UNDERLINE) { 1126 int ul = he - ((he < 16) ? 1 : 2); 1127 ffb_ras_fifo_wait(sc, 2); 1128 FBC_WRITE(sc, FFB_FBC_FONTXY, ((y + ul) << 16) | x); 1129 FBC_WRITE(sc, FFB_FBC_FONT, 0xffff0000); 1130 } 1131 } 1132 1133 /* alpha font */ 1134 void 1135 ffb_putchar_aa(void *cookie, int row, int col, u_int c, long attr) 1136 { 1137 struct rasops_info *ri = cookie; 1138 struct vcons_screen *scr = ri->ri_hw; 1139 struct wsdisplay_font *font = PICK_FONT(ri, c); 1140 struct ffb_softc *sc = scr->scr_cookie; 1141 volatile uint32_t *dest, *ddest; 1142 uint8_t *data8; 1143 uint32_t fg, bg; 1144 int i; 1145 int x, y, wi, he; 1146 uint32_t alpha = 0x80, old = 0; 1147 int j; 1148 1149 if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL) 1150 return; 1151 1152 wi = font->fontwidth; 1153 he = font->fontheight; 1154 1155 if (!CHAR_IN_FONT(c, font)) 1156 return; 1157 1158 bg = ri->ri_devcmap[(attr >> 16) & 0xf]; 1159 fg = ri->ri_devcmap[(attr >> 24) & 0xf]; 1160 x = ri->ri_xorigin + col * wi; 1161 y = ri->ri_yorigin + row * he; 1162 1163 data8 = WSFONT_GLYPH(c, font); 1164 1165 /* first we erase the background */ 1166 ffb_ras_fill(sc); 1167 ffb_ras_setfg(sc, bg); 1168 ffb_ras_fifo_wait(sc, 4); 1169 FBC_WRITE(sc, FFB_FBC_BY, y); 1170 FBC_WRITE(sc, FFB_FBC_BX, x); 1171 FBC_WRITE(sc, FFB_FBC_BH, he); 1172 FBC_WRITE(sc, FFB_FBC_BW, wi); 1173 1174 /* if we draw a space we're done */ 1175 if (c == ' ') goto out; 1176 1177 /* now enable alpha blending */ 1178 ffb_ras_setfg(sc, fg); 1179 ffb_ras_fifo_wait(sc, 2); 1180 FBC_WRITE(sc, FFB_FBC_ROP, FBC_ROP_NEW); 1181 1182 FBC_WRITE(sc, FFB_FBC_PPC, 1183 FBC_PPC_VCE_DIS | FBC_PPC_TBE_OPAQUE | FBC_PPC_ACE_DIS | 1184 FBC_PPC_APE_DIS | FBC_PPC_DCE_DIS | FBC_PPC_CS_CONST | 1185 FBC_PPC_ABE_ENA | FBC_PPC_XS_VAR); 1186 /* 1187 * we have to wait for both the rectangle drawing op above and the 1188 * FFB_FBC_PPC write to finish before mucking around in the SFB aperture 1189 */ 1190 ffb_ras_wait(sc); 1191 1192 /* ... and draw the character */ 1193 /* 1194 * We set the colour source to constant above so we only 1195 * have to write the alpha channel here and the colour 1196 * comes from the FG register. It would be nice if we 1197 * could just use the SFB8X aperture and memcpy() the 1198 * alpha map line by line but for some strange reason 1199 * that will take colour info from the framebuffer even 1200 * if we set the FBC_PPC_CS_CONST bit above. 1201 */ 1202 dest = sc->sc_sfb32 + (y << 11) + x; 1203 for (i = 0; i < he; i++) { 1204 ddest = dest; 1205 old = 0; 1206 if (attr & WSATTR_HILIT) { 1207 for (j = 0; j < wi; j++) { 1208 int new = *data8; 1209 alpha = new + old; 1210 if (alpha > 255) alpha = 255; 1211 old = new; 1212 *ddest = alpha << 24; 1213 data8++; 1214 ddest++; 1215 } 1216 dest += 2048; 1217 } else { 1218 for (j = 0; j < wi; j++) { 1219 alpha = *data8; 1220 *ddest = alpha << 24; 1221 data8++; 1222 ddest++; 1223 } 1224 dest += 2048; 1225 } 1226 } 1227 out: 1228 /* check if we need to draw an underline */ 1229 if (attr & WSATTR_UNDERLINE) { 1230 dest = sc->sc_sfb32 + ((y + he - 2) << 11) + x; 1231 for (i = 0; i < wi; i++) { 1232 *dest = 0xa0000000; 1233 dest++; 1234 } 1235 } 1236 } 1237 1238 int 1239 ffb_allocattr(void *cookie, int fg0, int bg0, int flg, long *attrp) 1240 { 1241 int fg = fg0, bg = bg0; 1242 1243 if ((flg & WSATTR_BLINK) != 0) 1244 return EINVAL; 1245 1246 if ((flg & WSATTR_REVERSE) != 0) { 1247 fg = bg0; 1248 bg = fg0; 1249 } 1250 1251 *attrp = (bg << 16) | (fg << 24) | flg; 1252 return 0; 1253 } 1254 1255 void 1256 ffb_init_screen(void *cookie, struct vcons_screen *scr, 1257 int existing, long *defattr) 1258 { 1259 struct ffb_softc *sc = cookie; 1260 struct rasops_info *ri = &scr->scr_ri; 1261 1262 ri->ri_depth = 32; 1263 ri->ri_width = sc->sc_width; 1264 ri->ri_height = sc->sc_height; 1265 ri->ri_stride = sc->sc_linebytes; 1266 ri->ri_flg = RI_CENTER | RI_FULLCLEAR | 1267 RI_ENABLE_ALPHA | RI_PREFER_ALPHA; 1268 1269 /* 1270 * we can't accelerate copycols() so instead of falling back to 1271 * software use vcons' putchar() based implementation 1272 */ 1273 scr->scr_flags |= VCONS_NO_COPYCOLS | VCONS_LOADFONT; 1274 1275 #ifdef VCONS_DRAW_INTR 1276 scr->scr_flags |= VCONS_DONT_READ; 1277 #endif 1278 DPRINTF(("ffb_init_screen: addr: %08lx\n",(ulong)ri->ri_bits)); 1279 1280 /* explicitly request BGR in case the default changes */ 1281 ri->ri_rnum = 8; 1282 ri->ri_gnum = 8; 1283 ri->ri_bnum = 8; 1284 ri->ri_rpos = 0; 1285 ri->ri_gpos = 8; 1286 ri->ri_bpos = 16; 1287 1288 rasops_init(ri, 0, 0); 1289 rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight, 1290 sc->sc_width / ri->ri_font->fontwidth); 1291 ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_UNDERLINE | WSSCREEN_HILIT | 1292 WSSCREEN_REVERSE | WSSCREEN_RESIZE; 1293 1294 /* enable acceleration */ 1295 ri->ri_ops.copyrows = ffb_ras_copyrows; 1296 ri->ri_ops.eraserows = ffb_ras_eraserows; 1297 ri->ri_ops.erasecols = ffb_ras_erasecols; 1298 ri->ri_ops.cursor = ffb_cursor; 1299 ri->ri_ops.allocattr = ffb_allocattr; 1300 if (FONT_IS_ALPHA(ri->ri_font)) { 1301 ri->ri_ops.putchar = ffb_putchar_aa; 1302 } else 1303 ri->ri_ops.putchar = ffb_putchar_mono; 1304 } 1305 1306 /* I2C bitbanging */ 1307 static void ffb_i2cbb_set_bits(void *cookie, uint32_t bits) 1308 { 1309 struct ffb_softc *sc = cookie; 1310 1311 DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_CFG_MPDATA); 1312 DAC_WRITE(sc, FFB_DAC_VALUE, bits); 1313 } 1314 1315 static void ffb_i2cbb_set_dir(void *cookie, uint32_t dir) 1316 { 1317 /* Nothing to do */ 1318 } 1319 1320 static uint32_t ffb_i2cbb_read(void *cookie) 1321 { 1322 struct ffb_softc *sc = cookie; 1323 uint32_t bits; 1324 1325 DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_CFG_MPSENSE); 1326 bits = DAC_READ(sc, FFB_DAC_VALUE); 1327 1328 return bits; 1329 } 1330 1331 /* higher level I2C stuff */ 1332 static int 1333 ffb_i2c_send_start(void *cookie, int flags) 1334 { 1335 return (i2c_bitbang_send_start(cookie, flags, &ffb_i2cbb_ops)); 1336 } 1337 1338 static int 1339 ffb_i2c_send_stop(void *cookie, int flags) 1340 { 1341 1342 return (i2c_bitbang_send_stop(cookie, flags, &ffb_i2cbb_ops)); 1343 } 1344 1345 static int 1346 ffb_i2c_initiate_xfer(void *cookie, i2c_addr_t addr, int flags) 1347 { 1348 /* 1349 * for some reason i2c_bitbang_initiate_xfer left-shifts 1350 * the I2C-address and then sets the direction bit 1351 */ 1352 return (i2c_bitbang_initiate_xfer(cookie, addr, flags, 1353 &ffb_i2cbb_ops)); 1354 } 1355 1356 static int 1357 ffb_i2c_read_byte(void *cookie, uint8_t *valp, int flags) 1358 { 1359 return (i2c_bitbang_read_byte(cookie, valp, flags, &ffb_i2cbb_ops)); 1360 } 1361 1362 static int 1363 ffb_i2c_write_byte(void *cookie, uint8_t val, int flags) 1364 { 1365 return (i2c_bitbang_write_byte(cookie, val, flags, &ffb_i2cbb_ops)); 1366 } 1367 1368 1369 #define TVC_READ_LIMIT 100000 1370 int 1371 ffb_tgc_disable(struct ffb_softc *sc) 1372 { 1373 int i; 1374 1375 /* Is the timing generator disabled? */ 1376 DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_TGC); 1377 if (!(DAC_READ(sc, FFB_DAC_VALUE) & FFB_DAC_TGC_TIMING_ENABLE)) 1378 return 1; 1379 1380 /* If not, disable it when the vertical counter reaches 0 */ 1381 for (i = 0; i < TVC_READ_LIMIT; i++) { 1382 DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_TVC); 1383 if (!DAC_READ(sc, FFB_DAC_VALUE)) { 1384 DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_TGC); 1385 DAC_WRITE(sc, FFB_DAC_VALUE, 0); 1386 return 1; 1387 } 1388 } 1389 return 0; 1390 } 1391 1392 /* 1393 * PLL Control Register values: 1394 * M)ultiplier = bits 0:6 + 1 1395 * D)ivisor = bits 7:10 + 1 1396 * P)ost divisor = bits 11:13 (000 = 1, 001 = 2, 010 = 4, 011 = 8) 1397 * Frequency = 13.5 * M / D / P 1398 */ 1399 #define FFB_PLL_FREQ 13500000 1400 void 1401 ffb_get_pclk(int request, uint32_t *pll, int *diff) 1402 { 1403 int m, d, p, f, hex = 0, curdiff; 1404 1405 *diff = 100000000; 1406 1407 for (m = 32; m <= 80; m++) { 1408 for (d = 4; d <= 11; d++) { 1409 for (p = 1; p <= 8; p = p << 1) { 1410 switch (p) { 1411 case 1: 1412 hex = 0x4000 + (d << 7) + m; 1413 break; 1414 case 2: 1415 hex = 0x4800 + (d << 7) + m; 1416 break; 1417 case 4: 1418 hex = 0x5000 + (d << 7) + m; 1419 break; 1420 case 8: 1421 hex = 0x6000 + (d << 7) + m; 1422 break; 1423 } 1424 f = 13500000 * m / d / p; 1425 if (f == request) { 1426 *diff = 0; 1427 *pll = hex; 1428 return; 1429 } else { 1430 curdiff = abs(request - f); 1431 if (curdiff < *diff) { 1432 *diff = curdiff; 1433 *pll = hex; 1434 } 1435 } 1436 } 1437 } 1438 } 1439 } 1440 1441 /* 1442 * Details of the FFB RAMDAC are contained in the Brooktree BT497/498 1443 * and in the Connexant BT497A/498A documentation. 1444 * 1445 * VESA timings to FFB register conversion: 1446 * If interleave = 4/2:1 then x = 2, if interleave = 8/2:1 then x = 4 1447 * VBE = VBS - vres = (sync pulse - 1) + back porch 1448 * VBS = VSS - front porch = (sync pulse - 1) + back porch + vres 1449 * VSE = sync pulse - 1 1450 * VSS = (sync pulse - 1) + back porch + vres + front porch 1451 * HRE = HSS - HSE - 1 1452 * HBE = (sync pulse + back porch) / x - 1 1453 * HBS = (sync pulse + back porch + hres) / x - 1 1454 * HSE = sync pulse / x - 1 1455 * HSS = (sync pulse + back porch + hres + front porch) / x - 1 1456 * HCE = HBS - 4 1457 * HCS = HBE - 4 1458 * EPE = EIE = EIS = 0 (for all non-interlaced modes) 1459 * 1460 * Note, that 8/2:1 Single Buffered Interleaving is only supported by the 1461 * double-buffered FFB (Creator3D), and at higher resolutions than 1280x1024 1462 * 1463 * Note, that the timing generator should be disabled and re-enabled when the 1464 * the timing parameter registers are being programmed. Stopping the timing 1465 * generator should only be done when the vertical counter is zero. 1466 */ 1467 #define DIVIDE(x,y) (((x) + ((y) / 2)) / (y)) 1468 int 1469 ffb_set_vmode(struct ffb_softc *sc, struct videomode *mode, int btype, 1470 int *hres, int *vres) 1471 { 1472 int diff; 1473 uint32_t fp, sp, bp, x; 1474 uint32_t pll, pfc, ucl, dcl, tgc; 1475 uint32_t vbe, vbs, vse, vss, hre, hbe, hbs, hse, hss, hce, hcs; 1476 uint32_t epe, eie, eis; 1477 uint32_t fbcfg0; 1478 1479 DPRINTF(("ffb_set_vmode: %dx%d@%d", mode->hdisplay, mode->vdisplay, 1480 DIVIDE(DIVIDE(mode->dot_clock * 1000, 1481 mode->htotal), mode->vtotal))); 1482 DPRINTF((" (%d %d %d %d %d %d %d", 1483 mode->dot_clock, mode->hsync_start, mode->hsync_end, mode->htotal, 1484 mode->vsync_start, mode->vsync_end, mode->vtotal)); 1485 DPRINTF((" %s%sH %s%sV)\n", 1486 mode->flags & VID_PHSYNC ? "+" : "", 1487 mode->flags & VID_NHSYNC ? "-" : "", 1488 mode->flags & VID_PVSYNC ? "+" : "", 1489 mode->flags & VID_NVSYNC ? "-" : "")); 1490 1491 /* We don't handle interlaced or doublescan (yet) */ 1492 if ((mode->flags & VID_INTERLACE) || (mode->flags & VID_DBLSCAN)) 1493 return 0; 1494 1495 /* Only Creator3D can be set to > 1280x1024 */ 1496 if(((sc->sc_type == FFB_CREATOR && !((btype & 7) == 3)) || 1497 sc->sc_type == FFB_AFB) 1498 && (mode->hdisplay > 1280 || mode->vdisplay > 1024)) 1499 return 0; 1500 /* Creator3D can be set to <= 1920x1360 */ 1501 if (mode->hdisplay > 1920 || mode->vdisplay > 1360) 1502 return 0; 1503 1504 /* 1505 * Look for a matching pixel clock and set PLL Control. 1506 * XXX: 640x480@60 is 25175000 in modelines but 25125000 in the 1507 * FFB PROM, and the closest match to 25175000 (0x4da9/25159090) 1508 * does not work. So, use the PROM value instead. 1509 */ 1510 if (mode->hdisplay == 640 && mode->vdisplay == 480 && 1511 mode->dot_clock == 25175) { 1512 DPRINTF(("ffb_set_vmode: 640x480@60: adjusted dot clock\n")); 1513 mode->dot_clock = 25125; 1514 } 1515 ffb_get_pclk(mode->dot_clock * 1000, &pll, &diff); 1516 if (diff > 250000) 1517 return 0; 1518 1519 /* Pixel Format Control, User Control and FBC Configuration. */ 1520 if (mode->hdisplay > 1280) { 1521 pfc = FFB_DAC_PIX_FMT_821; 1522 ucl = FFB_DAC_USR_CTRL_OVERLAY | FFB_DAC_USR_CTRL_WMODE_C; 1523 x = 4; 1524 fbcfg0 = FBC_READ(sc, FFB_FBC_FBCFG0) | FBC_CFG0_DOUBLE_BUF; 1525 } else { 1526 pfc = FFB_DAC_PIX_FMT_421; 1527 /* Only Creator3D and Elite3D can have double-buffer */ 1528 if ((sc->sc_type == FFB_CREATOR && !((btype & 7) == 3))) 1529 ucl = 0; 1530 else 1531 ucl = FFB_DAC_USR_CTRL_DOUBLE; 1532 ucl |= (FFB_DAC_USR_CTRL_OVERLAY | FFB_DAC_USR_CTRL_WMODE_S8); 1533 x = 2; 1534 fbcfg0 = FBC_READ(sc, FFB_FBC_FBCFG0) | FBC_CFG0_SINGLE_BUF; 1535 } 1536 1537 /* DAC Control and Timing Generator Control */ 1538 if (mode->flags & VID_PVSYNC) 1539 dcl = FFB_DAC_DAC_CTRL_POS_VSYNC; 1540 else 1541 dcl = 0; 1542 tgc = 0; 1543 #define EDID_VID_INP sc->sc_edid_info.edid_video_input 1544 if ((EDID_VID_INP & EDID_VIDEO_INPUT_COMPOSITE_SYNC)) { 1545 dcl |= FFB_DAC_DAC_CTRL_VSYNC_DIS; 1546 tgc = FFB_DAC_TGC_EQUAL_DISABLE; 1547 } else { 1548 dcl |= FFB_DAC_DAC_CTRL_SYNC_G; 1549 if (EDID_VID_INP & EDID_VIDEO_INPUT_SEPARATE_SYNCS) 1550 tgc |= FFB_DAC_TGC_VSYNC_DISABLE; 1551 else 1552 tgc = FFB_DAC_TGC_EQUAL_DISABLE; 1553 } 1554 if (EDID_VID_INP & EDID_VIDEO_INPUT_BLANK_TO_BLACK) 1555 dcl |= FFB_DAC_DAC_CTRL_PED_ENABLE; 1556 tgc |= (FFB_DAC_TGC_VIDEO_ENABLE | FFB_DAC_TGC_TIMING_ENABLE | 1557 FFB_DAC_TGC_MASTER_ENABLE); 1558 1559 /* Vertical timing */ 1560 fp = mode->vsync_start - mode->vdisplay; 1561 sp = mode->vsync_end - mode->vsync_start; 1562 bp = mode->vtotal - mode->vsync_end; 1563 1564 vbe = sp - 1 + bp; 1565 vbs = sp - 1 + bp + mode->vdisplay; 1566 vse = sp - 1; 1567 vss = sp - 1 + bp + mode->vdisplay + fp; 1568 1569 /* Horizontal timing */ 1570 fp = mode->hsync_start - mode->hdisplay; 1571 sp = mode->hsync_end - mode->hsync_start; 1572 bp = mode->htotal - mode->hsync_end; 1573 1574 hbe = (sp + bp) / x - 1; 1575 hbs = (sp + bp + mode->hdisplay) / x - 1; 1576 hse = sp / x - 1; 1577 hss = (sp + bp + mode->hdisplay + fp) / x -1; 1578 hre = hss - hse - 1; 1579 hce = hbs - 4; 1580 hcs = hbe - 4; 1581 1582 /* Equalisation (interlaced modes) */ 1583 epe = 0; 1584 eie = 0; 1585 eis = 0; 1586 1587 DPRINTF(("ffb_set_vmode: 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x\n", 1588 pll, pfc, ucl, dcl, tgc)); 1589 DPRINTF(("\t0x%04x 0x%04x 0x%04x 0x%04x\n", vbe, vbs, vse, vss)); 1590 DPRINTF(("\t0x%04x 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x\n", 1591 hre, hbe, hbs, hse, hss, hce, hcs)); 1592 DPRINTF(("\t0x%04x 0x%04x 0x%04x\n", epe, eie, eis)); 1593 1594 if (!ffb_tgc_disable(sc)) { 1595 DPRINTF(("ffb_set_vmode: failed to disable TGC register\n")); 1596 return 0; 1597 } 1598 1599 /* 1600 * Program the mode registers. 1601 * Program the timing generator last, as that re-enables output. 1602 * Note, that a read to/write from a register increments the 1603 * register address to the next register automatically. 1604 */ 1605 DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_PLL_CTRL); 1606 DAC_WRITE(sc, FFB_DAC_VALUE, pll); 1607 DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_PIX_FMT); 1608 DAC_WRITE(sc, FFB_DAC_VALUE, pfc); 1609 DAC_WRITE(sc, FFB_DAC_VALUE, ucl); 1610 DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_DAC_CTRL); 1611 DAC_WRITE(sc, FFB_DAC_VALUE, dcl); 1612 1613 DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_VBE); 1614 DAC_WRITE(sc, FFB_DAC_VALUE, vbe); 1615 DAC_WRITE(sc, FFB_DAC_VALUE, vbs); 1616 DAC_WRITE(sc, FFB_DAC_VALUE, vse); 1617 DAC_WRITE(sc, FFB_DAC_VALUE, vss); 1618 1619 DAC_WRITE(sc, FFB_DAC_VALUE, hre); 1620 DAC_WRITE(sc, FFB_DAC_VALUE, hbe); 1621 DAC_WRITE(sc, FFB_DAC_VALUE, hbs); 1622 DAC_WRITE(sc, FFB_DAC_VALUE, hse); 1623 DAC_WRITE(sc, FFB_DAC_VALUE, hss); 1624 DAC_WRITE(sc, FFB_DAC_VALUE, hce); 1625 DAC_WRITE(sc, FFB_DAC_VALUE, hcs); 1626 1627 DAC_WRITE(sc, FFB_DAC_VALUE, epe); 1628 DAC_WRITE(sc, FFB_DAC_VALUE, eie); 1629 DAC_WRITE(sc, FFB_DAC_VALUE, eis); 1630 1631 FBC_WRITE(sc, FFB_FBC_FBCFG0, fbcfg0); 1632 1633 DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_TGC); 1634 DAC_WRITE(sc, FFB_DAC_VALUE, tgc); 1635 DPRINTF(("new tgc: %08x\n", tgc)); 1636 1637 *hres = mode->hdisplay; 1638 *vres = mode->vdisplay; 1639 1640 printf("%s: video mode set to %d x %d @ %dHz\n", 1641 device_xname(sc->sc_dev), 1642 mode->hdisplay, mode->vdisplay, 1643 DIVIDE(DIVIDE(mode->dot_clock * 1000, 1644 mode->htotal), mode->vtotal)); 1645 1646 return 1; 1647 } 1648