1 /* $NetBSD: r128fb.c,v 1.22 2011/06/29 03:14:36 macallan Exp $ */ 2 3 /* 4 * Copyright (c) 2007 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.22 2011/06/29 03:14:36 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/malloc.h> 41 #include <sys/lwp.h> 42 #include <sys/kauth.h> 43 44 #include <dev/videomode/videomode.h> 45 46 #include <dev/pci/pcivar.h> 47 #include <dev/pci/pcireg.h> 48 #include <dev/pci/pcidevs.h> 49 #include <dev/pci/pciio.h> 50 #include <dev/pci/r128fbreg.h> 51 52 #include <dev/wscons/wsdisplayvar.h> 53 #include <dev/wscons/wsconsio.h> 54 #include <dev/wsfont/wsfont.h> 55 #include <dev/rasops/rasops.h> 56 #include <dev/wscons/wsdisplay_vconsvar.h> 57 #include <dev/pci/wsdisplay_pci.h> 58 59 #include <dev/i2c/i2cvar.h> 60 61 #include "opt_r128fb.h" 62 #include "opt_vcons.h" 63 64 #ifdef R128FB_DEBUG 65 #define DPRINTF printf 66 #else 67 #define DPRINTF while(0) printf 68 #endif 69 70 struct r128fb_softc { 71 device_t sc_dev; 72 73 pci_chipset_tag_t sc_pc; 74 pcitag_t sc_pcitag; 75 76 bus_space_tag_t sc_memt; 77 bus_space_tag_t sc_iot; 78 79 bus_space_handle_t sc_regh; 80 bus_addr_t sc_fb, sc_reg; 81 bus_size_t sc_fbsize, sc_regsize; 82 83 int sc_width, sc_height, sc_depth, sc_stride; 84 int sc_locked, sc_have_backlight, sc_bl_level, sc_bl_on; 85 struct vcons_screen sc_console_screen; 86 struct wsscreen_descr sc_defaultscreen_descr; 87 const struct wsscreen_descr *sc_screens[1]; 88 struct wsscreen_list sc_screenlist; 89 struct vcons_data vd; 90 int sc_mode; 91 u_char sc_cmap_red[256]; 92 u_char sc_cmap_green[256]; 93 u_char sc_cmap_blue[256]; 94 /* engine stuff */ 95 uint32_t sc_master_cntl; 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 extern const u_char rasops_cmap[768]; 105 106 static int r128fb_ioctl(void *, void *, u_long, void *, int, 107 struct lwp *); 108 static paddr_t r128fb_mmap(void *, void *, off_t, int); 109 static void r128fb_init_screen(void *, struct vcons_screen *, int, long *); 110 111 static int r128fb_putcmap(struct r128fb_softc *, struct wsdisplay_cmap *); 112 static int r128fb_getcmap(struct r128fb_softc *, struct wsdisplay_cmap *); 113 static void r128fb_restore_palette(struct r128fb_softc *); 114 static int r128fb_putpalreg(struct r128fb_softc *, uint8_t, uint8_t, 115 uint8_t, uint8_t); 116 117 static void r128fb_init(struct r128fb_softc *); 118 static void r128fb_flush_engine(struct r128fb_softc *); 119 static void r128fb_rectfill(struct r128fb_softc *, int, int, int, int, 120 uint32_t); 121 static void r128fb_bitblt(struct r128fb_softc *, int, int, int, int, int, 122 int, int); 123 124 static void r128fb_cursor(void *, int, int, int); 125 static void r128fb_putchar(void *, int, int, u_int, long); 126 static void r128fb_copycols(void *, int, int, int, int); 127 static void r128fb_erasecols(void *, int, int, int, long); 128 static void r128fb_copyrows(void *, int, int, int); 129 static void r128fb_eraserows(void *, int, int, long); 130 131 static void r128fb_brightness_up(device_t); 132 static void r128fb_brightness_down(device_t); 133 /* set backlight level */ 134 static void r128fb_set_backlight(struct r128fb_softc *, int); 135 /* turn backlight on and off without messing with the level */ 136 static void r128fb_switch_backlight(struct r128fb_softc *, int); 137 138 struct wsdisplay_accessops r128fb_accessops = { 139 r128fb_ioctl, 140 r128fb_mmap, 141 NULL, /* alloc_screen */ 142 NULL, /* free_screen */ 143 NULL, /* show_screen */ 144 NULL, /* load_font */ 145 NULL, /* pollc */ 146 NULL /* scroll */ 147 }; 148 149 static inline void 150 r128fb_wait(struct r128fb_softc *sc, int slots) 151 { 152 uint32_t reg; 153 154 do { 155 reg = (bus_space_read_4(sc->sc_memt, sc->sc_regh, 156 R128_GUI_STAT) & R128_GUI_FIFOCNT_MASK); 157 } while (reg <= slots); 158 } 159 160 static void 161 r128fb_flush_engine(struct r128fb_softc *sc) 162 { 163 uint32_t reg; 164 165 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_PC_NGUI_CTLSTAT); 166 reg |= R128_PC_FLUSH_ALL; 167 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PC_NGUI_CTLSTAT, reg); 168 do { 169 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, 170 R128_PC_NGUI_CTLSTAT); 171 } while (reg & R128_PC_BUSY); 172 } 173 174 static int 175 r128fb_match(device_t parent, cfdata_t match, void *aux) 176 { 177 struct pci_attach_args *pa = (struct pci_attach_args *)aux; 178 179 if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY) 180 return 0; 181 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_ATI) 182 return 0; 183 184 /* only cards tested on so far - likely need a list */ 185 if ((PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGE1AGP4XT) || 186 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGE3AGP4XT) || 187 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGEGLPCI) || 188 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGE_MOB_M3_AGP)) 189 return 100; 190 return (0); 191 } 192 193 static void 194 r128fb_attach(device_t parent, device_t self, void *aux) 195 { 196 struct r128fb_softc *sc = device_private(self); 197 struct pci_attach_args *pa = aux; 198 struct rasops_info *ri; 199 bus_space_tag_t tag; 200 char devinfo[256]; 201 struct wsemuldisplaydev_attach_args aa; 202 prop_dictionary_t dict; 203 unsigned long defattr; 204 bool is_console; 205 int i, j; 206 uint32_t reg, flags; 207 208 sc->sc_pc = pa->pa_pc; 209 sc->sc_pcitag = pa->pa_tag; 210 sc->sc_memt = pa->pa_memt; 211 sc->sc_iot = pa->pa_iot; 212 sc->sc_dev = self; 213 214 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo)); 215 aprint_normal(": %s\n", devinfo); 216 217 /* fill in parameters from properties */ 218 dict = device_properties(self); 219 if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) { 220 aprint_error("%s: no width property\n", device_xname(self)); 221 return; 222 } 223 if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) { 224 aprint_error("%s: no height property\n", device_xname(self)); 225 return; 226 } 227 if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) { 228 aprint_error("%s: no depth property\n", device_xname(self)); 229 return; 230 } 231 if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->sc_stride)) { 232 aprint_error("%s: no linebytes property\n", 233 device_xname(self)); 234 return; 235 } 236 237 prop_dictionary_get_bool(dict, "is_console", &is_console); 238 239 if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, 0x10, PCI_MAPREG_TYPE_MEM, 240 &sc->sc_fb, &sc->sc_fbsize, &flags)) { 241 aprint_error("%s: failed to map the frame buffer.\n", 242 device_xname(sc->sc_dev)); 243 } 244 245 if (pci_mapreg_map(pa, 0x18, PCI_MAPREG_TYPE_MEM, 0, 246 &tag, &sc->sc_regh, &sc->sc_reg, &sc->sc_regsize)) { 247 aprint_error("%s: failed to map registers.\n", 248 device_xname(sc->sc_dev)); 249 } 250 251 aprint_normal("%s: %d MB aperture at 0x%08x\n", device_xname(self), 252 (int)(sc->sc_fbsize >> 20), (uint32_t)sc->sc_fb); 253 254 sc->sc_defaultscreen_descr = (struct wsscreen_descr){ 255 "default", 256 0, 0, 257 NULL, 258 8, 16, 259 WSSCREEN_WSCOLORS | WSSCREEN_HILIT, 260 NULL 261 }; 262 sc->sc_screens[0] = &sc->sc_defaultscreen_descr; 263 sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens}; 264 sc->sc_mode = WSDISPLAYIO_MODE_EMUL; 265 sc->sc_locked = 0; 266 267 vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr, 268 &r128fb_accessops); 269 sc->vd.init_screen = r128fb_init_screen; 270 271 /* init engine here */ 272 r128fb_init(sc); 273 274 ri = &sc->sc_console_screen.scr_ri; 275 276 j = 0; 277 for (i = 0; i < (1 << sc->sc_depth); i++) { 278 279 sc->sc_cmap_red[i] = rasops_cmap[j]; 280 sc->sc_cmap_green[i] = rasops_cmap[j + 1]; 281 sc->sc_cmap_blue[i] = rasops_cmap[j + 2]; 282 r128fb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1], 283 rasops_cmap[j + 2]); 284 j += 3; 285 } 286 287 if (is_console) { 288 vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1, 289 &defattr); 290 sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC; 291 292 r128fb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height, 293 ri->ri_devcmap[(defattr >> 16) & 0xff]); 294 sc->sc_defaultscreen_descr.textops = &ri->ri_ops; 295 sc->sc_defaultscreen_descr.capabilities = ri->ri_caps; 296 sc->sc_defaultscreen_descr.nrows = ri->ri_rows; 297 sc->sc_defaultscreen_descr.ncols = ri->ri_cols; 298 wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0, 299 defattr); 300 vcons_replay_msgbuf(&sc->sc_console_screen); 301 } else { 302 /* 303 * since we're not the console we can postpone the rest 304 * until someone actually allocates a screen for us 305 */ 306 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr); 307 } 308 309 /* no suspend/resume support yet */ 310 pmf_device_register(sc->sc_dev, NULL, NULL); 311 312 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL); 313 DPRINTF("R128_LVDS_GEN_CNTL: %08x\n", reg); 314 if (reg & R128_LVDS_ON) { 315 sc->sc_have_backlight = 1; 316 sc->sc_bl_on = 1; 317 sc->sc_bl_level = 255 - 318 ((reg & R128_LEVEL_MASK) >> R128_LEVEL_SHIFT); 319 pmf_event_register(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_UP, 320 r128fb_brightness_up, TRUE); 321 pmf_event_register(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_DOWN, 322 r128fb_brightness_down, TRUE); 323 aprint_verbose("%s: LVDS output is active, enabling backlight" 324 " control\n", device_xname(self)); 325 } else 326 sc->sc_have_backlight = 0; 327 328 aa.console = is_console; 329 aa.scrdata = &sc->sc_screenlist; 330 aa.accessops = &r128fb_accessops; 331 aa.accesscookie = &sc->vd; 332 333 config_found(sc->sc_dev, &aa, wsemuldisplaydevprint); 334 } 335 336 static int 337 r128fb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, 338 struct lwp *l) 339 { 340 struct vcons_data *vd = v; 341 struct r128fb_softc *sc = vd->cookie; 342 struct wsdisplay_fbinfo *wdf; 343 struct vcons_screen *ms = vd->active; 344 struct wsdisplay_param *param; 345 346 switch (cmd) { 347 case WSDISPLAYIO_GTYPE: 348 *(u_int *)data = WSDISPLAY_TYPE_PCIMISC; 349 return 0; 350 351 /* PCI config read/write passthrough. */ 352 case PCI_IOC_CFGREAD: 353 case PCI_IOC_CFGWRITE: 354 return pci_devioctl(sc->sc_pc, sc->sc_pcitag, 355 cmd, data, flag, l); 356 357 case WSDISPLAYIO_GET_BUSID: 358 return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc, sc->sc_pcitag, data); 359 360 case WSDISPLAYIO_GINFO: 361 if (ms == NULL) 362 return ENODEV; 363 wdf = (void *)data; 364 wdf->height = ms->scr_ri.ri_height; 365 wdf->width = ms->scr_ri.ri_width; 366 wdf->depth = ms->scr_ri.ri_depth; 367 wdf->cmsize = 256; 368 return 0; 369 370 case WSDISPLAYIO_GETCMAP: 371 return r128fb_getcmap(sc, 372 (struct wsdisplay_cmap *)data); 373 374 case WSDISPLAYIO_PUTCMAP: 375 return r128fb_putcmap(sc, 376 (struct wsdisplay_cmap *)data); 377 378 case WSDISPLAYIO_LINEBYTES: 379 *(u_int *)data = sc->sc_stride; 380 return 0; 381 382 case WSDISPLAYIO_SMODE: { 383 int new_mode = *(int*)data; 384 if (new_mode != sc->sc_mode) { 385 sc->sc_mode = new_mode; 386 if(new_mode == WSDISPLAYIO_MODE_EMUL) { 387 r128fb_init(sc); 388 r128fb_restore_palette(sc); 389 r128fb_rectfill(sc, 0, 0, sc->sc_width, 390 sc->sc_height, ms->scr_ri.ri_devcmap[ 391 (ms->scr_defattr >> 16) & 0xff]); 392 vcons_redraw_screen(ms); 393 } 394 } 395 } 396 return 0; 397 398 case WSDISPLAYIO_GETPARAM: 399 param = (struct wsdisplay_param *)data; 400 if (sc->sc_have_backlight == 0) 401 return EPASSTHROUGH; 402 switch (param->param) { 403 case WSDISPLAYIO_PARAM_BRIGHTNESS: 404 param->min = 0; 405 param->max = 255; 406 param->curval = sc->sc_bl_level; 407 return 0; 408 case WSDISPLAYIO_PARAM_BACKLIGHT: 409 param->min = 0; 410 param->max = 1; 411 param->curval = sc->sc_bl_on; 412 return 0; 413 } 414 return EPASSTHROUGH; 415 416 case WSDISPLAYIO_SETPARAM: 417 param = (struct wsdisplay_param *)data; 418 if (sc->sc_have_backlight == 0) 419 return EPASSTHROUGH; 420 switch (param->param) { 421 case WSDISPLAYIO_PARAM_BRIGHTNESS: 422 r128fb_set_backlight(sc, param->curval); 423 return 0; 424 case WSDISPLAYIO_PARAM_BACKLIGHT: 425 r128fb_switch_backlight(sc, param->curval); 426 return 0; 427 } 428 return EPASSTHROUGH; 429 case WSDISPLAYIO_GET_EDID: { 430 struct wsdisplayio_edid_info *d = data; 431 return wsdisplayio_get_edid(sc->sc_dev, d); 432 } 433 } 434 return EPASSTHROUGH; 435 } 436 437 static paddr_t 438 r128fb_mmap(void *v, void *vs, off_t offset, int prot) 439 { 440 struct vcons_data *vd = v; 441 struct r128fb_softc *sc = vd->cookie; 442 paddr_t pa; 443 444 /* 'regular' framebuffer mmap()ing */ 445 if (offset < sc->sc_fbsize) { 446 pa = bus_space_mmap(sc->sc_memt, sc->sc_fb + offset, 0, prot, 447 BUS_SPACE_MAP_LINEAR); 448 return pa; 449 } 450 451 /* 452 * restrict all other mappings to processes with superuser privileges 453 * or the kernel itself 454 */ 455 if (kauth_authorize_generic(kauth_cred_get(), KAUTH_GENERIC_ISSUSER, 456 NULL) != 0) { 457 aprint_normal("%s: mmap() rejected.\n", 458 device_xname(sc->sc_dev)); 459 return -1; 460 } 461 462 if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) { 463 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot, 464 BUS_SPACE_MAP_LINEAR); 465 return pa; 466 } 467 468 if ((offset >= sc->sc_reg) && 469 (offset < (sc->sc_reg + sc->sc_regsize))) { 470 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot, 471 BUS_SPACE_MAP_LINEAR); 472 return pa; 473 } 474 475 #ifdef PCI_MAGIC_IO_RANGE 476 /* allow mapping of IO space */ 477 if ((offset >= PCI_MAGIC_IO_RANGE) && 478 (offset < PCI_MAGIC_IO_RANGE + 0x10000)) { 479 pa = bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE, 480 0, prot, BUS_SPACE_MAP_LINEAR); 481 return pa; 482 } 483 #endif 484 485 #ifdef OFB_ALLOW_OTHERS 486 if (offset >= 0x80000000) { 487 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot, 488 BUS_SPACE_MAP_LINEAR); 489 return pa; 490 } 491 #endif 492 return -1; 493 } 494 495 static void 496 r128fb_init_screen(void *cookie, struct vcons_screen *scr, 497 int existing, long *defattr) 498 { 499 struct r128fb_softc *sc = cookie; 500 struct rasops_info *ri = &scr->scr_ri; 501 502 ri->ri_depth = sc->sc_depth; 503 ri->ri_width = sc->sc_width; 504 ri->ri_height = sc->sc_height; 505 ri->ri_stride = sc->sc_stride; 506 ri->ri_flg = RI_CENTER; 507 508 rasops_init(ri, sc->sc_height / 8, sc->sc_width / 8); 509 ri->ri_caps = WSSCREEN_WSCOLORS; 510 #ifdef VCONS_DRAW_INTR 511 scr->scr_flags |= VCONS_DONT_READ; 512 #endif 513 514 rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight, 515 sc->sc_width / ri->ri_font->fontwidth); 516 517 ri->ri_hw = scr; 518 ri->ri_ops.copyrows = r128fb_copyrows; 519 ri->ri_ops.copycols = r128fb_copycols; 520 ri->ri_ops.eraserows = r128fb_eraserows; 521 ri->ri_ops.erasecols = r128fb_erasecols; 522 ri->ri_ops.cursor = r128fb_cursor; 523 ri->ri_ops.putchar = r128fb_putchar; 524 } 525 526 static int 527 r128fb_putcmap(struct r128fb_softc *sc, struct wsdisplay_cmap *cm) 528 { 529 u_char *r, *g, *b; 530 u_int index = cm->index; 531 u_int count = cm->count; 532 int i, error; 533 u_char rbuf[256], gbuf[256], bbuf[256]; 534 535 #ifdef R128FB_DEBUG 536 aprint_debug("putcmap: %d %d\n",index, count); 537 #endif 538 if (cm->index >= 256 || cm->count > 256 || 539 (cm->index + cm->count) > 256) 540 return EINVAL; 541 error = copyin(cm->red, &rbuf[index], count); 542 if (error) 543 return error; 544 error = copyin(cm->green, &gbuf[index], count); 545 if (error) 546 return error; 547 error = copyin(cm->blue, &bbuf[index], count); 548 if (error) 549 return error; 550 551 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count); 552 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count); 553 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count); 554 555 r = &sc->sc_cmap_red[index]; 556 g = &sc->sc_cmap_green[index]; 557 b = &sc->sc_cmap_blue[index]; 558 559 for (i = 0; i < count; i++) { 560 r128fb_putpalreg(sc, index, *r, *g, *b); 561 index++; 562 r++, g++, b++; 563 } 564 return 0; 565 } 566 567 static int 568 r128fb_getcmap(struct r128fb_softc *sc, struct wsdisplay_cmap *cm) 569 { 570 u_int index = cm->index; 571 u_int count = cm->count; 572 int error; 573 574 if (index >= 255 || count > 256 || index + count > 256) 575 return EINVAL; 576 577 error = copyout(&sc->sc_cmap_red[index], cm->red, count); 578 if (error) 579 return error; 580 error = copyout(&sc->sc_cmap_green[index], cm->green, count); 581 if (error) 582 return error; 583 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count); 584 if (error) 585 return error; 586 587 return 0; 588 } 589 590 static void 591 r128fb_restore_palette(struct r128fb_softc *sc) 592 { 593 int i; 594 595 for (i = 0; i < (1 << sc->sc_depth); i++) { 596 r128fb_putpalreg(sc, i, sc->sc_cmap_red[i], 597 sc->sc_cmap_green[i], sc->sc_cmap_blue[i]); 598 } 599 } 600 601 static int 602 r128fb_putpalreg(struct r128fb_softc *sc, uint8_t idx, uint8_t r, uint8_t g, 603 uint8_t b) 604 { 605 uint32_t reg; 606 607 /* whack the DAC */ 608 reg = (r << 16) | (g << 8) | b; 609 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PALETTE_INDEX, idx); 610 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PALETTE_DATA, reg); 611 return 0; 612 } 613 614 static void 615 r128fb_init(struct r128fb_softc *sc) 616 { 617 uint32_t datatype; 618 619 r128fb_flush_engine(sc); 620 621 r128fb_wait(sc, 9); 622 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_CRTC_OFFSET, 0); 623 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DEFAULT_OFFSET, 0); 624 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DEFAULT_PITCH, 625 sc->sc_stride >> 3); 626 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_AUX_SC_CNTL, 0); 627 bus_space_write_4(sc->sc_memt, sc->sc_regh, 628 R128_DEFAULT_SC_BOTTOM_RIGHT, 629 R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX); 630 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SC_TOP_LEFT, 0); 631 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SC_BOTTOM_RIGHT, 632 R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX); 633 bus_space_write_4(sc->sc_memt, sc->sc_regh, 634 R128_DEFAULT_SC_BOTTOM_RIGHT, 635 R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX); 636 637 #if BYTE_ORDER == BIG_ENDIAN 638 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_DATATYPE, 639 R128_HOST_BIG_ENDIAN_EN); 640 #else 641 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_DATATYPE, 0); 642 #endif 643 644 r128fb_wait(sc, 5); 645 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_PITCH, 646 sc->sc_stride >> 3); 647 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_PITCH, 648 sc->sc_stride >> 3); 649 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_OFFSET, 0); 650 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_OFFSET, 0); 651 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_WRITE_MASK, 652 0xffffffff); 653 654 switch (sc->sc_depth) { 655 case 8: 656 datatype = R128_GMC_DST_8BPP_CI; 657 break; 658 case 15: 659 datatype = R128_GMC_DST_15BPP; 660 break; 661 case 16: 662 datatype = R128_GMC_DST_16BPP; 663 break; 664 case 24: 665 datatype = R128_GMC_DST_24BPP; 666 break; 667 case 32: 668 datatype = R128_GMC_DST_32BPP; 669 break; 670 default: 671 aprint_error("%s: unsupported depth %d\n", 672 device_xname(sc->sc_dev), sc->sc_depth); 673 return; 674 } 675 sc->sc_master_cntl = R128_GMC_CLR_CMP_CNTL_DIS | 676 R128_GMC_AUX_CLIP_DIS | datatype; 677 678 r128fb_flush_engine(sc); 679 } 680 681 static void 682 r128fb_rectfill(struct r128fb_softc *sc, int x, int y, int wi, int he, 683 uint32_t colour) 684 { 685 686 r128fb_wait(sc, 5); 687 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_GUI_MASTER_CNTL, 688 R128_GMC_BRUSH_SOLID_COLOR | 689 R128_GMC_SRC_DATATYPE_COLOR | 690 R128_ROP3_P | 691 sc->sc_master_cntl); 692 693 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_BRUSH_FRGD_CLR, 694 colour); 695 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_CNTL, 696 R128_DST_X_LEFT_TO_RIGHT | R128_DST_Y_TOP_TO_BOTTOM); 697 /* now feed it coordinates */ 698 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y, 699 (x << 16) | y); 700 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_WIDTH_HEIGHT, 701 (wi << 16) | he); 702 } 703 704 static void 705 r128fb_bitblt(struct r128fb_softc *sc, int xs, int ys, int xd, int yd, 706 int wi, int he, int rop) 707 { 708 uint32_t dp_cntl = 0; 709 710 r128fb_wait(sc, 5); 711 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_GUI_MASTER_CNTL, 712 R128_GMC_BRUSH_SOLID_COLOR | 713 R128_GMC_SRC_DATATYPE_COLOR | 714 rop | 715 R128_DP_SRC_SOURCE_MEMORY | 716 sc->sc_master_cntl); 717 718 if (yd <= ys) { 719 dp_cntl = R128_DST_Y_TOP_TO_BOTTOM; 720 } else { 721 ys += he - 1; 722 yd += he - 1; 723 } 724 if (xd <= xs) { 725 dp_cntl |= R128_DST_X_LEFT_TO_RIGHT; 726 } else { 727 xs += wi - 1; 728 xd += wi - 1; 729 } 730 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_CNTL, dp_cntl); 731 732 /* now feed it coordinates */ 733 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_X_Y, 734 (xs << 16) | ys); 735 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y, 736 (xd << 16) | yd); 737 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_WIDTH_HEIGHT, 738 (wi << 16) | he); 739 } 740 741 static void 742 r128fb_cursor(void *cookie, int on, int row, int col) 743 { 744 struct rasops_info *ri = cookie; 745 struct vcons_screen *scr = ri->ri_hw; 746 struct r128fb_softc *sc = scr->scr_cookie; 747 int x, y, wi, he; 748 749 wi = ri->ri_font->fontwidth; 750 he = ri->ri_font->fontheight; 751 752 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) { 753 x = ri->ri_ccol * wi + ri->ri_xorigin; 754 y = ri->ri_crow * he + ri->ri_yorigin; 755 if (ri->ri_flg & RI_CURSOR) { 756 r128fb_bitblt(sc, x, y, x, y, wi, he, R128_ROP3_Dn); 757 ri->ri_flg &= ~RI_CURSOR; 758 } 759 ri->ri_crow = row; 760 ri->ri_ccol = col; 761 if (on) { 762 x = ri->ri_ccol * wi + ri->ri_xorigin; 763 y = ri->ri_crow * he + ri->ri_yorigin; 764 r128fb_bitblt(sc, x, y, x, y, wi, he, R128_ROP3_Dn); 765 ri->ri_flg |= RI_CURSOR; 766 } 767 } else { 768 scr->scr_ri.ri_crow = row; 769 scr->scr_ri.ri_ccol = col; 770 scr->scr_ri.ri_flg &= ~RI_CURSOR; 771 } 772 773 } 774 775 static void 776 r128fb_putchar(void *cookie, int row, int col, u_int c, long attr) 777 { 778 struct rasops_info *ri = cookie; 779 struct wsdisplay_font *font = PICK_FONT(ri, c); 780 struct vcons_screen *scr = ri->ri_hw; 781 struct r128fb_softc *sc = scr->scr_cookie; 782 783 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) { 784 void *data; 785 uint32_t fg, bg; 786 int uc, i; 787 int x, y, wi, he, offset; 788 789 wi = font->fontwidth; 790 he = font->fontheight; 791 792 if (!CHAR_IN_FONT(c, font)) 793 return; 794 bg = ri->ri_devcmap[(attr >> 16) & 0xf]; 795 fg = ri->ri_devcmap[(attr >> 24) & 0xf]; 796 x = ri->ri_xorigin + col * wi; 797 y = ri->ri_yorigin + row * he; 798 if (c == 0x20) { 799 r128fb_rectfill(sc, x, y, wi, he, bg); 800 } else { 801 uc = c - font->firstchar; 802 data = (uint8_t *)font->data + uc * ri->ri_fontscale; 803 804 r128fb_wait(sc, 8); 805 806 bus_space_write_4(sc->sc_memt, sc->sc_regh, 807 R128_DP_GUI_MASTER_CNTL, 808 R128_GMC_BRUSH_SOLID_COLOR | 809 R128_GMC_SRC_DATATYPE_MONO_FG_BG | 810 R128_ROP3_S | 811 R128_DP_SRC_SOURCE_HOST_DATA | 812 R128_GMC_DST_CLIPPING | 813 sc->sc_master_cntl); 814 815 bus_space_write_4(sc->sc_memt, sc->sc_regh, 816 R128_DP_CNTL, 817 R128_DST_Y_TOP_TO_BOTTOM | 818 R128_DST_X_LEFT_TO_RIGHT); 819 820 bus_space_write_4(sc->sc_memt, sc->sc_regh, 821 R128_DP_SRC_FRGD_CLR, fg); 822 bus_space_write_4(sc->sc_memt, sc->sc_regh, 823 R128_DP_SRC_BKGD_CLR, bg); 824 825 /* 826 * The Rage 128 doesn't have anything to skip pixels 827 * when colour expanding but all coordinates 828 * are signed so we just clip the leading bytes and 829 * trailing bits away 830 */ 831 bus_space_write_4(sc->sc_memt, sc->sc_regh, 832 R128_SC_RIGHT, x + wi - 1); 833 bus_space_write_4(sc->sc_memt, sc->sc_regh, 834 R128_SC_LEFT, x); 835 836 /* needed? */ 837 bus_space_write_4(sc->sc_memt, sc->sc_regh, 838 R128_SRC_X_Y, 0); 839 840 offset = 32 - (ri->ri_font->stride << 3); 841 bus_space_write_4(sc->sc_memt, sc->sc_regh, 842 R128_DST_X_Y, ((x - offset) << 16) | y); 843 bus_space_write_4(sc->sc_memt, sc->sc_regh, 844 R128_DST_WIDTH_HEIGHT, (32 << 16) | he); 845 846 r128fb_wait(sc, he); 847 switch (ri->ri_font->stride) { 848 case 1: { 849 uint8_t *data8 = data; 850 uint32_t reg; 851 for (i = 0; i < he; i++) { 852 reg = *data8; 853 bus_space_write_stream_4(sc->sc_memt, 854 sc->sc_regh, 855 R128_HOST_DATA0, reg); 856 data8++; 857 } 858 break; 859 } 860 case 2: { 861 uint16_t *data16 = data; 862 uint32_t reg; 863 for (i = 0; i < he; i++) { 864 reg = *data16; 865 bus_space_write_stream_4(sc->sc_memt, 866 sc->sc_regh, 867 R128_HOST_DATA0, reg); 868 data16++; 869 } 870 break; 871 } 872 } 873 } 874 } 875 } 876 877 static void 878 r128fb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols) 879 { 880 struct rasops_info *ri = cookie; 881 struct vcons_screen *scr = ri->ri_hw; 882 struct r128fb_softc *sc = scr->scr_cookie; 883 int32_t xs, xd, y, width, height; 884 885 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) { 886 xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol; 887 xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol; 888 y = ri->ri_yorigin + ri->ri_font->fontheight * row; 889 width = ri->ri_font->fontwidth * ncols; 890 height = ri->ri_font->fontheight; 891 r128fb_bitblt(sc, xs, y, xd, y, width, height, R128_ROP3_S); 892 } 893 } 894 895 static void 896 r128fb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr) 897 { 898 struct rasops_info *ri = cookie; 899 struct vcons_screen *scr = ri->ri_hw; 900 struct r128fb_softc *sc = scr->scr_cookie; 901 int32_t x, y, width, height, fg, bg, ul; 902 903 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) { 904 x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol; 905 y = ri->ri_yorigin + ri->ri_font->fontheight * row; 906 width = ri->ri_font->fontwidth * ncols; 907 height = ri->ri_font->fontheight; 908 rasops_unpack_attr(fillattr, &fg, &bg, &ul); 909 910 r128fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]); 911 } 912 } 913 914 static void 915 r128fb_copyrows(void *cookie, int srcrow, int dstrow, int nrows) 916 { 917 struct rasops_info *ri = cookie; 918 struct vcons_screen *scr = ri->ri_hw; 919 struct r128fb_softc *sc = scr->scr_cookie; 920 int32_t x, ys, yd, width, height; 921 922 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) { 923 x = ri->ri_xorigin; 924 ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow; 925 yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow; 926 width = ri->ri_emuwidth; 927 height = ri->ri_font->fontheight * nrows; 928 r128fb_bitblt(sc, x, ys, x, yd, width, height, R128_ROP3_S); 929 } 930 } 931 932 static void 933 r128fb_eraserows(void *cookie, int row, int nrows, long fillattr) 934 { 935 struct rasops_info *ri = cookie; 936 struct vcons_screen *scr = ri->ri_hw; 937 struct r128fb_softc *sc = scr->scr_cookie; 938 int32_t x, y, width, height, fg, bg, ul; 939 940 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) { 941 x = ri->ri_xorigin; 942 y = ri->ri_yorigin + ri->ri_font->fontheight * row; 943 width = ri->ri_emuwidth; 944 height = ri->ri_font->fontheight * nrows; 945 rasops_unpack_attr(fillattr, &fg, &bg, &ul); 946 947 r128fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]); 948 } 949 } 950 951 static void 952 r128fb_set_backlight(struct r128fb_softc *sc, int level) 953 { 954 uint32_t reg; 955 956 /* 957 * should we do nothing when backlight is off, should we just store the 958 * level and use it when turning back on or should we just flip sc_bl_on 959 * and turn the backlight on? 960 * For now turn it on so a crashed screensaver can't get the user stuck 961 * with a dark screen as long as hotkeys work 962 */ 963 if (level > 255) level = 255; 964 if (level < 0) level = 0; 965 if (level == sc->sc_bl_level) 966 return; 967 sc->sc_bl_level = level; 968 if (sc->sc_bl_on == 0) 969 sc->sc_bl_on = 1; 970 level = 255 - level; 971 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL); 972 reg &= ~R128_LEVEL_MASK; 973 reg |= (level << R128_LEVEL_SHIFT); 974 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL, reg); 975 DPRINTF("backlight level: %d reg %08x\n", level, reg); 976 } 977 978 static void 979 r128fb_switch_backlight(struct r128fb_softc *sc, int on) 980 { 981 uint32_t reg; 982 int level; 983 984 if (on == sc->sc_bl_on) 985 return; 986 sc->sc_bl_on = on; 987 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL); 988 reg &= ~R128_LEVEL_MASK; 989 level = on ? 255 - sc->sc_bl_level : 255; 990 reg |= level << R128_LEVEL_SHIFT; 991 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL, reg); 992 DPRINTF("backlight state: %d reg %08x\n", on, reg); 993 } 994 995 996 static void 997 r128fb_brightness_up(device_t dev) 998 { 999 struct r128fb_softc *sc = device_private(dev); 1000 1001 r128fb_set_backlight(sc, sc->sc_bl_level + 8); 1002 } 1003 1004 static void 1005 r128fb_brightness_down(device_t dev) 1006 { 1007 struct r128fb_softc *sc = device_private(dev); 1008 1009 r128fb_set_backlight(sc, sc->sc_bl_level - 8); 1010 } 1011