1 /* $NetBSD: r128fb.c,v 1.51 2026/11/30 10:43:11 macallan Exp $ */ 2 3 /* 4 * Copyright (c) 2007, 2012 Michael Lorenz 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 /* 29 * A console driver for ATI Rage 128 graphics controllers 30 * tested on macppc only so far 31 */ 32 33 #include <sys/cdefs.h> 34 __KERNEL_RCSID(0, "$NetBSD: r128fb.c,v 1.51 2026/11/30 10:43:11 macallan Exp $"); 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/kernel.h> 39 #include <sys/device.h> 40 #include <sys/lwp.h> 41 #include <sys/kauth.h> 42 43 #include <dev/pci/pcivar.h> 44 #include <dev/pci/pcireg.h> 45 #include <dev/pci/pcidevs.h> 46 #include <dev/pci/pciio.h> 47 #include <dev/pci/r128fbreg.h> 48 49 #include <dev/wscons/wsdisplayvar.h> 50 #include <dev/wscons/wsconsio.h> 51 #include <dev/wsfont/wsfont.h> 52 #include <dev/rasops/rasops.h> 53 #include <dev/wscons/wsdisplay_vconsvar.h> 54 #include <dev/pci/wsdisplay_pci.h> 55 #include <dev/wscons/wsdisplay_glyphcachevar.h> 56 57 #include "opt_r128fb.h" 58 #include "opt_vcons.h" 59 60 #ifdef R128FB_DEBUG 61 #define DPRINTF printf 62 #else 63 #define DPRINTF while(0) printf 64 #endif 65 66 struct r128fb_softc { 67 device_t sc_dev; 68 69 pci_chipset_tag_t sc_pc; 70 pcitag_t sc_pcitag; 71 72 bus_space_tag_t sc_memt; 73 bus_space_tag_t sc_iot; 74 75 bus_space_handle_t sc_regh, sc_cursorh; 76 bus_addr_t sc_fb, sc_reg; 77 bus_size_t sc_fbsize, sc_regsize; 78 int sc_cursor_offset; 79 uint32_t *sc_cursor; 80 int sc_cursor_x, sc_cursor_y, sc_hot_x, sc_hot_y; 81 82 int sc_width, sc_height, sc_depth, sc_stride; 83 int sc_locked, sc_have_backlight, sc_bl_level, sc_bl_on; 84 struct vcons_screen sc_console_screen; 85 struct wsscreen_descr sc_defaultscreen_descr; 86 const struct wsscreen_descr *sc_screens[1]; 87 struct wsscreen_list sc_screenlist; 88 struct vcons_data vd; 89 int sc_mode, sc_video; 90 u_char sc_cmap_red[256]; 91 u_char sc_cmap_green[256]; 92 u_char sc_cmap_blue[256]; 93 /* engine stuff */ 94 uint32_t sc_master_cntl; 95 glyphcache sc_gc; 96 }; 97 98 static int r128fb_match(device_t, cfdata_t, void *); 99 static void r128fb_attach(device_t, device_t, void *); 100 101 CFATTACH_DECL_NEW(r128fb, sizeof(struct r128fb_softc), 102 r128fb_match, r128fb_attach, NULL, NULL); 103 104 static int r128fb_ioctl(void *, void *, u_long, void *, int, 105 struct lwp *); 106 static paddr_t r128fb_mmap(void *, void *, off_t, int); 107 static void r128fb_init_screen(void *, struct vcons_screen *, int, long *); 108 109 static int r128fb_putcmap(struct r128fb_softc *, struct wsdisplay_cmap *); 110 static int r128fb_getcmap(struct r128fb_softc *, struct wsdisplay_cmap *); 111 static void r128fb_restore_palette(struct r128fb_softc *); 112 static int r128fb_putpalreg(struct r128fb_softc *, uint8_t, uint8_t, 113 uint8_t, uint8_t); 114 115 static void r128fb_init(struct r128fb_softc *); 116 static void r128fb_flush_engine(struct r128fb_softc *); 117 static void r128fb_rectfill(struct r128fb_softc *, int, int, int, int, 118 uint32_t); 119 static void r128fb_rectfill_a(void *, int, int, int, int, long); 120 static void r128fb_bitblt(void *, int, int, int, int, int, 121 int, int); 122 123 static void r128fb_cursor(void *, int, int, int); 124 static int r128fb_allocattr(void *, int, int, int, long *); 125 static void r128fb_putchar(void *, int, int, u_int, long); 126 static void r128fb_putchar_aa(void *, int, int, u_int, long); 127 static void r128fb_copycols(void *, int, int, int, int); 128 static void r128fb_erasecols(void *, int, int, int, long); 129 static void r128fb_copyrows(void *, int, int, int); 130 static void r128fb_eraserows(void *, int, int, long); 131 132 static void r128fb_brightness_up(device_t); 133 static void r128fb_brightness_down(device_t); 134 /* set backlight level */ 135 static void r128fb_set_backlight(struct r128fb_softc *, int); 136 /* turn backlight on and off without messing with the level */ 137 static void r128fb_switch_backlight(struct r128fb_softc *, int); 138 static void r128fb_set_video(struct r128fb_softc *, int); 139 140 static int r128fb_set_cursor(struct r128fb_softc *, struct wsdisplay_cursor *); 141 static int r128fb_set_curpos(struct r128fb_softc *, struct wsdisplay_curpos *); 142 143 struct wsdisplay_accessops r128fb_accessops = { 144 r128fb_ioctl, 145 r128fb_mmap, 146 NULL, /* alloc_screen */ 147 NULL, /* free_screen */ 148 NULL, /* show_screen */ 149 NULL, /* load_font */ 150 NULL, /* pollc */ 151 NULL /* scroll */ 152 }; 153 154 static inline void 155 r128fb_wait(struct r128fb_softc *sc, int slots) 156 { 157 uint32_t reg; 158 159 do { 160 reg = (bus_space_read_4(sc->sc_memt, sc->sc_regh, 161 R128_GUI_STAT) & R128_GUI_FIFOCNT_MASK); 162 } while (reg <= slots); 163 } 164 165 static void 166 r128fb_flush_engine(struct r128fb_softc *sc) 167 { 168 uint32_t reg; 169 170 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_PC_NGUI_CTLSTAT); 171 reg |= R128_PC_FLUSH_ALL; 172 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PC_NGUI_CTLSTAT, reg); 173 do { 174 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, 175 R128_PC_NGUI_CTLSTAT); 176 } while (reg & R128_PC_BUSY); 177 } 178 179 static int 180 r128fb_match(device_t parent, cfdata_t match, void *aux) 181 { 182 struct pci_attach_args *pa = (struct pci_attach_args *)aux; 183 184 if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY) 185 return 0; 186 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_ATI) 187 return 0; 188 189 /* only cards tested on so far - likely need a list */ 190 if ((PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGE1AGP4XT) || 191 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGE3AGP4XT) || 192 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGEVRAGP) || 193 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGEGLPCI) || 194 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGE_MOB_M3_AGP)) 195 return 100; 196 return (0); 197 } 198 199 static void 200 r128fb_attach(device_t parent, device_t self, void *aux) 201 { 202 struct r128fb_softc *sc = device_private(self); 203 struct pci_attach_args *pa = aux; 204 struct rasops_info *ri; 205 bus_space_tag_t tag; 206 struct wsemuldisplaydev_attach_args aa; 207 prop_dictionary_t dict; 208 unsigned long defattr; 209 bool is_console = FALSE; 210 int i, j; 211 uint32_t reg, flags; 212 uint8_t cmap[768]; 213 214 sc->sc_pc = pa->pa_pc; 215 sc->sc_pcitag = pa->pa_tag; 216 sc->sc_memt = pa->pa_memt; 217 sc->sc_iot = pa->pa_iot; 218 sc->sc_dev = self; 219 220 pci_aprint_devinfo(pa, NULL); 221 222 /* fill in parameters from properties */ 223 dict = device_properties(self); 224 if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) { 225 aprint_error("%s: no width property\n", device_xname(self)); 226 return; 227 } 228 if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) { 229 aprint_error("%s: no height property\n", device_xname(self)); 230 return; 231 } 232 233 #ifdef GLYPHCACHE_DEBUG 234 /* leave some visible VRAM unused so we can see the glyph cache */ 235 sc->sc_height -= 200; 236 #endif 237 238 if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) { 239 aprint_error("%s: no depth property\n", device_xname(self)); 240 return; 241 } 242 if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->sc_stride)) { 243 aprint_error("%s: no linebytes property\n", 244 device_xname(self)); 245 return; 246 } 247 248 prop_dictionary_get_bool(dict, "is_console", &is_console); 249 250 if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, 0x10, PCI_MAPREG_TYPE_MEM, 251 &sc->sc_fb, &sc->sc_fbsize, &flags)) { 252 aprint_error("%s: failed to map the frame buffer.\n", 253 device_xname(sc->sc_dev)); 254 } 255 256 if (pci_mapreg_map(pa, 0x18, PCI_MAPREG_TYPE_MEM, 0, 257 &tag, &sc->sc_regh, &sc->sc_reg, &sc->sc_regsize)) { 258 aprint_error("%s: failed to map registers.\n", 259 device_xname(sc->sc_dev)); 260 } 261 262 sc->sc_fbsize = bus_space_read_4(sc->sc_memt, sc->sc_regh, 263 R128_MEMORY_CONFIG); 264 sc->sc_cursor_offset = sc->sc_fbsize - PAGE_SIZE; 265 aprint_normal("%s: %d MB video memory at 0x%08x\n", device_xname(self), 266 (int)(sc->sc_fbsize >> 20), (uint32_t)sc->sc_fb); 267 268 if (bus_space_map(sc->sc_memt, sc->sc_fb + sc->sc_cursor_offset, 269 PAGE_SIZE, BUS_SPACE_MAP_LINEAR, &sc->sc_cursorh)) { 270 aprint_error_dev(sc->sc_dev, "failed to map cursor area\n"); 271 } 272 sc->sc_cursor = bus_space_vaddr(sc->sc_memt, sc->sc_cursorh); 273 274 sc->sc_defaultscreen_descr = (struct wsscreen_descr){ 275 "default", 276 0, 0, 277 NULL, 278 8, 16, 279 WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE | 280 WSSCREEN_RESIZE, 281 NULL 282 }; 283 284 sc->sc_video = WSDISPLAYIO_VIDEO_ON; 285 286 sc->sc_screens[0] = &sc->sc_defaultscreen_descr; 287 sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens}; 288 sc->sc_mode = WSDISPLAYIO_MODE_EMUL; 289 sc->sc_locked = 0; 290 291 vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr, 292 &r128fb_accessops); 293 sc->vd.init_screen = r128fb_init_screen; 294 sc->vd.show_screen_cookie = &sc->sc_gc; 295 sc->vd.show_screen_cb = glyphcache_adapt; 296 297 /* init engine here */ 298 r128fb_init(sc); 299 300 ri = &sc->sc_console_screen.scr_ri; 301 302 sc->sc_gc.gc_bitblt = r128fb_bitblt; 303 sc->sc_gc.gc_rectfill = r128fb_rectfill_a; 304 sc->sc_gc.gc_blitcookie = sc; 305 sc->sc_gc.gc_rop = R128_ROP3_S; 306 if (is_console) { 307 vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1, 308 &defattr); 309 sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC; 310 311 r128fb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height, 312 ri->ri_devcmap[(defattr >> 16) & 0xff]); 313 sc->sc_defaultscreen_descr.textops = &ri->ri_ops; 314 sc->sc_defaultscreen_descr.capabilities = ri->ri_caps; 315 sc->sc_defaultscreen_descr.nrows = ri->ri_rows; 316 sc->sc_defaultscreen_descr.ncols = ri->ri_cols; 317 glyphcache_init(&sc->sc_gc, sc->sc_height + 5, 318 (sc->sc_cursor_offset / sc->sc_stride) - sc->sc_height - 5, 319 sc->sc_width, 320 ri->ri_font->fontwidth, 321 ri->ri_font->fontheight, 322 defattr); 323 wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0, 324 defattr); 325 vcons_replay_msgbuf(&sc->sc_console_screen); 326 } else { 327 /* 328 * since we're not the console we can postpone the rest 329 * until someone actually allocates a screen for us 330 */ 331 if (sc->sc_console_screen.scr_ri.ri_rows == 0) { 332 /* do some minimal setup to avoid weirdnesses later */ 333 vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1, 334 &defattr); 335 } else 336 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr); 337 glyphcache_init(&sc->sc_gc, sc->sc_height + 5, 338 (sc->sc_cursor_offset / sc->sc_stride) - sc->sc_height - 5, 339 sc->sc_width, 340 ri->ri_font->fontwidth, 341 ri->ri_font->fontheight, 342 defattr); 343 } 344 345 j = 0; 346 rasops_get_cmap(ri, cmap, sizeof(cmap)); 347 for (i = 0; i < 256; i++) { 348 sc->sc_cmap_red[i] = cmap[j]; 349 sc->sc_cmap_green[i] = cmap[j + 1]; 350 sc->sc_cmap_blue[i] = cmap[j + 2]; 351 r128fb_putpalreg(sc, i, cmap[j], cmap[j + 1], cmap[j + 2]); 352 j += 3; 353 } 354 355 /* no suspend/resume support yet */ 356 if (!pmf_device_register(sc->sc_dev, NULL, NULL)) 357 aprint_error_dev(sc->sc_dev, 358 "couldn't establish power handler\n"); 359 360 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL); 361 DPRINTF("R128_LVDS_GEN_CNTL: %08x\n", reg); 362 if (reg & R128_LVDS_ON) { 363 sc->sc_have_backlight = 1; 364 sc->sc_bl_on = 1; 365 sc->sc_bl_level = 255 - 366 ((reg & R128_LEVEL_MASK) >> R128_LEVEL_SHIFT); 367 pmf_event_register(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_UP, 368 r128fb_brightness_up, TRUE); 369 pmf_event_register(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_DOWN, 370 r128fb_brightness_down, TRUE); 371 aprint_verbose("%s: LVDS output is active, enabling backlight" 372 " control\n", device_xname(self)); 373 } else 374 sc->sc_have_backlight = 0; 375 376 aa.console = is_console; 377 aa.scrdata = &sc->sc_screenlist; 378 aa.accessops = &r128fb_accessops; 379 aa.accesscookie = &sc->vd; 380 381 config_found(sc->sc_dev, &aa, wsemuldisplaydevprint, CFARGS_NONE); 382 } 383 384 static int 385 r128fb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, 386 struct lwp *l) 387 { 388 struct vcons_data *vd = v; 389 struct r128fb_softc *sc = vd->cookie; 390 struct wsdisplay_fbinfo *wdf; 391 struct vcons_screen *ms = vd->active; 392 struct wsdisplay_param *param; 393 394 switch (cmd) { 395 case WSDISPLAYIO_GTYPE: 396 *(u_int *)data = WSDISPLAY_TYPE_PCIMISC; 397 return 0; 398 399 /* PCI config read/write passthrough. */ 400 case PCI_IOC_CFGREAD: 401 case PCI_IOC_CFGWRITE: 402 return pci_devioctl(sc->sc_pc, sc->sc_pcitag, 403 cmd, data, flag, l); 404 405 case WSDISPLAYIO_GET_BUSID: 406 return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc, 407 sc->sc_pcitag, data); 408 409 case WSDISPLAYIO_GINFO: 410 if (ms == NULL) 411 return ENODEV; 412 wdf = (void *)data; 413 wdf->height = ms->scr_ri.ri_height; 414 wdf->width = ms->scr_ri.ri_width; 415 wdf->depth = ms->scr_ri.ri_depth; 416 wdf->cmsize = 256; 417 return 0; 418 419 case WSDISPLAYIO_GETCMAP: 420 return r128fb_getcmap(sc, 421 (struct wsdisplay_cmap *)data); 422 423 case WSDISPLAYIO_PUTCMAP: 424 return r128fb_putcmap(sc, 425 (struct wsdisplay_cmap *)data); 426 427 case WSDISPLAYIO_LINEBYTES: 428 *(u_int *)data = sc->sc_stride; 429 return 0; 430 431 case WSDISPLAYIO_SMODE: { 432 int new_mode = *(int*)data; 433 if (new_mode != sc->sc_mode) { 434 sc->sc_mode = new_mode; 435 if(new_mode == WSDISPLAYIO_MODE_EMUL) { 436 r128fb_init(sc); 437 r128fb_restore_palette(sc); 438 glyphcache_wipe(&sc->sc_gc); 439 r128fb_rectfill(sc, 0, 0, sc->sc_width, 440 sc->sc_height, ms->scr_ri.ri_devcmap[ 441 (ms->scr_defattr >> 16) & 0xff]); 442 vcons_redraw_screen(ms); 443 } 444 } 445 } 446 return 0; 447 448 case WSDISPLAYIO_GETPARAM: 449 param = (struct wsdisplay_param *)data; 450 if (sc->sc_have_backlight == 0) 451 return EPASSTHROUGH; 452 switch (param->param) { 453 case WSDISPLAYIO_PARAM_BRIGHTNESS: 454 param->min = 0; 455 param->max = 255; 456 param->curval = sc->sc_bl_level; 457 return 0; 458 case WSDISPLAYIO_PARAM_BACKLIGHT: 459 param->min = 0; 460 param->max = 1; 461 param->curval = sc->sc_bl_on; 462 return 0; 463 } 464 return EPASSTHROUGH; 465 466 case WSDISPLAYIO_SETPARAM: 467 param = (struct wsdisplay_param *)data; 468 if (sc->sc_have_backlight == 0) 469 return EPASSTHROUGH; 470 switch (param->param) { 471 case WSDISPLAYIO_PARAM_BRIGHTNESS: 472 r128fb_set_backlight(sc, param->curval); 473 return 0; 474 case WSDISPLAYIO_PARAM_BACKLIGHT: 475 r128fb_switch_backlight(sc, param->curval); 476 return 0; 477 } 478 return EPASSTHROUGH; 479 480 case WSDISPLAYIO_GET_EDID: { 481 struct wsdisplayio_edid_info *d = data; 482 return wsdisplayio_get_edid(sc->sc_dev, d); 483 } 484 485 case WSDISPLAYIO_GET_FBINFO: 486 { 487 struct wsdisplayio_fbinfo *fbi = data; 488 489 return wsdisplayio_get_fbinfo(&ms->scr_ri, fbi); 490 } 491 492 case WSDISPLAYIO_GVIDEO: 493 *(unsigned *)data = sc->sc_video; 494 return 0; 495 496 case WSDISPLAYIO_SVIDEO: 497 r128fb_set_video(sc, *(unsigned int *)data); 498 return 0; 499 500 case WSDISPLAYIO_GCURMAX: 501 ((struct wsdisplay_curpos *)data)->x = 64; 502 ((struct wsdisplay_curpos *)data)->y = 64; 503 return 0; 504 505 case WSDISPLAYIO_SCURSOR: 506 return r128fb_set_cursor(sc, (struct wsdisplay_cursor *)data); 507 508 case WSDISPLAYIO_GCURSOR: 509 return EPASSTHROUGH; 510 511 case WSDISPLAYIO_GCURPOS: 512 ((struct wsdisplay_curpos *)data)->x = sc->sc_cursor_x; 513 ((struct wsdisplay_curpos *)data)->y = sc->sc_cursor_y; 514 return 0; 515 516 case WSDISPLAYIO_SCURPOS: 517 return r128fb_set_curpos(sc, (struct wsdisplay_curpos *)data); 518 } 519 520 return EPASSTHROUGH; 521 } 522 523 static paddr_t 524 r128fb_mmap(void *v, void *vs, off_t offset, int prot) 525 { 526 struct vcons_data *vd = v; 527 struct r128fb_softc *sc = vd->cookie; 528 paddr_t pa; 529 530 /* 'regular' framebuffer mmap()ing */ 531 if (offset < sc->sc_fbsize) { 532 pa = bus_space_mmap(sc->sc_memt, sc->sc_fb + offset, 0, prot, 533 BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE); 534 return pa; 535 } 536 537 /* 538 * restrict all other mappings to processes with superuser privileges 539 * or the kernel itself 540 */ 541 if (kauth_authorize_machdep(kauth_cred_get(), KAUTH_MACHDEP_UNMANAGEDMEM, 542 NULL, NULL, NULL, NULL) != 0) { 543 aprint_normal("%s: mmap() rejected.\n", 544 device_xname(sc->sc_dev)); 545 return -1; 546 } 547 548 if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) { 549 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot, 550 BUS_SPACE_MAP_LINEAR); 551 return pa; 552 } 553 554 if ((offset >= sc->sc_reg) && 555 (offset < (sc->sc_reg + sc->sc_regsize))) { 556 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot, 557 BUS_SPACE_MAP_LINEAR); 558 return pa; 559 } 560 561 #ifdef PCI_MAGIC_IO_RANGE 562 /* allow mapping of IO space */ 563 if ((offset >= PCI_MAGIC_IO_RANGE) && 564 (offset < PCI_MAGIC_IO_RANGE + 0x10000)) { 565 pa = bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE, 566 0, prot, BUS_SPACE_MAP_LINEAR); 567 return pa; 568 } 569 #endif 570 571 return -1; 572 } 573 574 static void 575 r128fb_init_screen(void *cookie, struct vcons_screen *scr, 576 int existing, long *defattr) 577 { 578 struct r128fb_softc *sc = cookie; 579 struct rasops_info *ri = &scr->scr_ri; 580 581 ri->ri_depth = sc->sc_depth; 582 ri->ri_width = sc->sc_width; 583 ri->ri_height = sc->sc_height; 584 ri->ri_stride = sc->sc_stride; 585 ri->ri_flg = RI_CENTER; 586 if (sc->sc_depth == 8) 587 ri->ri_flg |= RI_8BIT_IS_RGB | RI_ENABLE_ALPHA | 588 RI_PREFER_ALPHA; 589 590 rasops_init(ri, 0, 0); 591 ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE | 592 WSSCREEN_RESIZE; 593 #ifdef VCONS_DRAW_INTR 594 scr->scr_flags |= VCONS_DONT_READ; 595 #endif 596 scr->scr_flags |= VCONS_LOADFONT; 597 598 rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight, 599 sc->sc_width / ri->ri_font->fontwidth); 600 601 ri->ri_hw = scr; 602 ri->ri_ops.copyrows = r128fb_copyrows; 603 ri->ri_ops.copycols = r128fb_copycols; 604 ri->ri_ops.eraserows = r128fb_eraserows; 605 ri->ri_ops.erasecols = r128fb_erasecols; 606 ri->ri_ops.allocattr = r128fb_allocattr; 607 ri->ri_ops.cursor = r128fb_cursor; 608 if (FONT_IS_ALPHA(ri->ri_font)) { 609 ri->ri_ops.putchar = r128fb_putchar_aa; 610 } else 611 ri->ri_ops.putchar = r128fb_putchar; 612 } 613 614 static int 615 r128fb_putcmap(struct r128fb_softc *sc, struct wsdisplay_cmap *cm) 616 { 617 u_char *r, *g, *b; 618 u_int index = cm->index; 619 u_int count = cm->count; 620 int i, error; 621 u_char rbuf[256], gbuf[256], bbuf[256]; 622 623 #ifdef R128FB_DEBUG 624 aprint_debug("putcmap: %d %d\n",index, count); 625 #endif 626 if (cm->index >= 256 || cm->count > 256 || 627 (cm->index + cm->count) > 256) 628 return EINVAL; 629 error = copyin(cm->red, &rbuf[index], count); 630 if (error) 631 return error; 632 error = copyin(cm->green, &gbuf[index], count); 633 if (error) 634 return error; 635 error = copyin(cm->blue, &bbuf[index], count); 636 if (error) 637 return error; 638 639 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count); 640 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count); 641 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count); 642 643 r = &sc->sc_cmap_red[index]; 644 g = &sc->sc_cmap_green[index]; 645 b = &sc->sc_cmap_blue[index]; 646 647 for (i = 0; i < count; i++) { 648 r128fb_putpalreg(sc, index, *r, *g, *b); 649 index++; 650 r++, g++, b++; 651 } 652 return 0; 653 } 654 655 static int 656 r128fb_getcmap(struct r128fb_softc *sc, struct wsdisplay_cmap *cm) 657 { 658 u_int index = cm->index; 659 u_int count = cm->count; 660 int error; 661 662 if (index >= 255 || count > 256 || index + count > 256) 663 return EINVAL; 664 665 error = copyout(&sc->sc_cmap_red[index], cm->red, count); 666 if (error) 667 return error; 668 error = copyout(&sc->sc_cmap_green[index], cm->green, count); 669 if (error) 670 return error; 671 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count); 672 if (error) 673 return error; 674 675 return 0; 676 } 677 678 static void 679 r128fb_restore_palette(struct r128fb_softc *sc) 680 { 681 int i; 682 683 for (i = 0; i < (1 << sc->sc_depth); i++) { 684 r128fb_putpalreg(sc, i, sc->sc_cmap_red[i], 685 sc->sc_cmap_green[i], sc->sc_cmap_blue[i]); 686 } 687 } 688 689 static int 690 r128fb_putpalreg(struct r128fb_softc *sc, uint8_t idx, uint8_t r, uint8_t g, 691 uint8_t b) 692 { 693 uint32_t reg; 694 695 /* whack the DAC */ 696 reg = (r << 16) | (g << 8) | b; 697 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PALETTE_INDEX, idx); 698 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PALETTE_DATA, reg); 699 return 0; 700 } 701 702 static void 703 r128fb_init(struct r128fb_softc *sc) 704 { 705 uint32_t datatype, d, reg; 706 int i; 707 708 r128fb_flush_engine(sc); 709 710 r128fb_wait(sc, 9); 711 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_CRTC_OFFSET, 0); 712 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DEFAULT_OFFSET, 0); 713 /* pitch is in units of 8 pixels */ 714 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DEFAULT_PITCH, 715 sc->sc_width >> 3); 716 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_AUX_SC_CNTL, 0); 717 bus_space_write_4(sc->sc_memt, sc->sc_regh, 718 R128_DEFAULT_SC_BOTTOM_RIGHT, 719 R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX); 720 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SC_TOP_LEFT, 0); 721 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SC_BOTTOM_RIGHT, 722 R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX); 723 bus_space_write_4(sc->sc_memt, sc->sc_regh, 724 R128_DEFAULT_SC_BOTTOM_RIGHT, 725 R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX); 726 727 #if 0 728 #if BYTE_ORDER == BIG_ENDIAN 729 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_DATATYPE, 730 R128_HOST_BIG_ENDIAN_EN); 731 #else 732 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_DATATYPE, 0); 733 #endif 734 #endif 735 r128fb_wait(sc, 7); 736 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_PITCH, 737 sc->sc_width >> 3); 738 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_PITCH, 739 sc->sc_width >> 3); 740 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_OFFSET, 0); 741 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_OFFSET, 0); 742 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_WRITE_MASK, 743 0xffffffff); 744 745 switch (sc->sc_depth) { 746 case 8: 747 datatype = R128_GMC_DST_8BPP_CI; 748 d = R128_CRTC_COLOR_8BIT; 749 break; 750 case 15: 751 datatype = R128_GMC_DST_15BPP; 752 d = R128_CRTC_COLOR_15BIT; 753 break; 754 case 16: 755 datatype = R128_GMC_DST_16BPP; 756 d = R128_CRTC_COLOR_16BIT; 757 break; 758 case 24: 759 datatype = R128_GMC_DST_24BPP; 760 d = R128_CRTC_COLOR_24BIT; 761 break; 762 case 32: 763 datatype = R128_GMC_DST_32BPP; 764 d = R128_CRTC_COLOR_32BIT; 765 break; 766 default: 767 aprint_error("%s: unsupported depth %d\n", 768 device_xname(sc->sc_dev), sc->sc_depth); 769 return; 770 } 771 sc->sc_master_cntl = R128_GMC_CLR_CMP_CNTL_DIS | 772 R128_GMC_AUX_CLIP_DIS | datatype; 773 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_CRTC_GEN_CNTL); 774 DPRINTF("depth: %d\n", reg & R128_CRTC_PIX_WIDTH); 775 reg &= ~R128_CRTC_PIX_WIDTH; 776 reg |= d; 777 reg &= ~R128_CRTC_CUR_MODE_MASK; 778 #ifdef R128FB_DEBUG 779 reg |= R128_CRTC_CUR_EN; 780 #endif 781 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_CRTC_GEN_CNTL, reg); 782 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_CRTC_PITCH, sc->sc_width >> 3); 783 r128fb_flush_engine(sc); 784 785 /* setup cursor */ 786 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_CUR_OFFSET, sc->sc_cursor_offset); 787 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_CUR_CLR0, 0xff0000); 788 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_CUR_CLR1, 0xffff00); 789 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_CUR_HORZ_VERT_POSN, 0x00100010); 790 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_CUR_HORZ_VERT_OFF, 0); 791 for (i = 0; i < 64; i++) { 792 sc->sc_cursor[i] = 0; 793 sc->sc_cursor[i + 64] = 0x55555555; 794 sc->sc_cursor[i + 128] = 0x99999999; 795 sc->sc_cursor[i + 192] = 0xffffffff; 796 } 797 sc->sc_cursor_x = 0; 798 sc->sc_cursor_y = 0; 799 sc->sc_hot_x = 0; 800 sc->sc_hot_y = 0; 801 } 802 803 static void 804 r128fb_rectfill(struct r128fb_softc *sc, int x, int y, int wi, int he, 805 uint32_t colour) 806 { 807 808 r128fb_wait(sc, 5); 809 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_GUI_MASTER_CNTL, 810 R128_GMC_BRUSH_SOLID_COLOR | 811 R128_GMC_SRC_DATATYPE_COLOR | 812 R128_ROP3_P | 813 sc->sc_master_cntl); 814 815 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_BRUSH_FRGD_CLR, 816 colour); 817 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_CNTL, 818 R128_DST_X_LEFT_TO_RIGHT | R128_DST_Y_TOP_TO_BOTTOM); 819 /* now feed it coordinates */ 820 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y, 821 (x << 16) | y); 822 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_WIDTH_HEIGHT, 823 (wi << 16) | he); 824 } 825 826 static void 827 r128fb_rectfill_a(void *cookie, int dstx, int dsty, 828 int width, int height, long attr) 829 { 830 struct r128fb_softc *sc = cookie; 831 832 r128fb_rectfill(sc, dstx, dsty, width, height, 833 sc->vd.active->scr_ri.ri_devcmap[(attr >> 24 & 0xf)]); 834 } 835 836 static void 837 r128fb_bitblt(void *cookie, int xs, int ys, int xd, int yd, 838 int wi, int he, int rop) 839 { 840 struct r128fb_softc *sc = cookie; 841 uint32_t dp_cntl = 0; 842 843 r128fb_wait(sc, 5); 844 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_GUI_MASTER_CNTL, 845 R128_GMC_BRUSH_SOLID_COLOR | 846 R128_GMC_SRC_DATATYPE_COLOR | 847 rop | 848 R128_DP_SRC_SOURCE_MEMORY | 849 sc->sc_master_cntl); 850 851 if (yd <= ys) { 852 dp_cntl = R128_DST_Y_TOP_TO_BOTTOM; 853 } else { 854 ys += he - 1; 855 yd += he - 1; 856 } 857 if (xd <= xs) { 858 dp_cntl |= R128_DST_X_LEFT_TO_RIGHT; 859 } else { 860 xs += wi - 1; 861 xd += wi - 1; 862 } 863 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_CNTL, dp_cntl); 864 865 /* now feed it coordinates */ 866 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_X_Y, 867 (xs << 16) | ys); 868 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y, 869 (xd << 16) | yd); 870 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_WIDTH_HEIGHT, 871 (wi << 16) | he); 872 } 873 874 static void 875 r128fb_cursor(void *cookie, int on, int row, int col) 876 { 877 struct rasops_info *ri = cookie; 878 struct vcons_screen *scr = ri->ri_hw; 879 struct r128fb_softc *sc = scr->scr_cookie; 880 int x, y, wi, he; 881 882 wi = ri->ri_font->fontwidth; 883 he = ri->ri_font->fontheight; 884 885 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) { 886 if (ri->ri_flg & RI_CURSOR) { 887 x = ri->ri_ccol * wi + ri->ri_xorigin; 888 y = ri->ri_crow * he + ri->ri_yorigin; 889 r128fb_bitblt(sc, x, y, x, y, wi, he, R128_ROP3_Dn); 890 ri->ri_flg &= ~RI_CURSOR; 891 } 892 ri->ri_crow = row; 893 ri->ri_ccol = col; 894 if (on) { 895 x = ri->ri_ccol * wi + ri->ri_xorigin; 896 y = ri->ri_crow * he + ri->ri_yorigin; 897 r128fb_bitblt(sc, x, y, x, y, wi, he, R128_ROP3_Dn); 898 ri->ri_flg |= RI_CURSOR; 899 } 900 } else { 901 scr->scr_ri.ri_crow = row; 902 scr->scr_ri.ri_ccol = col; 903 scr->scr_ri.ri_flg &= ~RI_CURSOR; 904 } 905 906 } 907 908 static int 909 r128fb_allocattr(void *cookie, int fg0, int bg0, int flg, long *attr) 910 { 911 struct rasops_info *ri = cookie; 912 int fg = fg0, bg = bg0; 913 914 if ((flg & WSATTR_BLINK) != 0) 915 return EINVAL; 916 917 if ((flg & WSATTR_REVERSE) != 0) { 918 fg = bg0; 919 bg = fg0; 920 } 921 922 if (FONT_IS_ALPHA(ri->ri_font) && ((flg & WSATTR_HILIT) != 0)) { 923 fg = fg0 < 8 ? fg0 + 8 : fg0; 924 } 925 926 *attr = (bg << 16) | (fg << 24) | flg; 927 return 0; 928 } 929 930 static void 931 r128fb_putchar(void *cookie, int row, int col, u_int c, long attr) 932 { 933 struct rasops_info *ri = cookie; 934 struct wsdisplay_font *font = PICK_FONT(ri, c); 935 struct vcons_screen *scr = ri->ri_hw; 936 struct r128fb_softc *sc = scr->scr_cookie; 937 void *data; 938 uint32_t fg, bg; 939 int i, x, y, wi, he, offset; 940 941 if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL) 942 return; 943 944 if (!CHAR_IN_FONT(c, font)) 945 return; 946 947 wi = font->fontwidth; 948 he = font->fontheight; 949 950 bg = ri->ri_devcmap[(attr >> 16) & 0xf]; 951 fg = ri->ri_devcmap[(attr >> 24) & 0xf]; 952 x = ri->ri_xorigin + col * wi; 953 y = ri->ri_yorigin + row * he; 954 955 if (c == 0x20) { 956 r128fb_rectfill(sc, x, y, wi, he, bg); 957 goto out; 958 } 959 960 data = WSFONT_GLYPH(c, font); 961 962 r128fb_wait(sc, 8); 963 964 bus_space_write_4(sc->sc_memt, sc->sc_regh, 965 R128_DP_GUI_MASTER_CNTL, 966 R128_GMC_BRUSH_SOLID_COLOR | 967 R128_GMC_SRC_DATATYPE_MONO_FG_BG | 968 R128_ROP3_S | 969 R128_DP_SRC_SOURCE_HOST_DATA | 970 R128_GMC_DST_CLIPPING | 971 sc->sc_master_cntl); 972 973 bus_space_write_4(sc->sc_memt, sc->sc_regh, 974 R128_DP_CNTL, 975 R128_DST_Y_TOP_TO_BOTTOM | 976 R128_DST_X_LEFT_TO_RIGHT); 977 978 bus_space_write_4(sc->sc_memt, sc->sc_regh, 979 R128_DP_SRC_FRGD_CLR, fg); 980 bus_space_write_4(sc->sc_memt, sc->sc_regh, 981 R128_DP_SRC_BKGD_CLR, bg); 982 983 /* 984 * The Rage 128 doesn't have anything to skip pixels 985 * when colour expanding but all coordinates 986 * are signed so we just clip the leading bytes and 987 * trailing bits away 988 */ 989 bus_space_write_4(sc->sc_memt, sc->sc_regh, 990 R128_SC_RIGHT, x + wi - 1); 991 bus_space_write_4(sc->sc_memt, sc->sc_regh, 992 R128_SC_LEFT, x); 993 994 /* needed? */ 995 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_X_Y, 0); 996 997 offset = 32 - (font->stride << 3); 998 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y, 999 ((x - offset) << 16) | y); 1000 bus_space_write_4(sc->sc_memt, sc->sc_regh, 1001 R128_DST_WIDTH_HEIGHT, (32 << 16) | he); 1002 1003 r128fb_wait(sc, he); 1004 if (attr & WSATTR_HILIT) { 1005 switch (font->stride) { 1006 case 1: { 1007 uint8_t *data8 = data; 1008 uint32_t reg; 1009 for (i = 0; i < he; i++) { 1010 reg = *data8; 1011 reg |= reg >> 1; 1012 bus_space_write_stream_4(sc->sc_memt, 1013 sc->sc_regh, R128_HOST_DATA0, reg); 1014 data8++; 1015 } 1016 break; 1017 } 1018 case 2: { 1019 uint16_t *data16 = data; 1020 uint32_t reg; 1021 for (i = 0; i < he; i++) { 1022 reg = *data16; 1023 reg |= reg >> 1; 1024 bus_space_write_stream_4(sc->sc_memt, 1025 sc->sc_regh, R128_HOST_DATA0, reg); 1026 data16++; 1027 } 1028 break; 1029 } 1030 } 1031 } else { 1032 switch (font->stride) { 1033 case 1: { 1034 uint8_t *data8 = data; 1035 uint32_t reg; 1036 for (i = 0; i < he; i++) { 1037 reg = *data8; 1038 bus_space_write_stream_4(sc->sc_memt, 1039 sc->sc_regh, R128_HOST_DATA0, reg); 1040 data8++; 1041 } 1042 break; 1043 } 1044 case 2: { 1045 uint16_t *data16 = data; 1046 uint32_t reg; 1047 for (i = 0; i < he; i++) { 1048 reg = *data16; 1049 bus_space_write_stream_4(sc->sc_memt, 1050 sc->sc_regh, R128_HOST_DATA0, reg); 1051 data16++; 1052 } 1053 break; 1054 } 1055 } 1056 } 1057 out: 1058 if (attr & WSATTR_UNDERLINE) 1059 r128fb_rectfill(sc, x, y + he - 2, wi, 1, fg); 1060 1061 } 1062 1063 static void 1064 r128fb_putchar_aa(void *cookie, int row, int col, u_int c, long attr) 1065 { 1066 struct rasops_info *ri = cookie; 1067 struct wsdisplay_font *font = PICK_FONT(ri, c); 1068 struct vcons_screen *scr = ri->ri_hw; 1069 struct r128fb_softc *sc = scr->scr_cookie; 1070 uint32_t bg, latch = 0, bg8, fg8, pixel; 1071 int i, x, y, wi, he, r, g, b, aval; 1072 int r1, g1, b1, r0, g0, b0, fgo, bgo; 1073 uint8_t *data8; 1074 int rv, cnt = 0; 1075 1076 if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL) 1077 return; 1078 1079 if (!CHAR_IN_FONT(c, font)) 1080 return; 1081 1082 wi = font->fontwidth; 1083 he = font->fontheight; 1084 1085 bg = ri->ri_devcmap[(attr >> 16) & 0xf]; 1086 x = ri->ri_xorigin + col * wi; 1087 y = ri->ri_yorigin + row * he; 1088 if (c == 0x20) { 1089 r128fb_rectfill(sc, x, y, wi, he, bg); 1090 goto out; 1091 } 1092 1093 rv = glyphcache_try(&sc->sc_gc, c, x, y, attr); 1094 if (rv == GC_OK) 1095 return; 1096 1097 data8 = WSFONT_GLYPH(c, font); 1098 1099 r128fb_wait(sc, 5); 1100 bus_space_write_4(sc->sc_memt, sc->sc_regh, 1101 R128_DP_GUI_MASTER_CNTL, 1102 R128_GMC_BRUSH_SOLID_COLOR | 1103 R128_GMC_SRC_DATATYPE_COLOR | 1104 R128_ROP3_S | 1105 R128_DP_SRC_SOURCE_HOST_DATA | 1106 sc->sc_master_cntl); 1107 1108 bus_space_write_4(sc->sc_memt, sc->sc_regh, 1109 R128_DP_CNTL, 1110 R128_DST_Y_TOP_TO_BOTTOM | 1111 R128_DST_X_LEFT_TO_RIGHT); 1112 1113 /* needed? */ 1114 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_X_Y, 0); 1115 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y, 1116 (x << 16) | y); 1117 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_WIDTH_HEIGHT, 1118 (wi << 16) | he); 1119 1120 /* 1121 * we need the RGB colours here, so get offsets into rasops_cmap 1122 */ 1123 fgo = ((attr >> 24) & 0xf) * 3; 1124 bgo = ((attr >> 16) & 0xf) * 3; 1125 1126 r0 = rasops_cmap[bgo]; 1127 r1 = rasops_cmap[fgo]; 1128 g0 = rasops_cmap[bgo + 1]; 1129 g1 = rasops_cmap[fgo + 1]; 1130 b0 = rasops_cmap[bgo + 2]; 1131 b1 = rasops_cmap[fgo + 2]; 1132 #define R3G3B2(r, g, b) ((r & 0xe0) | ((g >> 3) & 0x1c) | (b >> 6)) 1133 bg8 = R3G3B2(r0, g0, b0); 1134 fg8 = R3G3B2(r1, g1, b1); 1135 1136 r128fb_wait(sc, 16); 1137 1138 for (i = 0; i < ri->ri_fontscale; i++) { 1139 aval = *data8; 1140 if (aval == 0) { 1141 pixel = bg8; 1142 } else if (aval == 255) { 1143 pixel = fg8; 1144 } else { 1145 r = aval * r1 + (255 - aval) * r0; 1146 g = aval * g1 + (255 - aval) * g0; 1147 b = aval * b1 + (255 - aval) * b0; 1148 pixel = ((r & 0xe000) >> 8) | 1149 ((g & 0xe000) >> 11) | 1150 ((b & 0xc000) >> 14); 1151 } 1152 latch = (latch << 8) | pixel; 1153 /* write in 32bit chunks */ 1154 if ((i & 3) == 3) { 1155 bus_space_write_stream_4(sc->sc_memt, sc->sc_regh, 1156 R128_HOST_DATA0, latch); 1157 /* 1158 * not strictly necessary, old data should be shifted 1159 * out 1160 */ 1161 latch = 0; 1162 cnt++; 1163 if (cnt > 15) { 1164 r128fb_wait(sc, 16); 1165 cnt = 0; 1166 } 1167 } 1168 data8++; 1169 } 1170 /* if we have pixels left in latch write them out */ 1171 if ((i & 3) != 0) { 1172 latch = latch << ((4 - (i & 3)) << 3); 1173 bus_space_write_stream_4(sc->sc_memt, sc->sc_regh, 1174 R128_HOST_DATA0, latch); 1175 } 1176 if (rv == GC_ADD) { 1177 glyphcache_add(&sc->sc_gc, c, x, y); 1178 } 1179 out: 1180 if (attr & WSATTR_UNDERLINE) 1181 glyphcache_underline(&sc->sc_gc, x, y, attr); 1182 } 1183 1184 static void 1185 r128fb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols) 1186 { 1187 struct rasops_info *ri = cookie; 1188 struct vcons_screen *scr = ri->ri_hw; 1189 struct r128fb_softc *sc = scr->scr_cookie; 1190 int32_t xs, xd, y, width, height; 1191 1192 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) { 1193 xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol; 1194 xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol; 1195 y = ri->ri_yorigin + ri->ri_font->fontheight * row; 1196 width = ri->ri_font->fontwidth * ncols; 1197 height = ri->ri_font->fontheight; 1198 r128fb_bitblt(sc, xs, y, xd, y, width, height, R128_ROP3_S); 1199 } 1200 } 1201 1202 static void 1203 r128fb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr) 1204 { 1205 struct rasops_info *ri = cookie; 1206 struct vcons_screen *scr = ri->ri_hw; 1207 struct r128fb_softc *sc = scr->scr_cookie; 1208 int32_t x, y, width, height, fg, bg, ul; 1209 1210 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) { 1211 x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol; 1212 y = ri->ri_yorigin + ri->ri_font->fontheight * row; 1213 width = ri->ri_font->fontwidth * ncols; 1214 height = ri->ri_font->fontheight; 1215 rasops_unpack_attr(fillattr, &fg, &bg, &ul); 1216 1217 r128fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]); 1218 } 1219 } 1220 1221 static void 1222 r128fb_copyrows(void *cookie, int srcrow, int dstrow, int nrows) 1223 { 1224 struct rasops_info *ri = cookie; 1225 struct vcons_screen *scr = ri->ri_hw; 1226 struct r128fb_softc *sc = scr->scr_cookie; 1227 int32_t x, ys, yd, width, height; 1228 1229 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) { 1230 x = ri->ri_xorigin; 1231 ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow; 1232 yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow; 1233 width = ri->ri_emuwidth; 1234 height = ri->ri_font->fontheight * nrows; 1235 r128fb_bitblt(sc, x, ys, x, yd, width, height, R128_ROP3_S); 1236 } 1237 } 1238 1239 static void 1240 r128fb_eraserows(void *cookie, int row, int nrows, long fillattr) 1241 { 1242 struct rasops_info *ri = cookie; 1243 struct vcons_screen *scr = ri->ri_hw; 1244 struct r128fb_softc *sc = scr->scr_cookie; 1245 int32_t x, y, width, height, fg, bg, ul; 1246 1247 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) { 1248 x = ri->ri_xorigin; 1249 y = ri->ri_yorigin + ri->ri_font->fontheight * row; 1250 width = ri->ri_emuwidth; 1251 height = ri->ri_font->fontheight * nrows; 1252 rasops_unpack_attr(fillattr, &fg, &bg, &ul); 1253 1254 r128fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]); 1255 } 1256 } 1257 1258 1259 static void 1260 r128fb_set_video(struct r128fb_softc *sc, int on) 1261 { 1262 uint32_t reg; 1263 uint32_t mask = R128_CRTC_HSYNC_DIS | R128_CRTC_VSYNC_DIS | R128_CRTC_DISPLAY_DIS; 1264 1265 if (sc->sc_video == on) return; 1266 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_CRTC_EXT_CNTL); 1267 if (on == WSDISPLAYIO_VIDEO_OFF) { 1268 sc->sc_video = on; 1269 reg |= mask; 1270 } else { 1271 sc->sc_video = WSDISPLAYIO_VIDEO_ON; 1272 reg &= ~mask; 1273 } 1274 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_CRTC_EXT_CNTL, reg); 1275 if (sc->sc_have_backlight) 1276 r128fb_switch_backlight(sc, on); 1277 } 1278 1279 static void 1280 r128fb_set_backlight(struct r128fb_softc *sc, int level) 1281 { 1282 uint32_t reg; 1283 1284 /* 1285 * should we do nothing when backlight is off, should we just store the 1286 * level and use it when turning back on or should we just flip sc_bl_on 1287 * and turn the backlight on? 1288 * For now turn it on so a crashed screensaver can't get the user stuck 1289 * with a dark screen as long as hotkeys work 1290 */ 1291 if (level > 255) level = 255; 1292 if (level < 0) level = 0; 1293 if (level == sc->sc_bl_level) 1294 return; 1295 sc->sc_bl_level = level; 1296 if (sc->sc_bl_on == 0) 1297 sc->sc_bl_on = 1; 1298 level = 255 - level; 1299 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL); 1300 reg &= ~R128_LEVEL_MASK; 1301 reg |= (level << R128_LEVEL_SHIFT); 1302 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL, reg); 1303 DPRINTF("backlight level: %d reg %08x\n", level, reg); 1304 } 1305 1306 static void 1307 r128fb_switch_backlight(struct r128fb_softc *sc, int on) 1308 { 1309 uint32_t reg; 1310 int level; 1311 1312 if (on == sc->sc_bl_on) 1313 return; 1314 sc->sc_bl_on = on; 1315 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL); 1316 reg &= ~R128_LEVEL_MASK; 1317 level = on ? 255 - sc->sc_bl_level : 255; 1318 reg |= level << R128_LEVEL_SHIFT; 1319 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL, reg); 1320 DPRINTF("backlight state: %d reg %08x\n", on, reg); 1321 } 1322 1323 static void 1324 r128fb_brightness_up(device_t dev) 1325 { 1326 struct r128fb_softc *sc = device_private(dev); 1327 1328 r128fb_set_backlight(sc, sc->sc_bl_level + 8); 1329 } 1330 1331 static void 1332 r128fb_brightness_down(device_t dev) 1333 { 1334 struct r128fb_softc *sc = device_private(dev); 1335 1336 r128fb_set_backlight(sc, sc->sc_bl_level - 8); 1337 } 1338 1339 static uint32_t 1340 bitflip(uint32_t d) 1341 { 1342 uint32_t res; 1343 1344 res = ((d & 0x01010101) << 7) | 1345 ((d & 0x02020202) << 5) | 1346 ((d & 0x04040404) << 3) | 1347 ((d & 0x08080808) << 1) | 1348 ((d & 0x10101010) >> 1) | 1349 ((d & 0x20202020) >> 3) | 1350 ((d & 0x40404040) >> 5) | 1351 ((d & 0x80808080) >> 7); 1352 return res; 1353 } 1354 1355 static int 1356 r128fb_set_cursor(struct r128fb_softc *sc, struct wsdisplay_cursor *wc) 1357 { 1358 unsigned flags; 1359 1360 uint8_t r[2], g[2], b[2]; 1361 unsigned index, count; 1362 int i, err; 1363 uint32_t clr, image[128], mask[128]; 1364 int redo_shape = 0; 1365 1366 flags = wc->which; 1367 1368 if (flags & WSDISPLAY_CURSOR_DOCMAP) { 1369 index = wc->cmap.index; 1370 count = wc->cmap.count; 1371 1372 if (index >= 2 || count > 2 - index) 1373 return EINVAL; 1374 1375 err = copyin(wc->cmap.red, &r[index], count); 1376 if (err) 1377 return err; 1378 err = copyin(wc->cmap.green, &g[index], count); 1379 if (err) 1380 return err; 1381 err = copyin(wc->cmap.blue, &b[index], count); 1382 if (err) 1383 return err; 1384 1385 clr = r[0] << 16 | g[0] << 8 | b[0]; 1386 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_CUR_CLR0, clr); 1387 clr = r[1] << 16 | g[1] << 8 | b[1]; 1388 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_CUR_CLR1, clr); 1389 } 1390 1391 if (flags & WSDISPLAY_CURSOR_DOSHAPE) { 1392 if ((wc->size.x > 64) || 1393 (wc->size.y > 64)) 1394 return EINVAL; 1395 1396 if ((err = copyin(wc->image, image, 512)) != 0) 1397 return err; 1398 1399 if ((err = copyin(wc->mask, mask, 512)) != 0) 1400 return err; 1401 redo_shape = 1; 1402 } 1403 1404 if (redo_shape) { 1405 /* 1406 * the cursor sprite is stored in video memory as a 64x64 1407 * bitmap with mask and image line-interleaved, and mask 1408 * inverted 1409 */ 1410 uint32_t *csr = sc->sc_cursor; 1411 for (i = 0; i < 128; i += 2) { 1412 *csr = bitflip(~mask[i]); 1413 csr++; 1414 *csr = bitflip(~mask[i + 1]); 1415 csr++; 1416 *csr = bitflip(image[i]); 1417 csr++; 1418 *csr = bitflip(image[i + 1]); 1419 csr++; 1420 } 1421 } 1422 1423 if (flags & WSDISPLAY_CURSOR_DOHOT) { 1424 sc->sc_hot_x = wc->hot.x; 1425 sc->sc_hot_y = wc->hot.y; 1426 flags |= WSDISPLAY_CURSOR_DOPOS; 1427 } 1428 1429 if (flags & WSDISPLAY_CURSOR_DOPOS) { 1430 r128fb_set_curpos(sc, &wc->pos); 1431 } 1432 if (flags & WSDISPLAY_CURSOR_DOCUR) { 1433 uint32_t reg; 1434 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, 1435 R128_CRTC_GEN_CNTL); 1436 reg = wc->enable == 0 ? 1437 reg & ~R128_CRTC_CUR_EN : reg | R128_CRTC_CUR_EN; 1438 bus_space_write_4(sc->sc_memt, sc->sc_regh, 1439 R128_CRTC_GEN_CNTL, reg); 1440 } 1441 return 0; 1442 } 1443 1444 static int 1445 r128fb_set_curpos(struct r128fb_softc *sc, struct wsdisplay_curpos *p) 1446 { 1447 uint32_t xoff = 0, yoff = 0, coff; 1448 int x, y; 1449 1450 sc->sc_cursor_x = p->x; 1451 x = p->x - sc->sc_hot_x; 1452 sc->sc_cursor_y = p->y; 1453 y = p->y - sc->sc_hot_y; 1454 if (x < 0) { 1455 xoff = -x; 1456 x = 0; 1457 } 1458 if (y < 0) { 1459 yoff = -y; 1460 y = 0; 1461 coff = sc->sc_cursor_offset + (16 * yoff); 1462 } else 1463 coff = sc->sc_cursor_offset; 1464 1465 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_CUR_HORZ_VERT_OFF, 1466 (yoff & 0xffff) | (xoff << 16) | R128_CUR_LOCK); 1467 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_CUR_HORZ_VERT_POSN, 1468 (y & 0xffff) | (x << 16) | R128_CUR_LOCK); 1469 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_CUR_OFFSET, coff); 1470 return 0; 1471 } 1472