1 /* $NetBSD: chipsfb.c,v 1.25 2011/01/22 15:14:27 cegger Exp $ */ 2 3 /* 4 * Copyright (c) 2006 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 Chips & Technologies 65550 graphics controllers 30 * tested on macppc only so far 31 */ 32 33 #include <sys/cdefs.h> 34 __KERNEL_RCSID(0, "$NetBSD: chipsfb.c,v 1.25 2011/01/22 15:14:27 cegger 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/callout.h> 42 #include <sys/lwp.h> 43 #include <sys/kauth.h> 44 45 #include <dev/videomode/videomode.h> 46 47 #include <dev/pci/pcivar.h> 48 #include <dev/pci/pcireg.h> 49 #include <dev/pci/pcidevs.h> 50 #include <dev/pci/pciio.h> 51 #include <dev/pci/chipsfbreg.h> 52 53 #include <dev/wscons/wsdisplayvar.h> 54 #include <dev/wscons/wsconsio.h> 55 #include <dev/wsfont/wsfont.h> 56 #include <dev/rasops/rasops.h> 57 #include <dev/wscons/wsdisplay_vconsvar.h> 58 #include <dev/pci/wsdisplay_pci.h> 59 60 #include <dev/i2c/i2cvar.h> 61 62 #include "opt_wsemul.h" 63 #include "opt_chipsfb.h" 64 65 struct chipsfb_softc { 66 struct device sc_dev; 67 pci_chipset_tag_t sc_pc; 68 pcitag_t sc_pcitag; 69 70 bus_space_tag_t sc_memt; 71 bus_space_tag_t sc_iot; 72 bus_space_handle_t sc_memh; 73 74 bus_space_tag_t sc_fbt; 75 bus_space_tag_t sc_ioregt; 76 bus_space_handle_t sc_fbh; 77 bus_space_handle_t sc_ioregh; 78 bus_addr_t sc_fb, sc_ioreg; 79 bus_size_t sc_fbsize, sc_ioregsize; 80 81 void *sc_ih; 82 83 size_t memsize; 84 85 int bits_per_pixel; 86 int width, height, linebytes; 87 88 int sc_mode; 89 uint32_t sc_bg; 90 91 u_char sc_cmap_red[256]; 92 u_char sc_cmap_green[256]; 93 u_char sc_cmap_blue[256]; 94 int sc_dacw; 95 96 /* 97 * I2C stuff 98 * DDC2 clock is on GPIO1, data on GPIO0 99 */ 100 struct i2c_controller sc_i2c; 101 uint8_t sc_edid[1024]; 102 int sc_edidbytes; /* number of bytes read from the monitor */ 103 104 struct vcons_data vd; 105 }; 106 107 static struct vcons_screen chipsfb_console_screen; 108 109 extern const u_char rasops_cmap[768]; 110 111 static int chipsfb_match(device_t, cfdata_t, void *); 112 static void chipsfb_attach(device_t, device_t, void *); 113 114 CFATTACH_DECL(chipsfb, sizeof(struct chipsfb_softc), chipsfb_match, 115 chipsfb_attach, NULL, NULL); 116 117 static void chipsfb_init(struct chipsfb_softc *); 118 119 static void chipsfb_cursor(void *, int, int, int); 120 static void chipsfb_copycols(void *, int, int, int, int); 121 static void chipsfb_erasecols(void *, int, int, int, long); 122 static void chipsfb_copyrows(void *, int, int, int); 123 static void chipsfb_eraserows(void *, int, int, long); 124 125 #if 0 126 static int chipsfb_allocattr(void *, int, int, int, long *); 127 static void chipsfb_scroll(void *, void *, int); 128 static int chipsfb_load_font(void *, void *, struct wsdisplay_font *); 129 #endif 130 131 static int chipsfb_putcmap(struct chipsfb_softc *, 132 struct wsdisplay_cmap *); 133 static int chipsfb_getcmap(struct chipsfb_softc *, 134 struct wsdisplay_cmap *); 135 static int chipsfb_putpalreg(struct chipsfb_softc *, uint8_t, uint8_t, 136 uint8_t, uint8_t); 137 138 static void chipsfb_bitblt(struct chipsfb_softc *, int, int, int, int, 139 int, int, uint8_t); 140 static void chipsfb_rectfill(struct chipsfb_softc *, int, int, int, int, 141 int); 142 static void chipsfb_putchar(void *, int, int, u_int, long); 143 static void chipsfb_setup_mono(struct chipsfb_softc *, int, int, int, 144 int, uint32_t, uint32_t); 145 static void chipsfb_feed(struct chipsfb_softc *, int, uint8_t *); 146 147 #if 0 148 static void chipsfb_showpal(struct chipsfb_softc *); 149 #endif 150 static void chipsfb_restore_palette(struct chipsfb_softc *); 151 152 static void chipsfb_wait_idle(struct chipsfb_softc *); 153 154 static uint32_t chipsfb_probe_vram(struct chipsfb_softc *); 155 156 struct wsscreen_descr chipsfb_defaultscreen = { 157 "default", /* name */ 158 0, 0, /* ncols, nrows */ 159 NULL, /* textops */ 160 8, 16, /* fontwidth, fontheight */ 161 WSSCREEN_WSCOLORS | WSSCREEN_HILIT, /* capabilities */ 162 NULL, /* modecookie */ 163 }; 164 165 const struct wsscreen_descr *_chipsfb_scrlist[] = { 166 &chipsfb_defaultscreen, 167 /* XXX other formats, graphics screen? */ 168 }; 169 170 struct wsscreen_list chipsfb_screenlist = { 171 sizeof(_chipsfb_scrlist) / sizeof(struct wsscreen_descr *), _chipsfb_scrlist 172 }; 173 174 static int chipsfb_ioctl(void *, void *, u_long, void *, int, 175 struct lwp *); 176 static paddr_t chipsfb_mmap(void *, void *, off_t, int); 177 static void chipsfb_clearscreen(struct chipsfb_softc *); 178 static void chipsfb_init_screen(void *, struct vcons_screen *, int, 179 long *); 180 181 182 struct wsdisplay_accessops chipsfb_accessops = { 183 chipsfb_ioctl, 184 chipsfb_mmap, 185 NULL, /* vcons_alloc_screen */ 186 NULL, /* vcons_free_screen */ 187 NULL, /* vcons_show_screen */ 188 NULL, /* load_font */ 189 NULL, /* polls */ 190 NULL, /* scroll */ 191 }; 192 193 /* 194 * Inline functions for getting access to register aperture. 195 */ 196 static inline void 197 chipsfb_write32(struct chipsfb_softc *sc, uint32_t reg, uint32_t val) 198 { 199 bus_space_write_4(sc->sc_fbt, sc->sc_fbh, reg, val); 200 } 201 202 static inline uint32_t 203 chipsfb_read32(struct chipsfb_softc *sc, uint32_t reg) 204 { 205 return bus_space_read_4(sc->sc_fbt, sc->sc_fbh, reg); 206 } 207 208 static inline void 209 chipsfb_write_vga(struct chipsfb_softc *sc, uint32_t reg, uint8_t val) 210 { 211 bus_space_write_1(sc->sc_iot, sc->sc_ioregh, reg, val); 212 } 213 214 static inline uint8_t 215 chipsfb_read_vga(struct chipsfb_softc *sc, uint32_t reg) 216 { 217 return bus_space_read_1(sc->sc_iot, sc->sc_ioregh, reg); 218 } 219 220 static inline uint8_t 221 chipsfb_read_indexed(struct chipsfb_softc *sc, uint32_t reg, uint8_t index) 222 { 223 224 chipsfb_write_vga(sc, reg & 0xfffe, index); 225 return chipsfb_read_vga(sc, reg | 0x0001); 226 } 227 228 static inline void 229 chipsfb_write_indexed(struct chipsfb_softc *sc, uint32_t reg, uint8_t index, 230 uint8_t val) 231 { 232 233 chipsfb_write_vga(sc, reg & 0xfffe, index); 234 chipsfb_write_vga(sc, reg | 0x0001, val); 235 } 236 237 static void 238 chipsfb_wait_idle(struct chipsfb_softc *sc) 239 { 240 241 #ifdef CHIPSFB_DEBUG 242 chipsfb_write32(sc, CT_OFF_FB + (800 * 598) - 4, 0); 243 #endif 244 245 /* spin until the blitter is idle */ 246 while ((chipsfb_read32(sc, CT_BLT_CONTROL) & BLT_IS_BUSY) != 0) { 247 } 248 249 #ifdef CHIPSFB_DEBUG 250 chipsfb_write32(sc, CT_OFF_FB + (800 * 598) - 4, 0xffffffff); 251 #endif 252 } 253 254 static int 255 chipsfb_match(device_t parent, cfdata_t match, void *aux) 256 { 257 struct pci_attach_args *pa = (struct pci_attach_args *)aux; 258 259 if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY || 260 PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_DISPLAY_VGA) 261 return 0; 262 if ((PCI_VENDOR(pa->pa_id) == PCI_VENDOR_CHIPS) && 263 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CHIPS_65550)) 264 return 100; 265 if ((PCI_VENDOR(pa->pa_id) == PCI_VENDOR_CHIPS) && 266 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CHIPS_65554)) 267 return 100; 268 return 0; 269 } 270 271 static void 272 chipsfb_attach(device_t parent, device_t self, void *aux) 273 { 274 struct chipsfb_softc *sc = device_private(self); 275 struct pci_attach_args *pa = aux; 276 char devinfo[256]; 277 struct wsemuldisplaydev_attach_args aa; 278 struct rasops_info *ri; 279 prop_dictionary_t dict; 280 pcireg_t screg; 281 ulong defattr; 282 bool console = false; 283 int width, height, i, j; 284 uint32_t bg, fg, ul; 285 286 dict = device_properties(self); 287 sc->sc_mode = WSDISPLAYIO_MODE_EMUL; 288 sc->sc_pc = pa->pa_pc; 289 sc->sc_pcitag = pa->pa_tag; 290 sc->sc_dacw = -1; 291 292 screg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, PCI_COMMAND_STATUS_REG); 293 screg |= PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED; 294 pci_conf_write(sc->sc_pc, sc->sc_pcitag,PCI_COMMAND_STATUS_REG,screg); 295 296 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo)); 297 aprint_normal(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class)); 298 #ifdef CHIPSFB_DEBUG 299 printf(prop_dictionary_externalize(dict)); 300 #endif 301 302 sc->sc_memt = pa->pa_memt; 303 sc->sc_iot = pa->pa_iot; 304 305 /* the framebuffer */ 306 if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM, 307 BUS_SPACE_MAP_LINEAR, 308 &sc->sc_fbt, &sc->sc_fbh, &sc->sc_fb, &sc->sc_fbsize)) { 309 aprint_error_dev(&sc->sc_dev, "failed to map the frame buffer.\n"); 310 } 311 312 /* IO-mapped registers */ 313 if (bus_space_map(sc->sc_iot, 0x0, 0x400, 0, &sc->sc_ioregh) != 0) { 314 aprint_error_dev(&sc->sc_dev, "failed to map IO registers.\n"); 315 } 316 317 sc->memsize = chipsfb_probe_vram(sc); 318 chipsfb_init(sc); 319 320 /* we should read these from the chip instead of depending on OF */ 321 width = height = -1; 322 323 /* detect panel size */ 324 width = chipsfb_read_indexed(sc, CT_FP_INDEX, FP_HSIZE_LSB); 325 width |= (chipsfb_read_indexed(sc, CT_FP_INDEX, FP_HORZ_OVERFLOW_1) 326 & 0x0f) << 8; 327 width = (width + 1) * 8; 328 height = chipsfb_read_indexed(sc, CT_FP_INDEX, FP_VSIZE_LSB); 329 height |= (chipsfb_read_indexed(sc, CT_FP_INDEX, FP_VERT_OVERFLOW_1) 330 & 0x0f) << 8; 331 height++; 332 aprint_verbose("Panel size: %d x %d\n", width, height); 333 334 if (!prop_dictionary_get_uint32(dict, "width", &sc->width)) 335 sc->width = width; 336 if (!prop_dictionary_get_uint32(dict, "height", &sc->height)) 337 sc->height = height; 338 if (!prop_dictionary_get_uint32(dict, "depth", &sc->bits_per_pixel)) 339 sc->bits_per_pixel = 8; 340 if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->linebytes)) 341 sc->linebytes = (sc->width * sc->bits_per_pixel) >> 3; 342 343 prop_dictionary_get_bool(dict, "is_console", &console); 344 345 #ifdef notyet 346 /* XXX this should at least be configurable via kernel config */ 347 chipsfb_set_videomode(sc, &videomode_list[16]); 348 #endif 349 350 vcons_init(&sc->vd, sc, &chipsfb_defaultscreen, &chipsfb_accessops); 351 sc->vd.init_screen = chipsfb_init_screen; 352 353 ri = &chipsfb_console_screen.scr_ri; 354 if (console) { 355 vcons_init_screen(&sc->vd, &chipsfb_console_screen, 1, 356 &defattr); 357 chipsfb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC; 358 359 chipsfb_defaultscreen.textops = &ri->ri_ops; 360 chipsfb_defaultscreen.capabilities = ri->ri_caps; 361 chipsfb_defaultscreen.nrows = ri->ri_rows; 362 chipsfb_defaultscreen.ncols = ri->ri_cols; 363 wsdisplay_cnattach(&chipsfb_defaultscreen, ri, 0, 0, defattr); 364 } else { 365 /* 366 * since we're not the console we can postpone the rest 367 * until someone actually allocates a screen for us 368 */ 369 #ifdef notyet 370 chipsfb_set_videomode(sc, &videomode_list[0]); 371 #endif 372 } 373 374 rasops_unpack_attr(defattr, &fg, &bg, &ul); 375 sc->sc_bg = ri->ri_devcmap[bg]; 376 chipsfb_clearscreen(sc); 377 378 if (console) 379 vcons_replay_msgbuf(&chipsfb_console_screen); 380 381 aprint_normal_dev(&sc->sc_dev, "%d MB aperture, %d MB VRAM at 0x%08x\n", 382 (u_int)(sc->sc_fbsize >> 20), 383 sc->memsize >> 20, (u_int)sc->sc_fb); 384 #ifdef CHIPSFB_DEBUG 385 aprint_debug("fb: %08lx\n", (ulong)ri->ri_bits); 386 #endif 387 388 j = 0; 389 for (i = 0; i < 256; i++) { 390 chipsfb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1], 391 rasops_cmap[j + 2]); 392 j += 3; 393 } 394 395 aa.console = console; 396 aa.scrdata = &chipsfb_screenlist; 397 aa.accessops = &chipsfb_accessops; 398 aa.accesscookie = &sc->vd; 399 400 config_found(self, &aa, wsemuldisplaydevprint); 401 } 402 403 static int 404 chipsfb_putpalreg(struct chipsfb_softc *sc, uint8_t index, uint8_t r, 405 uint8_t g, uint8_t b) 406 { 407 408 sc->sc_cmap_red[index] = r; 409 sc->sc_cmap_green[index] = g; 410 sc->sc_cmap_blue[index] = b; 411 412 chipsfb_write_vga(sc, CT_DACMASK, 0xff); 413 chipsfb_write_vga(sc, CT_WRITEINDEX, index); 414 chipsfb_write_vga(sc, CT_DACDATA, r); 415 chipsfb_write_vga(sc, CT_DACDATA, g); 416 chipsfb_write_vga(sc, CT_DACDATA, b); 417 418 return 0; 419 } 420 421 static int 422 chipsfb_putcmap(struct chipsfb_softc *sc, struct wsdisplay_cmap *cm) 423 { 424 u_char *r, *g, *b; 425 u_int index = cm->index; 426 u_int count = cm->count; 427 int i, error; 428 u_char rbuf[256], gbuf[256], bbuf[256]; 429 430 #ifdef CHIPSFB_DEBUG 431 aprint_debug("putcmap: %d %d\n",index, count); 432 #endif 433 if (cm->index >= 256 || cm->count > 256 || 434 (cm->index + cm->count) > 256) 435 return EINVAL; 436 error = copyin(cm->red, &rbuf[index], count); 437 if (error) 438 return error; 439 error = copyin(cm->green, &gbuf[index], count); 440 if (error) 441 return error; 442 error = copyin(cm->blue, &bbuf[index], count); 443 if (error) 444 return error; 445 446 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count); 447 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count); 448 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count); 449 450 r = &sc->sc_cmap_red[index]; 451 g = &sc->sc_cmap_green[index]; 452 b = &sc->sc_cmap_blue[index]; 453 454 for (i = 0; i < count; i++) { 455 chipsfb_putpalreg(sc, index, *r, *g, *b); 456 index++; 457 r++, g++, b++; 458 } 459 return 0; 460 } 461 462 static int 463 chipsfb_getcmap(struct chipsfb_softc *sc, struct wsdisplay_cmap *cm) 464 { 465 u_int index = cm->index; 466 u_int count = cm->count; 467 int error; 468 469 if (index >= 255 || count > 256 || index + count > 256) 470 return EINVAL; 471 472 error = copyout(&sc->sc_cmap_red[index], cm->red, count); 473 if (error) 474 return error; 475 error = copyout(&sc->sc_cmap_green[index], cm->green, count); 476 if (error) 477 return error; 478 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count); 479 if (error) 480 return error; 481 482 return 0; 483 } 484 485 static void 486 chipsfb_clearscreen(struct chipsfb_softc *sc) 487 { 488 chipsfb_rectfill(sc, 0, 0, sc->width, sc->height, sc->sc_bg); 489 } 490 491 /* 492 * wsdisplay_emulops 493 */ 494 495 static void 496 chipsfb_cursor(void *cookie, int on, int row, int col) 497 { 498 struct rasops_info *ri = cookie; 499 struct vcons_screen *scr = ri->ri_hw; 500 struct chipsfb_softc *sc = scr->scr_cookie; 501 int x, y, wi, he; 502 503 wi = ri->ri_font->fontwidth; 504 he = ri->ri_font->fontheight; 505 506 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) { 507 x = ri->ri_ccol * wi + ri->ri_xorigin; 508 y = ri->ri_crow * he + ri->ri_yorigin; 509 if (ri->ri_flg & RI_CURSOR) { 510 chipsfb_bitblt(sc, x, y, x, y, wi, he, ROP_NOT_DST); 511 ri->ri_flg &= ~RI_CURSOR; 512 } 513 ri->ri_crow = row; 514 ri->ri_ccol = col; 515 if (on) { 516 x = ri->ri_ccol * wi + ri->ri_xorigin; 517 y = ri->ri_crow * he + ri->ri_yorigin; 518 chipsfb_bitblt(sc, x, y, x, y, wi, he, ROP_NOT_DST); 519 ri->ri_flg |= RI_CURSOR; 520 } 521 } else { 522 ri->ri_flg &= ~RI_CURSOR; 523 ri->ri_crow = row; 524 ri->ri_ccol = col; 525 } 526 } 527 528 #if 0 529 int 530 chipsfb_mapchar(void *cookie, int uni, u_int *index) 531 { 532 return 0; 533 } 534 #endif 535 536 static void 537 chipsfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols) 538 { 539 struct rasops_info *ri = cookie; 540 struct vcons_screen *scr = ri->ri_hw; 541 struct chipsfb_softc *sc = scr->scr_cookie; 542 int32_t xs, xd, y, width, height; 543 544 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) { 545 xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol; 546 xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol; 547 y = ri->ri_yorigin + ri->ri_font->fontheight * row; 548 width = ri->ri_font->fontwidth * ncols; 549 height = ri->ri_font->fontheight; 550 chipsfb_bitblt(sc, xs, y, xd, y, width, height, ROP_COPY); 551 } 552 } 553 554 static void 555 chipsfb_erasecols(void *cookie, int row, int startcol, int ncols, 556 long fillattr) 557 { 558 struct rasops_info *ri = cookie; 559 struct vcons_screen *scr = ri->ri_hw; 560 struct chipsfb_softc *sc = scr->scr_cookie; 561 int32_t x, y, width, height, fg, bg, ul; 562 563 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) { 564 x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol; 565 y = ri->ri_yorigin + ri->ri_font->fontheight * row; 566 width = ri->ri_font->fontwidth * ncols; 567 height = ri->ri_font->fontheight; 568 rasops_unpack_attr(fillattr, &fg, &bg, &ul); 569 570 chipsfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]); 571 } 572 } 573 574 static void 575 chipsfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows) 576 { 577 struct rasops_info *ri = cookie; 578 struct vcons_screen *scr = ri->ri_hw; 579 struct chipsfb_softc *sc = scr->scr_cookie; 580 int32_t x, ys, yd, width, height; 581 582 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) { 583 x = ri->ri_xorigin; 584 ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow; 585 yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow; 586 width = ri->ri_emuwidth; 587 height = ri->ri_font->fontheight * nrows; 588 chipsfb_bitblt(sc, x, ys, x, yd, width, height, ROP_COPY); 589 } 590 } 591 592 static void 593 chipsfb_eraserows(void *cookie, int row, int nrows, long fillattr) 594 { 595 struct rasops_info *ri = cookie; 596 struct vcons_screen *scr = ri->ri_hw; 597 struct chipsfb_softc *sc = scr->scr_cookie; 598 int32_t x, y, width, height, fg, bg, ul; 599 600 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) { 601 rasops_unpack_attr(fillattr, &fg, &bg, &ul); 602 if ((row == 0) && (nrows == ri->ri_rows)) { 603 /* clear the whole screen */ 604 chipsfb_rectfill(sc, 0, 0, ri->ri_width, 605 ri->ri_height, ri->ri_devcmap[bg]); 606 } else { 607 x = ri->ri_xorigin; 608 y = ri->ri_yorigin + ri->ri_font->fontheight * row; 609 width = ri->ri_emuwidth; 610 height = ri->ri_font->fontheight * nrows; 611 chipsfb_rectfill(sc, x, y, width, height, 612 ri->ri_devcmap[bg]); 613 } 614 } 615 } 616 617 static void 618 chipsfb_bitblt(struct chipsfb_softc *sc, int xs, int ys, int xd, int yd, 619 int width, int height, uint8_t rop) 620 { 621 uint32_t src, dst, cmd = rop, stride, size; 622 623 cmd |= BLT_PAT_IS_SOLID; 624 625 /* we assume 8 bit for now */ 626 src = xs + ys * sc->linebytes; 627 dst = xd + yd * sc->linebytes; 628 629 if (xs < xd) { 630 /* right-to-left operation */ 631 cmd |= BLT_START_RIGHT; 632 src += width - 1; 633 dst += width - 1; 634 } 635 636 if (ys < yd) { 637 /* bottom-to-top operation */ 638 cmd |= BLT_START_BOTTOM; 639 src += (height - 1) * sc->linebytes; 640 dst += (height - 1) * sc->linebytes; 641 } 642 643 stride = (sc->linebytes << 16) | sc->linebytes; 644 size = (height << 16) | width; 645 646 chipsfb_wait_idle(sc); 647 chipsfb_write32(sc, CT_BLT_STRIDE, stride); 648 chipsfb_write32(sc, CT_BLT_SRCADDR, src); 649 chipsfb_write32(sc, CT_BLT_DSTADDR, dst); 650 chipsfb_write32(sc, CT_BLT_CONTROL, cmd); 651 chipsfb_write32(sc, CT_BLT_SIZE, size); 652 #ifdef CHIPSFB_WAIT 653 chipsfb_wait_idle(sc); 654 #endif 655 } 656 657 static void 658 chipsfb_rectfill(struct chipsfb_softc *sc, int x, int y, int width, 659 int height, int colour) 660 { 661 uint32_t dst, cmd, stride, size; 662 663 cmd = BLT_PAT_IS_SOLID | BLT_PAT_IS_MONO | ROP_PAT; 664 665 /* we assume 8 bit for now */ 666 dst = x + y * sc->linebytes; 667 668 stride = (sc->linebytes << 16) | sc->linebytes; 669 size = (height << 16) | width; 670 671 chipsfb_wait_idle(sc); 672 chipsfb_write32(sc, CT_BLT_STRIDE, stride); 673 chipsfb_write32(sc, CT_BLT_SRCADDR, dst); 674 chipsfb_write32(sc, CT_BLT_DSTADDR, dst); 675 chipsfb_write32(sc, CT_BLT_CONTROL, cmd); 676 chipsfb_write32(sc, CT_BLT_BG, colour); 677 chipsfb_write32(sc, CT_BLT_FG, colour); 678 chipsfb_write32(sc, CT_BLT_SIZE, size); 679 #ifdef CHIPSFB_WAIT 680 chipsfb_wait_idle(sc); 681 #endif 682 } 683 684 static void 685 chipsfb_putchar(void *cookie, int row, int col, u_int c, long attr) 686 { 687 struct rasops_info *ri = cookie; 688 struct wsdisplay_font *font = PICK_FONT(ri, c); 689 struct vcons_screen *scr = ri->ri_hw; 690 struct chipsfb_softc *sc = scr->scr_cookie; 691 692 if (__predict_false((unsigned int)row > ri->ri_rows || 693 (unsigned int)col > ri->ri_cols)) 694 return; 695 696 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) { 697 uint8_t *data; 698 int fg, bg, uc; 699 int x, y, wi, he; 700 701 wi = font->fontwidth; 702 he = font->fontheight; 703 704 if (!CHAR_IN_FONT(c, font)) 705 return; 706 bg = (u_char)ri->ri_devcmap[(attr >> 16) & 0xf]; 707 fg = (u_char)ri->ri_devcmap[(attr >> 24) & 0xf]; 708 x = ri->ri_xorigin + col * wi; 709 y = ri->ri_yorigin + row * he; 710 if (c == 0x20) { 711 chipsfb_rectfill(sc, x, y, wi, he, bg); 712 } else { 713 uc = c - font->firstchar; 714 data = (uint8_t *)font->data + uc * 715 ri->ri_fontscale; 716 chipsfb_setup_mono(sc, x, y, wi, he, fg, bg); 717 chipsfb_feed(sc, font->stride * he, data); 718 } 719 } 720 } 721 722 static void 723 chipsfb_setup_mono(struct chipsfb_softc *sc, int xd, int yd, int width, 724 int height, uint32_t fg, uint32_t bg) 725 { 726 uint32_t dst, cmd, stride, size; 727 728 cmd = BLT_PAT_IS_SOLID | BLT_SRC_IS_CPU | BLT_SRC_IS_MONO | ROP_COPY; 729 730 /* we assume 8 bit for now */ 731 dst = xd + yd * sc->linebytes; 732 733 stride = (sc->linebytes << 16); 734 size = (height << 16) | width; 735 736 chipsfb_wait_idle(sc); 737 chipsfb_write32(sc, CT_BLT_STRIDE, stride); 738 chipsfb_write32(sc, CT_BLT_EXPCTL, MONO_SRC_ALIGN_BYTE); 739 chipsfb_write32(sc, CT_BLT_DSTADDR, dst); 740 chipsfb_write32(sc, CT_BLT_SRCADDR, 0); 741 chipsfb_write32(sc, CT_BLT_CONTROL, cmd); 742 chipsfb_write32(sc, CT_BLT_BG, bg); 743 chipsfb_write32(sc, CT_BLT_FG, fg); 744 chipsfb_write32(sc, CT_BLT_SIZE, size); 745 } 746 747 static void 748 chipsfb_feed(struct chipsfb_softc *sc, int count, uint8_t *data) 749 { 750 int i; 751 uint32_t latch = 0, bork; 752 int shift = 0; 753 754 for (i = 0; i < count; i++) { 755 bork = data[i]; 756 latch |= (bork << shift); 757 if (shift == 24) { 758 chipsfb_write32(sc, CT_OFF_DATA, latch); 759 latch = 0; 760 shift = 0; 761 } else 762 shift += 8; 763 } 764 765 if (shift != 0) { 766 chipsfb_write32(sc, CT_OFF_DATA, latch); 767 } 768 769 /* apparently the chip wants 64bit-aligned data or it won't go idle */ 770 if ((count + 3) & 0x04) { 771 chipsfb_write32(sc, CT_OFF_DATA, 0); 772 } 773 #ifdef CHIPSFB_WAIT 774 chipsfb_wait_idle(sc); 775 #endif 776 } 777 778 #if 0 779 static void 780 chipsfb_showpal(struct chipsfb_softc *sc) 781 { 782 int i, x = 0; 783 784 for (i = 0; i < 16; i++) { 785 chipsfb_rectfill(sc, x, 0, 64, 64, i); 786 x += 64; 787 } 788 } 789 #endif 790 791 #if 0 792 static int 793 chipsfb_allocattr(void *cookie, int fg, int bg, int flags, long *attrp) 794 { 795 796 return 0; 797 } 798 #endif 799 800 static void 801 chipsfb_restore_palette(struct chipsfb_softc *sc) 802 { 803 int i; 804 805 for (i = 0; i < 256; i++) { 806 chipsfb_putpalreg(sc, 807 i, 808 sc->sc_cmap_red[i], 809 sc->sc_cmap_green[i], 810 sc->sc_cmap_blue[i]); 811 } 812 } 813 814 /* 815 * wsdisplay_accessops 816 */ 817 818 static int 819 chipsfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, 820 struct lwp *l) 821 { 822 struct vcons_data *vd = v; 823 struct chipsfb_softc *sc = vd->cookie; 824 struct wsdisplay_fbinfo *wdf; 825 struct vcons_screen *ms = vd->active; 826 827 switch (cmd) { 828 case WSDISPLAYIO_GTYPE: 829 *(u_int *)data = WSDISPLAY_TYPE_PCIMISC; 830 return 0; 831 832 case WSDISPLAYIO_GINFO: 833 wdf = (void *)data; 834 wdf->height = ms->scr_ri.ri_height; 835 wdf->width = ms->scr_ri.ri_width; 836 wdf->depth = ms->scr_ri.ri_depth; 837 wdf->cmsize = 256; 838 return 0; 839 840 case WSDISPLAYIO_GETCMAP: 841 return chipsfb_getcmap(sc, 842 (struct wsdisplay_cmap *)data); 843 844 case WSDISPLAYIO_PUTCMAP: 845 return chipsfb_putcmap(sc, 846 (struct wsdisplay_cmap *)data); 847 848 /* PCI config read/write passthrough. */ 849 case PCI_IOC_CFGREAD: 850 case PCI_IOC_CFGWRITE: 851 return pci_devioctl(sc->sc_pc, sc->sc_pcitag, 852 cmd, data, flag, l); 853 854 case WSDISPLAYIO_GET_BUSID: 855 return wsdisplayio_busid_pci(&sc->sc_dev, sc->sc_pc, 856 sc->sc_pcitag, data); 857 858 case WSDISPLAYIO_SMODE: { 859 int new_mode = *(int*)data; 860 if (new_mode != sc->sc_mode) { 861 sc->sc_mode = new_mode; 862 if(new_mode == WSDISPLAYIO_MODE_EMUL) { 863 chipsfb_restore_palette(sc); 864 vcons_redraw_screen(ms); 865 } 866 } 867 } 868 return 0; 869 } 870 return EPASSTHROUGH; 871 } 872 873 static paddr_t 874 chipsfb_mmap(void *v, void *vs, off_t offset, int prot) 875 { 876 struct vcons_data *vd = v; 877 struct chipsfb_softc *sc = vd->cookie; 878 paddr_t pa; 879 880 /* 'regular' framebuffer mmap()ing */ 881 if (offset < sc->memsize) { 882 pa = bus_space_mmap(sc->sc_fbt, offset, 0, prot, 883 BUS_SPACE_MAP_LINEAR); 884 return pa; 885 } 886 887 /* 888 * restrict all other mappings to processes with superuser privileges 889 * or the kernel itself 890 */ 891 if (kauth_authorize_generic(kauth_cred_get(), KAUTH_GENERIC_ISSUSER, 892 NULL) != 0) { 893 aprint_normal_dev(&sc->sc_dev, "mmap() rejected.\n"); 894 return -1; 895 } 896 897 if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) { 898 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot, 899 BUS_SPACE_MAP_LINEAR); 900 return pa; 901 } 902 903 #ifdef PCI_MAGIC_IO_RANGE 904 /* allow mapping of IO space */ 905 if ((offset >= PCI_MAGIC_IO_RANGE) && 906 (offset < PCI_MAGIC_IO_RANGE + 0x10000)) { 907 pa = bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE, 908 0, prot, BUS_SPACE_MAP_LINEAR); 909 return pa; 910 } 911 #endif 912 913 #ifdef OFB_ALLOW_OTHERS 914 if (offset >= 0x80000000) { 915 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot, 916 BUS_SPACE_MAP_LINEAR); 917 return pa; 918 } 919 #endif 920 return -1; 921 } 922 923 static void 924 chipsfb_init_screen(void *cookie, struct vcons_screen *scr, 925 int existing, long *defattr) 926 { 927 struct chipsfb_softc *sc = cookie; 928 struct rasops_info *ri = &scr->scr_ri; 929 930 ri->ri_depth = sc->bits_per_pixel; 931 ri->ri_width = sc->width; 932 ri->ri_height = sc->height; 933 ri->ri_stride = sc->width; 934 ri->ri_flg = RI_CENTER | RI_FULLCLEAR; 935 936 ri->ri_bits = bus_space_vaddr(sc->sc_fbt, sc->sc_fbh); 937 938 #ifdef CHIPSFB_DEBUG 939 aprint_debug("addr: %08lx\n", (ulong)ri->ri_bits); 940 #endif 941 if (existing) { 942 ri->ri_flg |= RI_CLEAR; 943 } 944 945 rasops_init(ri, sc->height/8, sc->width/8); 946 ri->ri_caps = WSSCREEN_WSCOLORS; 947 948 rasops_reconfig(ri, sc->height / ri->ri_font->fontheight, 949 sc->width / ri->ri_font->fontwidth); 950 951 ri->ri_hw = scr; 952 ri->ri_ops.copyrows = chipsfb_copyrows; 953 ri->ri_ops.copycols = chipsfb_copycols; 954 ri->ri_ops.eraserows = chipsfb_eraserows; 955 ri->ri_ops.erasecols = chipsfb_erasecols; 956 ri->ri_ops.cursor = chipsfb_cursor; 957 ri->ri_ops.putchar = chipsfb_putchar; 958 } 959 960 #if 0 961 int 962 chipsfb_load_font(void *v, void *cookie, struct wsdisplay_font *data) 963 { 964 965 return 0; 966 } 967 #endif 968 969 static void 970 chipsfb_init(struct chipsfb_softc *sc) 971 { 972 973 chipsfb_wait_idle(sc); 974 975 chipsfb_write_indexed(sc, CT_CONF_INDEX, XR_IO_CONTROL, 976 ENABLE_CRTC_EXT | ENABLE_ATTR_EXT); 977 chipsfb_write_indexed(sc, CT_CONF_INDEX, XR_ADDR_MAPPING, 978 ENABLE_LINEAR); 979 980 /* setup the blitter */ 981 } 982 983 static uint32_t 984 chipsfb_probe_vram(struct chipsfb_softc *sc) 985 { 986 uint32_t ofs = 0x00080000; /* 512kB */ 987 988 /* 989 * advance in 0.5MB steps, see if we can read back what we wrote and 990 * if what we wrote to 0 is left untouched. Max. fb size is 4MB so 991 * we voluntarily stop there. 992 */ 993 chipsfb_write32(sc, 0, 0xf0f0f0f0); 994 chipsfb_write32(sc, ofs, 0x0f0f0f0f); 995 while ((chipsfb_read32(sc, 0) == 0xf0f0f0f0) && 996 (chipsfb_read32(sc, ofs) == 0x0f0f0f0f) && 997 (ofs < 0x00400000)) { 998 999 ofs += 0x00080000; 1000 chipsfb_write32(sc, ofs, 0x0f0f0f0f); 1001 } 1002 1003 return ofs; 1004 } 1005