1 /* $OpenBSD: sisfb.c,v 1.2 2010/12/26 15:40:59 miod Exp $ */ 2 3 /* 4 * Copyright (c) 2010 Miodrag Vallat. 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 /* 20 * Minimalistic driver for the SIS315 Pro frame buffer found on the 21 * Lemote Fuloong 2F systems. 22 * Does not support accelaration, mode change, secondary output, or 23 * anything fancy. 24 */ 25 26 #include <sys/cdefs.h> 27 __KERNEL_RCSID(0, "$NetBSD: sisfb.c,v 1.5 2014/01/26 21:22:49 bouyer Exp $"); 28 29 #include <sys/param.h> 30 #include <sys/systm.h> 31 #include <sys/device.h> 32 #include <sys/bus.h> 33 #include <sys/kauth.h> 34 35 #include <uvm/uvm_extern.h> 36 37 #include <dev/pci/pcireg.h> 38 #include <dev/pci/pcivar.h> 39 #include <dev/pci/pcidevs.h> 40 #include <dev/pci/pciio.h> 41 42 #include <dev/videomode/videomode.h> 43 44 #include <dev/wscons/wsdisplayvar.h> 45 #include <dev/wscons/wsconsio.h> 46 #include <dev/wsfont/wsfont.h> 47 #include <dev/rasops/rasops.h> 48 #include <dev/wscons/wsdisplay_vconsvar.h> 49 #include <dev/pci/wsdisplay_pci.h> 50 51 #include <dev/i2c/i2cvar.h> 52 53 #include <dev/pci/sisfb.h> 54 55 struct sisfb_softc; 56 57 /* minimal frame buffer information, suitable for early console */ 58 59 struct sisfb { 60 struct sisfb_softc *sc; 61 uint8_t cmap[256 * 3]; 62 63 bus_space_tag_t fbt; 64 bus_space_handle_t fbh; 65 bus_addr_t fbbase; 66 bus_size_t fbsize; 67 68 bus_space_tag_t mmiot; 69 bus_space_handle_t mmioh; 70 bus_addr_t mmiobase; 71 bus_size_t mmiosize; 72 73 bus_space_tag_t iot; 74 bus_space_handle_t ioh; 75 bus_addr_t iobase; 76 bus_size_t iosize; 77 78 struct vcons_screen vcs; 79 struct wsscreen_descr wsd; 80 81 int fb_depth; 82 int fb_width; 83 int fb_height; 84 int fb_stride; 85 void *fb_addr; 86 }; 87 88 struct sisfb_softc { 89 device_t sc_dev; 90 struct sisfb *sc_fb; 91 struct sisfb sc_fb_store; 92 93 struct vcons_data vd; 94 struct wsscreen_list sc_wsl; 95 const struct wsscreen_descr *sc_scrlist[1]; 96 int sc_nscr; 97 int sc_mode; 98 99 pci_chipset_tag_t sc_pc; 100 pcitag_t sc_pt; 101 }; 102 103 int sisfb_match(device_t, cfdata_t, void *); 104 void sisfb_attach(device_t, device_t, void *); 105 106 CFATTACH_DECL_NEW(sisfb, sizeof(struct sisfb_softc), 107 sisfb_match, sisfb_attach, NULL, NULL); 108 109 110 int sisfb_alloc_screen(void *, const struct wsscreen_descr *, void **, int *, 111 int *, long *); 112 void sisfb_free_screen(void *, void *); 113 int sisfb_ioctl(void *, void *, u_long, void *, int, struct lwp *); 114 int sisfb_show_screen(void *, void *, int, void (*)(void *, int, int), 115 void *); 116 paddr_t sisfb_mmap(void *, void *, off_t, int); 117 void sisfb_init_screen(void *, struct vcons_screen *, int, long *); 118 119 120 struct wsdisplay_accessops sisfb_accessops = { 121 sisfb_ioctl, 122 sisfb_mmap, 123 sisfb_alloc_screen, 124 sisfb_free_screen, 125 sisfb_show_screen, 126 NULL, /* load_font */ 127 NULL, /* poolc */ 128 NULL /* scroll */ 129 }; 130 131 int sisfb_getcmap(uint8_t *, struct wsdisplay_cmap *); 132 void sisfb_loadcmap(struct sisfb *, int, int); 133 int sisfb_putcmap(uint8_t *, struct wsdisplay_cmap *); 134 int sisfb_setup(struct sisfb *); 135 136 static struct sisfb sisfbcn; 137 138 /* 139 * Control Register access 140 * 141 * These are 8 bit registers; the choice of larger width types is intentional. 142 */ 143 144 #define SIS_VGA_PORT_OFFSET 0x380 145 146 #define SEQ_ADDR (0x3c4 - SIS_VGA_PORT_OFFSET) 147 #define SEQ_DATA (0x3c5 - SIS_VGA_PORT_OFFSET) 148 #define DAC_ADDR (0x3c8 - SIS_VGA_PORT_OFFSET) 149 #define DAC_DATA (0x3c9 - SIS_VGA_PORT_OFFSET) 150 #undef CRTC_ADDR 151 #define CRTC_ADDR (0x3d4 - SIS_VGA_PORT_OFFSET) 152 #define CRTC_DATA (0x3d5 - SIS_VGA_PORT_OFFSET) 153 154 #define CRTC_HDISPLE 0x01 /* horizontal display end */ 155 #define CRTC_OVERFLL 0x07 /* overflow low */ 156 #define CRTC_STARTADRH 0x0C /* linear start address mid */ 157 #define CRTC_STARTADRL 0x0D /* linear start address low */ 158 #define CRTC_VDE 0x12 /* vertical display end */ 159 160 161 static inline uint sisfb_crtc_read(struct sisfb *, uint); 162 static inline void sisfb_crtc_write(struct sisfb *, uint, uint); 163 static inline uint sisfb_seq_read(struct sisfb *, uint); 164 static inline void sisfb_seq_write(struct sisfb *, uint, uint); 165 166 static inline uint 167 sisfb_crtc_read(struct sisfb *fb, uint idx) 168 { 169 uint val; 170 bus_space_write_1(fb->iot, fb->ioh, CRTC_ADDR, idx); 171 val = bus_space_read_1(fb->iot, fb->ioh, CRTC_DATA); 172 #ifdef SIS_DEBUG 173 printf("CRTC %04x -> %02x\n", idx, val); 174 #endif 175 return val; 176 } 177 178 static inline void 179 sisfb_crtc_write(struct sisfb *fb, uint idx, uint val) 180 { 181 #ifdef SIS_DEBUG 182 printf("CRTC %04x <- %02x\n", idx, val); 183 #endif 184 bus_space_write_1(fb->iot, fb->ioh, CRTC_ADDR, idx); 185 bus_space_write_1(fb->iot, fb->ioh, CRTC_DATA, val); 186 } 187 188 static inline uint 189 sisfb_seq_read(struct sisfb *fb, uint idx) 190 { 191 uint val; 192 bus_space_write_1(fb->iot, fb->ioh, SEQ_ADDR, idx); 193 val = bus_space_read_1(fb->iot, fb->ioh, SEQ_DATA); 194 #ifdef SIS_DEBUG 195 printf("SEQ %04x -> %02x\n", idx, val); 196 #endif 197 return val; 198 } 199 200 static inline void 201 sisfb_seq_write(struct sisfb *fb, uint idx, uint val) 202 { 203 #ifdef SIS_DEBUG 204 printf("SEQ %04x <- %02x\n", idx, val); 205 #endif 206 bus_space_write_1(fb->iot, fb->ioh, SEQ_ADDR, idx); 207 bus_space_write_1(fb->iot, fb->ioh, SEQ_DATA, val); 208 } 209 210 int 211 sisfb_match(device_t parent, cfdata_t match, void *aux) 212 { 213 struct pci_attach_args *pa = (struct pci_attach_args *)aux; 214 215 if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY) 216 return 0; 217 218 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_SIS) 219 return 0; 220 221 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SIS_315PRO_VGA) 222 return 100; 223 return (0); 224 } 225 226 void 227 sisfb_attach(device_t parent, device_t self, void *aux) 228 { 229 struct sisfb_softc *sc = device_private(self); 230 struct pci_attach_args *pa = (struct pci_attach_args *)aux; 231 struct rasops_info *ri; 232 struct wsemuldisplaydev_attach_args waa; 233 struct sisfb *fb; 234 int console; 235 unsigned long defattr; 236 237 sc->sc_dev = self; 238 console = sisfbcn.vcs.scr_ri.ri_hw != NULL; 239 240 if (console) 241 fb = &sisfbcn; 242 else { 243 fb = &sc->sc_fb_store; 244 } 245 246 sc->sc_fb = fb; 247 fb->sc = sc; 248 249 pci_aprint_devinfo(pa, NULL); 250 251 sc->sc_pt = pa->pa_tag; 252 sc->sc_pc = pa->pa_pc; 253 254 if (!console) { 255 fb->fbt = pa->pa_memt; 256 fb->mmiot = pa->pa_memt; 257 fb->iot = pa->pa_iot; 258 if (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_MEM, 259 BUS_SPACE_MAP_LINEAR, &fb->fbt, &fb->fbh, 260 &fb->fbbase, &fb->fbsize) != 0) { 261 aprint_error_dev(self, ": can't map frame buffer\n"); 262 return; 263 } 264 265 if (pci_mapreg_map(pa, PCI_MAPREG_START + 4, 266 PCI_MAPREG_TYPE_MEM, 0, 267 &fb->mmiot, &fb->mmioh, &fb->mmiobase, &fb->mmiosize) != 0) { 268 aprint_error_dev(self, ": can't map mmio area\n"); 269 goto fail1; 270 } 271 272 if (pci_mapreg_map(pa, PCI_MAPREG_START + 8, PCI_MAPREG_TYPE_IO, 273 0, &fb->iot, &fb->ioh, &fb->iobase, &fb->iosize) != 0) { 274 aprint_error_dev(self, ": can't map registers\n"); 275 goto fail2; 276 } 277 278 279 if (sisfb_setup(sc->sc_fb) != 0) { 280 aprint_error_dev(self, ": can't setup frame buffer\n"); 281 goto fail3; 282 } 283 } 284 285 aprint_normal_dev(self, ": %dx%dx%d frame buffer\n", 286 fb->vcs.scr_ri.ri_width, fb->vcs.scr_ri.ri_height, 287 fb->vcs.scr_ri.ri_depth); 288 289 aprint_debug_dev(self, ": fb 0x%" PRIxBUSSIZE "@0x%" PRIxBUSADDR 290 ", mmio 0x%" PRIxBUSSIZE "@0x%" PRIxBUSADDR 291 ", io 0x%" PRIxBUSSIZE "@0x%" PRIxBUSADDR "\n", 292 fb->fbsize, fb->fbbase, 293 fb->mmiosize, fb->mmiobase, 294 fb->iosize, fb->iobase); 295 296 fb->wsd = (struct wsscreen_descr){ 297 "default", 298 0, 0, 299 NULL, 300 8, 16, 301 WSSCREEN_WSCOLORS | WSSCREEN_HILIT, 302 NULL 303 }; 304 sc->sc_scrlist[0] = &fb->wsd; 305 sc->sc_wsl = (struct wsscreen_list){1, sc->sc_scrlist}; 306 sc->sc_mode = WSDISPLAYIO_MODE_EMUL; 307 308 309 vcons_init(&sc->vd, sc, &fb->wsd, &sisfb_accessops); 310 sc->vd.init_screen = sisfb_init_screen; 311 312 ri = &fb->vcs.scr_ri; 313 if (console) { 314 vcons_init_screen(&sc->vd, &fb->vcs, 1, &defattr); 315 fb->vcs.scr_flags |= VCONS_SCREEN_IS_STATIC; 316 fb->wsd.textops = &ri->ri_ops; 317 fb->wsd.capabilities = ri->ri_caps; 318 fb->wsd.nrows = ri->ri_rows; 319 fb->wsd.ncols = ri->ri_cols; 320 wsdisplay_cnattach(&fb->wsd, ri, 0, 0, defattr); 321 vcons_replay_msgbuf(&fb->vcs); 322 } else { 323 /* 324 * since we're not the console we can postpone the rest 325 * until someone actually allocates a screen for us 326 */ 327 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr); 328 } 329 330 waa.console = console; 331 waa.scrdata = &sc->sc_wsl; 332 waa.accessops = &sisfb_accessops; 333 waa.accesscookie = &sc->vd; 334 335 config_found(sc->sc_dev, &waa, wsemuldisplaydevprint); 336 return; 337 338 fail3: 339 bus_space_unmap(fb->iot, fb->ioh, fb->iosize); 340 fail2: 341 bus_space_unmap(fb->mmiot, fb->mmioh, fb->mmiosize); 342 fail1: 343 bus_space_unmap(fb->fbt, fb->fbh, fb->fbsize); 344 } 345 346 /* 347 * wsdisplay accesops 348 */ 349 350 int 351 sisfb_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep, 352 int *curxp, int *curyp, long *attrp) 353 { 354 struct sisfb_softc *sc = (struct sisfb_softc *)v; 355 struct rasops_info *ri = &sc->sc_fb->vcs.scr_ri; 356 357 if (sc->sc_nscr > 0) 358 return ENOMEM; 359 360 *cookiep = ri; 361 *curxp = *curyp = 0; 362 ri->ri_ops.allocattr(ri, 0, 0, 0, attrp); 363 sc->sc_nscr++; 364 365 return 0; 366 } 367 368 void 369 sisfb_free_screen(void *v, void *cookie) 370 { 371 struct sisfb_softc *sc = (struct sisfb_softc *)v; 372 373 sc->sc_nscr--; 374 } 375 376 int 377 sisfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flags, 378 struct lwp *l) 379 { 380 struct vcons_data *vd = v; 381 struct sisfb_softc *sc = vd->cookie; 382 struct sisfb *fb = sc->sc_fb; 383 struct rasops_info *ri = &fb->vcs.scr_ri; 384 struct wsdisplay_cmap *cm; 385 struct wsdisplay_fbinfo *wdf; 386 int rc; 387 388 switch (cmd) { 389 case WSDISPLAYIO_GTYPE: 390 *(uint *)data = WSDISPLAY_TYPE_PCIMISC; 391 return 0; 392 case WSDISPLAYIO_GINFO: 393 if (vd->active != NULL) { 394 wdf = (struct wsdisplay_fbinfo *)data; 395 wdf->width = ri->ri_width; 396 wdf->height = ri->ri_height; 397 wdf->depth = ri->ri_depth; 398 wdf->cmsize = 256; 399 return 0; 400 } else 401 return ENODEV; 402 case WSDISPLAYIO_LINEBYTES: 403 *(uint *)data = ri->ri_stride; 404 return 0; 405 case WSDISPLAYIO_GETCMAP: 406 cm = (struct wsdisplay_cmap *)data; 407 rc = sisfb_getcmap(fb->cmap, cm); 408 return rc; 409 case WSDISPLAYIO_PUTCMAP: 410 cm = (struct wsdisplay_cmap *)data; 411 rc = sisfb_putcmap(fb->cmap, cm); 412 if (rc != 0) 413 return rc; 414 if (ri->ri_depth == 8) 415 sisfb_loadcmap(fb, cm->index, cm->count); 416 return 0; 417 case WSDISPLAYIO_SMODE: 418 sc->sc_mode = *(uint *)data; 419 aprint_debug_dev(sc->sc_dev, ": switching to "); 420 switch(sc->sc_mode) { 421 case WSDISPLAYIO_MODE_EMUL: 422 aprint_debug("WSDISPLAYIO_MODE_EMUL\n"); 423 break; 424 case WSDISPLAYIO_MODE_MAPPED: 425 aprint_debug("WSDISPLAYIO_MODE_MAPPED\n"); 426 break; 427 case WSDISPLAYIO_MODE_DUMBFB: 428 aprint_debug("WSDISPLAYIO_MODE_DUMBFB\n"); 429 break; 430 default: 431 aprint_debug("unknown mode %d\n", sc->sc_mode); 432 return EINVAL; 433 } 434 return 0; 435 case WSDISPLAYIO_GMODE: 436 *(uint *)data = sc->sc_mode; 437 return 0; 438 case PCI_IOC_CFGREAD: 439 case PCI_IOC_CFGWRITE: 440 return pci_devioctl(sc->sc_pc, sc->sc_pt, cmd, data, flags, l); 441 case WSDISPLAYIO_GET_BUSID: 442 return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc, 443 sc->sc_pt, data); 444 } 445 return EPASSTHROUGH; 446 } 447 448 int 449 sisfb_show_screen(void *v, void *cookie, int waitok, 450 void (*cb)(void *, int, int), void *cbarg) 451 { 452 return 0; 453 } 454 455 paddr_t 456 sisfb_mmap(void *v, void *vs, off_t offset, int prot) 457 { 458 struct vcons_data *vd = v; 459 struct sisfb_softc *sc = vd->cookie; 460 struct rasops_info *ri = &sc->sc_fb->vcs.scr_ri; 461 struct sisfb *fb = sc->sc_fb; 462 const uintptr_t fb_offset = 463 (uintptr_t)bus_space_vaddr(fb->fbt, fb->fbh) - (uintptr_t)fb->fb_addr; 464 paddr_t pa; 465 466 if (sc->sc_mode != WSDISPLAYIO_MODE_MAPPED) { 467 if (offset >= 0 && offset < ri->ri_stride * ri->ri_height) { 468 pa = bus_space_mmap(fb->fbt, 469 fb->fbbase, fb_offset + offset, 470 prot, BUS_SPACE_MAP_LINEAR); 471 return pa; 472 } 473 return -1; 474 } 475 if (kauth_authorize_generic(kauth_cred_get(), KAUTH_GENERIC_ISSUSER, 476 NULL) != 0) { 477 return -1; 478 } 479 if (offset >= (fb->fbbase & ~PAGE_MASK) && 480 offset <= ((fb->fbbase + fb->fbsize + PAGE_SIZE - 1) & ~PAGE_MASK)) { 481 pa = bus_space_mmap(fb->fbt, fb->fbbase, offset - fb->fbbase, 482 prot, BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE); 483 return pa; 484 } 485 if (offset >= (fb->mmiobase & ~PAGE_MASK) && 486 offset <= ((fb->mmiobase + fb->mmiosize + PAGE_SIZE - 1) & ~PAGE_MASK)) { 487 pa = bus_space_mmap(fb->mmiot, fb->mmiobase, offset - fb->mmiobase, 488 prot, BUS_SPACE_MAP_LINEAR); 489 return pa; 490 } 491 if (offset >= (fb->iobase & ~PAGE_MASK) && 492 offset <= ((fb->iobase + fb->iosize + PAGE_SIZE - 1) & ~PAGE_MASK)) { 493 pa = bus_space_mmap(fb->iot, fb->iobase, offset - fb->iobase, 494 prot, BUS_SPACE_MAP_LINEAR); 495 return pa; 496 } 497 return -1; 498 } 499 500 void 501 sisfb_init_screen(void *cookie, struct vcons_screen *scr, 502 int existing, long *defattr) 503 { 504 struct sisfb_softc *sc = cookie; 505 struct sisfb *fb = sc->sc_fb; 506 struct rasops_info *ri = &scr->scr_ri; 507 508 ri->ri_depth = fb->fb_depth; 509 ri->ri_width = fb->fb_width; 510 ri->ri_height = fb->fb_height; 511 ri->ri_stride = fb->fb_stride; 512 ri->ri_flg = RI_CENTER | RI_FULLCLEAR; 513 514 ri->ri_bits = fb->fb_addr; 515 516 if (existing) { 517 ri->ri_flg |= RI_CLEAR; 518 } 519 520 rasops_init(ri, fb->fb_height / 8, fb->fb_width / 8); 521 ri->ri_caps = WSSCREEN_WSCOLORS; 522 rasops_reconfig(ri, fb->fb_height / ri->ri_font->fontheight, 523 fb->fb_width / ri->ri_font->fontwidth); 524 525 ri->ri_hw = scr; 526 } 527 528 529 /* 530 * Frame buffer initialization. 531 */ 532 533 int 534 sisfb_setup(struct sisfb *fb) 535 { 536 struct rasops_info *ri = &fb->vcs.scr_ri; 537 uint width, height, bpp; 538 bus_size_t fbaddr; 539 uint tmp; 540 541 /* 542 * Unlock access to extended registers. 543 */ 544 545 sisfb_seq_write(fb, 0x05, 0x86); 546 547 /* 548 * Try and figure out display settings. 549 */ 550 551 height = sisfb_crtc_read(fb, CRTC_VDE); 552 tmp = sisfb_crtc_read(fb, CRTC_OVERFLL); 553 if (ISSET(tmp, 1 << 1)) 554 height |= 1 << 8; 555 if (ISSET(tmp, 1 << 6)) 556 height |= 1 << 9; 557 tmp = sisfb_seq_read(fb, 0x0a); 558 if (ISSET(tmp, 1 << 1)) 559 height |= 1 << 10; 560 height++; 561 562 width = sisfb_crtc_read(fb, CRTC_HDISPLE); 563 tmp = sisfb_seq_read(fb, 0x0b); 564 if (ISSET(tmp, 1 << 2)) 565 width |= 1 << 8; 566 if (ISSET(tmp, 1 << 3)) 567 width |= 1 << 9; 568 width++; 569 width <<= 3; 570 571 #ifdef SIS_DEBUG 572 printf("height %d width %d\n", height, width); 573 #endif 574 575 fbaddr = sisfb_crtc_read(fb, CRTC_STARTADRL) | 576 (sisfb_crtc_read(fb, CRTC_STARTADRH) << 8) | 577 (sisfb_seq_read(fb, 0x0d) << 16) | 578 ((sisfb_seq_read(fb, 0x37) & 0x03) << 24); 579 fbaddr <<= 2; 580 #ifdef SIS_DEBUG 581 printf("FBADDR %lx\n", fbaddr); 582 #endif 583 584 tmp = sisfb_seq_read(fb, 0x06); 585 switch (tmp & 0x1c) { 586 case 0x00: 587 bpp = 8; 588 break; 589 case 0x04: 590 bpp = 15; 591 break; 592 case 0x08: 593 bpp = 16; 594 break; 595 case 0x10: 596 bpp = 32; 597 break; 598 default: 599 aprint_error("unknown bpp for 0x%x\n", tmp); 600 return EINVAL; 601 } 602 #ifdef SIS_DEBUG 603 printf("BPP %d\n", bpp); 604 #endif 605 606 fb->fb_width = ri->ri_width = width; 607 fb->fb_height = ri->ri_height = height; 608 fb->fb_depth = ri->ri_depth = bpp; 609 fb->fb_stride = ri->ri_stride = (ri->ri_width * ri->ri_depth) / 8; 610 ri->ri_flg = /* RI_CENTER | RI_CLEAR | RI_FULLCLEAR */ RI_CENTER | RI_NO_AUTO; 611 fb->fb_addr = ri->ri_bits = 612 (void *)((char *)bus_space_vaddr(fb->fbt, fb->fbh) + fbaddr); 613 ri->ri_hw = fb; 614 615 #ifdef SIS_DEBUG 616 printf("ri_bits %p\n", ri->ri_bits); 617 #endif 618 619 #ifdef __MIPSEL__ 620 /* swap B and R */ 621 switch (bpp) { 622 case 15: 623 ri->ri_rnum = 5; 624 ri->ri_rpos = 10; 625 ri->ri_gnum = 5; 626 ri->ri_gpos = 5; 627 ri->ri_bnum = 5; 628 ri->ri_bpos = 0; 629 break; 630 case 16: 631 ri->ri_rnum = 5; 632 ri->ri_rpos = 11; 633 ri->ri_gnum = 6; 634 ri->ri_gpos = 5; 635 ri->ri_bnum = 5; 636 ri->ri_bpos = 0; 637 break; 638 } 639 #endif 640 641 bcopy(rasops_cmap, fb->cmap, sizeof(fb->cmap)); 642 if (bpp == 8) { 643 sisfb_loadcmap(fb, 0, 256); 644 } 645 646 rasops_init(ri, 25, 80); 647 rasops_reconfig(ri, ri->ri_height / ri->ri_font->fontheight, 648 ri->ri_width / ri->ri_font->fontwidth); 649 650 fb->wsd.name = "std"; 651 fb->wsd.ncols = ri->ri_cols; 652 fb->wsd.nrows = ri->ri_rows; 653 fb->wsd.textops = &ri->ri_ops; 654 fb->wsd.fontwidth = ri->ri_font->fontwidth; 655 fb->wsd.fontheight = ri->ri_font->fontheight; 656 fb->wsd.capabilities = ri->ri_caps; 657 658 return 0; 659 } 660 661 /* 662 * Colormap handling routines. 663 */ 664 665 void 666 sisfb_loadcmap(struct sisfb *fb, int baseidx, int count) 667 { 668 uint8_t *cmap = fb->cmap + baseidx * 3; 669 670 bus_space_write_1(fb->iot, fb->ioh, DAC_ADDR, baseidx); 671 while (count-- != 0) { 672 bus_space_write_1(fb->iot, fb->ioh, DAC_DATA, *cmap++ >> 2); 673 bus_space_write_1(fb->iot, fb->ioh, DAC_DATA, *cmap++ >> 2); 674 bus_space_write_1(fb->iot, fb->ioh, DAC_DATA, *cmap++ >> 2); 675 } 676 } 677 678 int 679 sisfb_getcmap(uint8_t *cmap, struct wsdisplay_cmap *cm) 680 { 681 uint index = cm->index, count = cm->count, i; 682 uint8_t ramp[256], *dst, *src; 683 int rc; 684 685 if (index >= 256 || count > 256 - index) 686 return EINVAL; 687 688 index *= 3; 689 690 src = cmap + index; 691 dst = ramp; 692 for (i = 0; i < count; i++) 693 *dst++ = *src, src += 3; 694 rc = copyout(ramp, cm->red, count); 695 if (rc != 0) 696 return rc; 697 698 src = cmap + index + 1; 699 dst = ramp; 700 for (i = 0; i < count; i++) 701 *dst++ = *src, src += 3; 702 rc = copyout(ramp, cm->green, count); 703 if (rc != 0) 704 return rc; 705 706 src = cmap + index + 2; 707 dst = ramp; 708 for (i = 0; i < count; i++) 709 *dst++ = *src, src += 3; 710 rc = copyout(ramp, cm->blue, count); 711 if (rc != 0) 712 return rc; 713 714 return 0; 715 } 716 717 int 718 sisfb_putcmap(uint8_t *cmap, struct wsdisplay_cmap *cm) 719 { 720 uint index = cm->index, count = cm->count, i; 721 uint8_t ramp[256], *dst, *src; 722 int rc; 723 724 if (index >= 256 || count > 256 - index) 725 return EINVAL; 726 727 index *= 3; 728 729 rc = copyin(cm->red, ramp, count); 730 if (rc != 0) 731 return rc; 732 dst = cmap + index; 733 src = ramp; 734 for (i = 0; i < count; i++) 735 *dst = *src++, dst += 3; 736 737 rc = copyin(cm->green, ramp, count); 738 if (rc != 0) 739 return rc; 740 dst = cmap + index + 1; 741 src = ramp; 742 for (i = 0; i < count; i++) 743 *dst = *src++, dst += 3; 744 745 rc = copyin(cm->blue, ramp, count); 746 if (rc != 0) 747 return rc; 748 dst = cmap + index + 2; 749 src = ramp; 750 for (i = 0; i < count; i++) 751 *dst = *src++, dst += 3; 752 753 return 0; 754 } 755 756 /* 757 * Early console code 758 */ 759 760 int 761 sisfb_cnattach(bus_space_tag_t memt, bus_space_tag_t iot, 762 pci_chipset_tag_t pc, pcitag_t tag, pcireg_t id) 763 { 764 long defattr; 765 struct rasops_info * const ri = &sisfbcn.vcs.scr_ri; 766 int flags; 767 int rc; 768 769 /* filter out unrecognized devices */ 770 switch (id) { 771 default: 772 return ENODEV; 773 case PCI_ID_CODE(PCI_VENDOR_SIS, PCI_PRODUCT_SIS_315PRO_VGA): 774 break; 775 } 776 777 if (pci_mapreg_info(pc, tag, PCI_MAPREG_START, 778 PCI_MAPREG_TYPE_MEM, 779 &sisfbcn.fbbase, &sisfbcn.fbsize, &flags)) { 780 printf("sisfb can't map frame buffer\n"); 781 return ENODEV; 782 } 783 784 sisfbcn.fbt = memt; 785 rc = bus_space_map(memt, sisfbcn.fbbase, sisfbcn.fbsize, 786 BUS_SPACE_MAP_LINEAR, &sisfbcn.fbh); 787 #ifdef SIS_DEBUG 788 printf("sisfb_cnattach(memt, 0x%x rc %d\n", PCI_MAPREG_MEM_ADDR(bar), rc); 789 #endif 790 if (rc != 0) 791 return rc; 792 793 if (pci_mapreg_info(pc, tag, PCI_MAPREG_START + 4, 794 PCI_MAPREG_TYPE_MEM, 795 &sisfbcn.mmiobase, &sisfbcn.mmiosize, &flags)) { 796 printf("sisfb can't map mem space\n"); 797 return ENODEV; 798 } 799 sisfbcn.mmiot = memt; 800 rc = bus_space_map(memt, sisfbcn.mmiobase, sisfbcn.mmiosize, 801 BUS_SPACE_MAP_LINEAR, &sisfbcn.mmioh); 802 #ifdef SIS_DEBUG 803 printf("sisfb_cnattach(memt2, 0x%x rc %d\n", PCI_MAPREG_MEM_ADDR(bar), rc); 804 #endif 805 if (rc != 0) 806 return rc; 807 808 if (pci_mapreg_info(pc, tag, PCI_MAPREG_START + 8, 809 PCI_MAPREG_TYPE_IO, 810 &sisfbcn.iobase, &sisfbcn.iosize, &flags)) { 811 printf("sisfb can't map mem space\n"); 812 return ENODEV; 813 } 814 sisfbcn.iot = iot; 815 rc = bus_space_map(iot, sisfbcn.iobase, sisfbcn.iosize, 816 0, &sisfbcn.ioh); 817 #ifdef SIS_DEBUG 818 printf("sisfb_cnattach(iot, 0x%x rc %d\n", PCI_MAPREG_MEM_ADDR(bar), rc); 819 #endif 820 if (rc != 0) 821 return rc; 822 823 rc = sisfb_setup(&sisfbcn); 824 #ifdef SIS_DEBUG 825 printf("sisfb_setup %d %p\n", rc, sisfbcn.vcs.scr_ri.ri_hw); 826 #endif 827 if (rc != 0) 828 return rc; 829 830 ri->ri_ops.allocattr(ri, 0, ri->ri_rows - 1, 0, &defattr); 831 wsdisplay_preattach(&sisfbcn.wsd, ri, 0, 0, defattr); 832 833 return 0; 834 } 835