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