1 /* $NetBSD: r128fb.c,v 1.8 2009/05/06 18:41:54 elad 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.8 2009/05/06 18:41:54 elad 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 <uvm/uvm_extern.h> 45 46 #include <dev/videomode/videomode.h> 47 48 #include <dev/pci/pcivar.h> 49 #include <dev/pci/pcireg.h> 50 #include <dev/pci/pcidevs.h> 51 #include <dev/pci/pciio.h> 52 #include <dev/pci/r128fbreg.h> 53 54 #include <dev/wscons/wsdisplayvar.h> 55 #include <dev/wscons/wsconsio.h> 56 #include <dev/wsfont/wsfont.h> 57 #include <dev/rasops/rasops.h> 58 #include <dev/wscons/wsdisplay_vconsvar.h> 59 60 #include <dev/i2c/i2cvar.h> 61 62 struct r128fb_softc { 63 device_t sc_dev; 64 65 pci_chipset_tag_t sc_pc; 66 pcitag_t sc_pcitag; 67 68 bus_space_tag_t sc_memt; 69 bus_space_tag_t sc_iot; 70 71 bus_space_handle_t sc_fbh; 72 bus_space_handle_t sc_regh; 73 bus_addr_t sc_fb, sc_reg; 74 bus_size_t sc_fbsize, sc_regsize; 75 76 int sc_width, sc_height, sc_depth, sc_stride; 77 int sc_locked; 78 void *sc_fbaddr; 79 struct vcons_screen sc_console_screen; 80 struct wsscreen_descr sc_defaultscreen_descr; 81 const struct wsscreen_descr *sc_screens[1]; 82 struct wsscreen_list sc_screenlist; 83 struct vcons_data vd; 84 int sc_mode; 85 u_char sc_cmap_red[256]; 86 u_char sc_cmap_green[256]; 87 u_char sc_cmap_blue[256]; 88 /* engine stuff */ 89 uint32_t sc_master_cntl; 90 }; 91 92 static int r128fb_match(device_t, cfdata_t, void *); 93 static void r128fb_attach(device_t, device_t, void *); 94 95 CFATTACH_DECL_NEW(r128fb, sizeof(struct r128fb_softc), 96 r128fb_match, r128fb_attach, NULL, NULL); 97 98 extern const u_char rasops_cmap[768]; 99 100 static int r128fb_ioctl(void *, void *, u_long, void *, int, 101 struct lwp *); 102 static paddr_t r128fb_mmap(void *, void *, off_t, int); 103 static void r128fb_init_screen(void *, struct vcons_screen *, int, long *); 104 105 static int r128fb_putcmap(struct r128fb_softc *, struct wsdisplay_cmap *); 106 static int r128fb_getcmap(struct r128fb_softc *, struct wsdisplay_cmap *); 107 static void r128fb_restore_palette(struct r128fb_softc *); 108 static int r128fb_putpalreg(struct r128fb_softc *, uint8_t, uint8_t, 109 uint8_t, uint8_t); 110 111 static void r128fb_init(struct r128fb_softc *); 112 static void r128fb_flush_engine(struct r128fb_softc *); 113 static void r128fb_rectfill(struct r128fb_softc *, int, int, int, int, 114 uint32_t); 115 static void r128fb_bitblt(struct r128fb_softc *, int, int, int, int, int, 116 int, int); 117 118 static void r128fb_cursor(void *, int, int, int); 119 #if 0 120 static void r128fb_putchar(void *, int, int, u_int, long); 121 #endif 122 static void r128fb_copycols(void *, int, int, int, int); 123 static void r128fb_erasecols(void *, int, int, int, long); 124 static void r128fb_copyrows(void *, int, int, int); 125 static void r128fb_eraserows(void *, int, int, long); 126 127 struct wsdisplay_accessops r128fb_accessops = { 128 r128fb_ioctl, 129 r128fb_mmap, 130 NULL, /* alloc_screen */ 131 NULL, /* free_screen */ 132 NULL, /* show_screen */ 133 NULL, /* load_font */ 134 NULL, /* pollc */ 135 NULL /* scroll */ 136 }; 137 138 static inline void 139 r128fb_wait(struct r128fb_softc *sc, int slots) 140 { 141 uint32_t reg; 142 143 do { 144 reg = (bus_space_read_4(sc->sc_memt, sc->sc_regh, 145 R128_GUI_STAT) & R128_GUI_FIFOCNT_MASK); 146 } while (reg <= slots); 147 } 148 149 static void 150 r128fb_flush_engine(struct r128fb_softc *sc) 151 { 152 uint32_t reg; 153 154 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_PC_NGUI_CTLSTAT); 155 reg |= R128_PC_FLUSH_ALL; 156 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PC_NGUI_CTLSTAT, reg); 157 do { 158 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, 159 R128_PC_NGUI_CTLSTAT); 160 } while (reg & R128_PC_BUSY); 161 } 162 163 static int 164 r128fb_match(device_t parent, cfdata_t match, void *aux) 165 { 166 struct pci_attach_args *pa = (struct pci_attach_args *)aux; 167 168 if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY) 169 return 0; 170 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_ATI) 171 return 0; 172 173 /* only cards tested on so far - likely need a list */ 174 if ((PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGE1AGP4XT) || 175 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGE3AGP4XT) || 176 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGE_MOB_M3_AGP)) 177 return 100; 178 return (0); 179 } 180 181 static void 182 r128fb_attach(device_t parent, device_t self, void *aux) 183 { 184 struct r128fb_softc *sc = device_private(self); 185 struct pci_attach_args *pa = aux; 186 struct rasops_info *ri; 187 char devinfo[256]; 188 struct wsemuldisplaydev_attach_args aa; 189 prop_dictionary_t dict; 190 unsigned long defattr; 191 bool is_console; 192 int i, j; 193 194 sc->sc_pc = pa->pa_pc; 195 sc->sc_pcitag = pa->pa_tag; 196 sc->sc_memt = pa->pa_memt; 197 sc->sc_iot = pa->pa_iot; 198 sc->sc_dev = self; 199 200 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo)); 201 aprint_normal(": %s\n", devinfo); 202 203 /* fill in parameters from properties */ 204 dict = device_properties(self); 205 if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) { 206 aprint_error("%s: no width property\n", device_xname(self)); 207 return; 208 } 209 if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) { 210 aprint_error("%s: no height property\n", device_xname(self)); 211 return; 212 } 213 if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) { 214 aprint_error("%s: no depth property\n", device_xname(self)); 215 return; 216 } 217 if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->sc_stride)) { 218 aprint_error("%s: no linebytes property\n", 219 device_xname(self)); 220 return; 221 } 222 223 prop_dictionary_get_bool(dict, "is_console", &is_console); 224 225 if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM, 226 BUS_SPACE_MAP_LINEAR, 227 &sc->sc_memt, &sc->sc_fbh, &sc->sc_fb, &sc->sc_fbsize)) { 228 aprint_error("%s: failed to map the frame buffer.\n", 229 device_xname(sc->sc_dev)); 230 } 231 sc->sc_fbaddr = bus_space_vaddr(sc->sc_memt, sc->sc_fbh); 232 233 if (pci_mapreg_map(pa, 0x18, PCI_MAPREG_TYPE_MEM, 0, 234 &sc->sc_memt, &sc->sc_regh, &sc->sc_reg, &sc->sc_regsize)) { 235 aprint_error("%s: failed to map registers.\n", 236 device_xname(sc->sc_dev)); 237 } 238 239 /* 240 * XXX yeah, casting the fb address to uint32_t is formally wrong 241 * but as far as I know there are no mach64 with 64bit BARs 242 */ 243 aprint_normal("%s: %d MB aperture at 0x%08x\n", device_xname(self), 244 (int)(sc->sc_fbsize >> 20), (uint32_t)sc->sc_fb); 245 246 sc->sc_defaultscreen_descr = (struct wsscreen_descr){ 247 "default", 248 0, 0, 249 NULL, 250 8, 16, 251 WSSCREEN_WSCOLORS | WSSCREEN_HILIT, 252 NULL 253 }; 254 sc->sc_screens[0] = &sc->sc_defaultscreen_descr; 255 sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens}; 256 sc->sc_mode = WSDISPLAYIO_MODE_EMUL; 257 sc->sc_locked = 0; 258 259 vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr, 260 &r128fb_accessops); 261 sc->vd.init_screen = r128fb_init_screen; 262 263 /* init engine here */ 264 r128fb_init(sc); 265 266 ri = &sc->sc_console_screen.scr_ri; 267 268 if (is_console) { 269 vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1, 270 &defattr); 271 sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC; 272 273 sc->sc_defaultscreen_descr.textops = &ri->ri_ops; 274 sc->sc_defaultscreen_descr.capabilities = ri->ri_caps; 275 sc->sc_defaultscreen_descr.nrows = ri->ri_rows; 276 sc->sc_defaultscreen_descr.ncols = ri->ri_cols; 277 wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0, 278 defattr); 279 } else { 280 /* 281 * since we're not the console we can postpone the rest 282 * until someone actually allocates a screen for us 283 */ 284 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr); 285 } 286 287 j = 0; 288 for (i = 0; i < (1 << sc->sc_depth); i++) { 289 290 sc->sc_cmap_red[i] = rasops_cmap[j]; 291 sc->sc_cmap_green[i] = rasops_cmap[j + 1]; 292 sc->sc_cmap_blue[i] = rasops_cmap[j + 2]; 293 r128fb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1], 294 rasops_cmap[j + 2]); 295 j += 3; 296 } 297 298 r128fb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height, 299 ri->ri_devcmap[(defattr >> 16) & 0xff]); 300 301 aa.console = is_console; 302 aa.scrdata = &sc->sc_screenlist; 303 aa.accessops = &r128fb_accessops; 304 aa.accesscookie = &sc->vd; 305 306 config_found(sc->sc_dev, &aa, wsemuldisplaydevprint); 307 308 } 309 310 static int 311 r128fb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, 312 struct lwp *l) 313 { 314 struct vcons_data *vd = v; 315 struct r128fb_softc *sc = vd->cookie; 316 struct wsdisplay_fbinfo *wdf; 317 struct vcons_screen *ms = vd->active; 318 319 switch (cmd) { 320 321 case WSDISPLAYIO_GTYPE: 322 *(u_int *)data = WSDISPLAY_TYPE_PCIMISC; 323 return 0; 324 325 /* PCI config read/write passthrough. */ 326 case PCI_IOC_CFGREAD: 327 case PCI_IOC_CFGWRITE: 328 return (pci_devioctl(sc->sc_pc, sc->sc_pcitag, 329 cmd, data, flag, l)); 330 331 case WSDISPLAYIO_GINFO: 332 if (ms == NULL) 333 return ENODEV; 334 wdf = (void *)data; 335 wdf->height = ms->scr_ri.ri_height; 336 wdf->width = ms->scr_ri.ri_width; 337 wdf->depth = ms->scr_ri.ri_depth; 338 wdf->cmsize = 256; 339 return 0; 340 341 case WSDISPLAYIO_GETCMAP: 342 return r128fb_getcmap(sc, 343 (struct wsdisplay_cmap *)data); 344 345 case WSDISPLAYIO_PUTCMAP: 346 return r128fb_putcmap(sc, 347 (struct wsdisplay_cmap *)data); 348 349 case WSDISPLAYIO_LINEBYTES: 350 *(u_int *)data = sc->sc_stride; 351 return 0; 352 353 case WSDISPLAYIO_SMODE: 354 { 355 int new_mode = *(int*)data; 356 357 /* notify the bus backend */ 358 if (new_mode != sc->sc_mode) { 359 sc->sc_mode = new_mode; 360 if(new_mode == WSDISPLAYIO_MODE_EMUL) { 361 r128fb_restore_palette(sc); 362 vcons_redraw_screen(ms); 363 } 364 } 365 } 366 return 0; 367 } 368 return EPASSTHROUGH; 369 } 370 371 static paddr_t 372 r128fb_mmap(void *v, void *vs, off_t offset, int prot) 373 { 374 struct vcons_data *vd = v; 375 struct r128fb_softc *sc = vd->cookie; 376 paddr_t pa; 377 378 /* 'regular' framebuffer mmap()ing */ 379 if (offset < sc->sc_fbsize) { 380 pa = bus_space_mmap(sc->sc_memt, sc->sc_fb + offset, 0, prot, 381 BUS_SPACE_MAP_LINEAR); 382 return pa; 383 } 384 385 /* 386 * restrict all other mappings to processes with superuser privileges 387 * or the kernel itself 388 */ 389 if (kauth_authorize_generic(kauth_cred_get(), KAUTH_GENERIC_ISSUSER, 390 NULL) != 0) { 391 aprint_normal("%s: mmap() rejected.\n", 392 device_xname(sc->sc_dev)); 393 return -1; 394 } 395 396 if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) { 397 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot, 398 BUS_SPACE_MAP_LINEAR); 399 return pa; 400 } 401 402 if ((offset >= sc->sc_reg) && 403 (offset < (sc->sc_reg + sc->sc_regsize))) { 404 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot, 405 BUS_SPACE_MAP_LINEAR); 406 return pa; 407 } 408 409 #ifdef PCI_MAGIC_IO_RANGE 410 /* allow mapping of IO space */ 411 if ((offset >= PCI_MAGIC_IO_RANGE) && 412 (offset < PCI_MAGIC_IO_RANGE + 0x10000)) { 413 pa = bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE, 414 0, prot, BUS_SPACE_MAP_LINEAR); 415 return pa; 416 } 417 #endif 418 419 #ifdef OFB_ALLOW_OTHERS 420 if (offset >= 0x80000000) { 421 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot, 422 BUS_SPACE_MAP_LINEAR); 423 return pa; 424 } 425 #endif 426 return -1; 427 } 428 429 static void 430 r128fb_init_screen(void *cookie, struct vcons_screen *scr, 431 int existing, long *defattr) 432 { 433 struct r128fb_softc *sc = cookie; 434 struct rasops_info *ri = &scr->scr_ri; 435 436 ri->ri_depth = sc->sc_depth; 437 ri->ri_width = sc->sc_width; 438 ri->ri_height = sc->sc_height; 439 ri->ri_stride = sc->sc_stride; 440 ri->ri_flg = RI_CENTER | RI_FULLCLEAR; 441 442 ri->ri_bits = (char *)sc->sc_fbaddr; 443 444 if (existing) { 445 ri->ri_flg |= RI_CLEAR; 446 } 447 448 rasops_init(ri, sc->sc_height / 8, sc->sc_width / 8); 449 ri->ri_caps = WSSCREEN_WSCOLORS; 450 451 rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight, 452 sc->sc_width / ri->ri_font->fontwidth); 453 454 ri->ri_hw = scr; 455 ri->ri_ops.copyrows = r128fb_copyrows; 456 ri->ri_ops.copycols = r128fb_copycols; 457 ri->ri_ops.eraserows = r128fb_eraserows; 458 ri->ri_ops.erasecols = r128fb_erasecols; 459 ri->ri_ops.cursor = r128fb_cursor; 460 #if 0 461 ri->ri_ops.putchar = r128fb_putchar; 462 #endif 463 } 464 465 static int 466 r128fb_putcmap(struct r128fb_softc *sc, struct wsdisplay_cmap *cm) 467 { 468 u_char *r, *g, *b; 469 u_int index = cm->index; 470 u_int count = cm->count; 471 int i, error; 472 u_char rbuf[256], gbuf[256], bbuf[256]; 473 474 #ifdef R128FB_DEBUG 475 aprint_debug("putcmap: %d %d\n",index, count); 476 #endif 477 if (cm->index >= 256 || cm->count > 256 || 478 (cm->index + cm->count) > 256) 479 return EINVAL; 480 error = copyin(cm->red, &rbuf[index], count); 481 if (error) 482 return error; 483 error = copyin(cm->green, &gbuf[index], count); 484 if (error) 485 return error; 486 error = copyin(cm->blue, &bbuf[index], count); 487 if (error) 488 return error; 489 490 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count); 491 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count); 492 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count); 493 494 r = &sc->sc_cmap_red[index]; 495 g = &sc->sc_cmap_green[index]; 496 b = &sc->sc_cmap_blue[index]; 497 498 for (i = 0; i < count; i++) { 499 r128fb_putpalreg(sc, index, *r, *g, *b); 500 index++; 501 r++, g++, b++; 502 } 503 return 0; 504 } 505 506 static int 507 r128fb_getcmap(struct r128fb_softc *sc, struct wsdisplay_cmap *cm) 508 { 509 u_int index = cm->index; 510 u_int count = cm->count; 511 int error; 512 513 if (index >= 255 || count > 256 || index + count > 256) 514 return EINVAL; 515 516 error = copyout(&sc->sc_cmap_red[index], cm->red, count); 517 if (error) 518 return error; 519 error = copyout(&sc->sc_cmap_green[index], cm->green, count); 520 if (error) 521 return error; 522 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count); 523 if (error) 524 return error; 525 526 return 0; 527 } 528 529 static void 530 r128fb_restore_palette(struct r128fb_softc *sc) 531 { 532 int i; 533 534 for (i = 0; i < (1 << sc->sc_depth); i++) { 535 r128fb_putpalreg(sc, i, sc->sc_cmap_red[i], 536 sc->sc_cmap_green[i], sc->sc_cmap_blue[i]); 537 } 538 } 539 540 static int 541 r128fb_putpalreg(struct r128fb_softc *sc, uint8_t idx, uint8_t r, uint8_t g, 542 uint8_t b) 543 { 544 uint32_t reg; 545 546 /* whack the DAC */ 547 reg = (r << 16) | (g << 8) | b; 548 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PALETTE_INDEX, idx); 549 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PALETTE_DATA, reg); 550 return 0; 551 } 552 553 static void 554 r128fb_init(struct r128fb_softc *sc) 555 { 556 uint32_t datatype; 557 558 r128fb_flush_engine(sc); 559 560 r128fb_wait(sc, 8); 561 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_CRTC_OFFSET, 0); 562 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DEFAULT_OFFSET, 0); 563 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DEFAULT_PITCH, 564 sc->sc_stride >> 3); 565 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_AUX_SC_CNTL, 0); 566 bus_space_write_4(sc->sc_memt, sc->sc_regh, 567 R128_DEFAULT_SC_BOTTOM_RIGHT, 568 R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX); 569 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SC_TOP_LEFT, 0); 570 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SC_BOTTOM_RIGHT, 571 R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX); 572 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_DATATYPE, 573 R128_HOST_BIG_ENDIAN_EN); 574 575 r128fb_wait(sc, 5); 576 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_PITCH, 577 sc->sc_stride >> 3); 578 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_PITCH, 579 sc->sc_stride >> 3); 580 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_OFFSET, 0); 581 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_OFFSET, 0); 582 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_WRITE_MASK, 583 0xffffffff); 584 585 switch (sc->sc_depth) { 586 case 8: 587 datatype = R128_GMC_DST_8BPP_CI; 588 break; 589 case 15: 590 datatype = R128_GMC_DST_15BPP; 591 break; 592 case 16: 593 datatype = R128_GMC_DST_16BPP; 594 break; 595 case 24: 596 datatype = R128_GMC_DST_24BPP; 597 break; 598 case 32: 599 datatype = R128_GMC_DST_32BPP; 600 break; 601 default: 602 aprint_error("%s: unsupported depth %d\n", 603 device_xname(sc->sc_dev), sc->sc_depth); 604 return; 605 } 606 sc->sc_master_cntl = R128_GMC_CLR_CMP_CNTL_DIS | 607 R128_GMC_AUX_CLIP_DIS | datatype; 608 609 r128fb_flush_engine(sc); 610 } 611 612 static void 613 r128fb_rectfill(struct r128fb_softc *sc, int x, int y, int wi, int he, 614 uint32_t colour) 615 { 616 617 r128fb_wait(sc, 6); 618 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_GUI_MASTER_CNTL, 619 R128_GMC_BRUSH_SOLID_COLOR | 620 R128_GMC_SRC_DATATYPE_COLOR | 621 R128_ROP3_P | 622 sc->sc_master_cntl); 623 624 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_BRUSH_FRGD_CLR, 625 colour); 626 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_CNTL, 627 R128_DST_X_LEFT_TO_RIGHT | R128_DST_Y_TOP_TO_BOTTOM); 628 /* now feed it coordinates */ 629 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y, 630 (x << 16) | y); 631 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_WIDTH_HEIGHT, 632 (wi << 16) | he); 633 r128fb_flush_engine(sc); 634 } 635 636 static void 637 r128fb_bitblt(struct r128fb_softc *sc, int xs, int ys, int xd, int yd, 638 int wi, int he, int rop) 639 { 640 uint32_t dp_cntl = 0; 641 642 r128fb_wait(sc, 5); 643 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_GUI_MASTER_CNTL, 644 R128_GMC_BRUSH_SOLID_COLOR | 645 R128_GMC_SRC_DATATYPE_COLOR | 646 rop | 647 R128_DP_SRC_SOURCE_MEMORY | 648 sc->sc_master_cntl); 649 650 if (yd <= ys) { 651 dp_cntl = R128_DST_Y_TOP_TO_BOTTOM; 652 } else { 653 ys += he - 1; 654 yd += he - 1; 655 } 656 if (xd <= xs) { 657 dp_cntl |= R128_DST_X_LEFT_TO_RIGHT; 658 } else { 659 xs += wi - 1; 660 xd += wi - 1; 661 } 662 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_CNTL, dp_cntl); 663 664 /* now feed it coordinates */ 665 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_X_Y, 666 (xs << 16) | ys); 667 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y, 668 (xd << 16) | yd); 669 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_WIDTH_HEIGHT, 670 (wi << 16) | he); 671 r128fb_flush_engine(sc); 672 } 673 674 static void 675 r128fb_cursor(void *cookie, int on, int row, int col) 676 { 677 struct rasops_info *ri = cookie; 678 struct vcons_screen *scr = ri->ri_hw; 679 struct r128fb_softc *sc = scr->scr_cookie; 680 int x, y, wi, he; 681 682 wi = ri->ri_font->fontwidth; 683 he = ri->ri_font->fontheight; 684 685 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) { 686 x = ri->ri_ccol * wi + ri->ri_xorigin; 687 y = ri->ri_crow * he + ri->ri_yorigin; 688 if (ri->ri_flg & RI_CURSOR) { 689 r128fb_bitblt(sc, x, y, x, y, wi, he, R128_ROP3_Dn); 690 ri->ri_flg &= ~RI_CURSOR; 691 } 692 ri->ri_crow = row; 693 ri->ri_ccol = col; 694 if (on) { 695 x = ri->ri_ccol * wi + ri->ri_xorigin; 696 y = ri->ri_crow * he + ri->ri_yorigin; 697 r128fb_bitblt(sc, x, y, x, y, wi, he, R128_ROP3_Dn); 698 ri->ri_flg |= RI_CURSOR; 699 } 700 } else { 701 scr->scr_ri.ri_crow = row; 702 scr->scr_ri.ri_ccol = col; 703 scr->scr_ri.ri_flg &= ~RI_CURSOR; 704 } 705 706 } 707 708 #if 0 709 static void 710 r128fb_putchar(void *cookie, int row, int col, u_int c, long attr) 711 { 712 } 713 #endif 714 715 static void 716 r128fb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols) 717 { 718 struct rasops_info *ri = cookie; 719 struct vcons_screen *scr = ri->ri_hw; 720 struct r128fb_softc *sc = scr->scr_cookie; 721 int32_t xs, xd, y, width, height; 722 723 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) { 724 xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol; 725 xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol; 726 y = ri->ri_yorigin + ri->ri_font->fontheight * row; 727 width = ri->ri_font->fontwidth * ncols; 728 height = ri->ri_font->fontheight; 729 r128fb_bitblt(sc, xs, y, xd, y, width, height, R128_ROP3_S); 730 } 731 } 732 733 static void 734 r128fb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr) 735 { 736 struct rasops_info *ri = cookie; 737 struct vcons_screen *scr = ri->ri_hw; 738 struct r128fb_softc *sc = scr->scr_cookie; 739 int32_t x, y, width, height, fg, bg, ul; 740 741 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) { 742 x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol; 743 y = ri->ri_yorigin + ri->ri_font->fontheight * row; 744 width = ri->ri_font->fontwidth * ncols; 745 height = ri->ri_font->fontheight; 746 rasops_unpack_attr(fillattr, &fg, &bg, &ul); 747 748 r128fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]); 749 } 750 } 751 752 static void 753 r128fb_copyrows(void *cookie, int srcrow, int dstrow, int nrows) 754 { 755 struct rasops_info *ri = cookie; 756 struct vcons_screen *scr = ri->ri_hw; 757 struct r128fb_softc *sc = scr->scr_cookie; 758 int32_t x, ys, yd, width, height; 759 760 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) { 761 x = ri->ri_xorigin; 762 ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow; 763 yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow; 764 width = ri->ri_emuwidth; 765 height = ri->ri_font->fontheight*nrows; 766 r128fb_bitblt(sc, x, ys, x, yd, width, height, R128_ROP3_S); 767 } 768 } 769 770 static void 771 r128fb_eraserows(void *cookie, int row, int nrows, long fillattr) 772 { 773 struct rasops_info *ri = cookie; 774 struct vcons_screen *scr = ri->ri_hw; 775 struct r128fb_softc *sc = scr->scr_cookie; 776 int32_t x, y, width, height, fg, bg, ul; 777 778 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) { 779 x = ri->ri_xorigin; 780 y = ri->ri_yorigin + ri->ri_font->fontheight * row; 781 width = ri->ri_emuwidth; 782 height = ri->ri_font->fontheight * nrows; 783 rasops_unpack_attr(fillattr, &fg, &bg, &ul); 784 785 r128fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]); 786 } 787 } 788 789