1 /*- 2 * Copyright (c) 2003 Peter Grehan 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/module.h> 33 #include <sys/bus.h> 34 #include <sys/kernel.h> 35 #include <sys/sysctl.h> 36 #include <sys/limits.h> 37 #include <sys/conf.h> 38 #include <sys/cons.h> 39 #include <sys/proc.h> 40 #include <sys/fcntl.h> 41 #include <sys/malloc.h> 42 #include <sys/fbio.h> 43 #include <sys/consio.h> 44 45 #include <machine/bus.h> 46 #include <machine/sc_machdep.h> 47 48 #include <sys/rman.h> 49 50 #include <dev/fb/fbreg.h> 51 #include <dev/syscons/syscons.h> 52 53 #include <dev/ofw/openfirm.h> 54 #include <dev/ofw/ofw_bus.h> 55 #include <dev/ofw/ofw_pci.h> 56 #include <powerpc/ofw/ofw_syscons.h> 57 58 static int ofwfb_ignore_mmap_checks; 59 SYSCTL_NODE(_hw, OID_AUTO, ofwfb, CTLFLAG_RD, 0, "ofwfb"); 60 SYSCTL_INT(_hw_ofwfb, OID_AUTO, relax_mmap, CTLFLAG_RW, 61 &ofwfb_ignore_mmap_checks, 0, "relax mmap bounds checking"); 62 63 extern u_char dflt_font_16[]; 64 extern u_char dflt_font_14[]; 65 extern u_char dflt_font_8[]; 66 67 static int ofwfb_configure(int flags); 68 69 static vi_probe_t ofwfb_probe; 70 static vi_init_t ofwfb_init; 71 static vi_get_info_t ofwfb_get_info; 72 static vi_query_mode_t ofwfb_query_mode; 73 static vi_set_mode_t ofwfb_set_mode; 74 static vi_save_font_t ofwfb_save_font; 75 static vi_load_font_t ofwfb_load_font; 76 static vi_show_font_t ofwfb_show_font; 77 static vi_save_palette_t ofwfb_save_palette; 78 static vi_load_palette_t ofwfb_load_palette; 79 static vi_set_border_t ofwfb_set_border; 80 static vi_save_state_t ofwfb_save_state; 81 static vi_load_state_t ofwfb_load_state; 82 static vi_set_win_org_t ofwfb_set_win_org; 83 static vi_read_hw_cursor_t ofwfb_read_hw_cursor; 84 static vi_set_hw_cursor_t ofwfb_set_hw_cursor; 85 static vi_set_hw_cursor_shape_t ofwfb_set_hw_cursor_shape; 86 static vi_blank_display_t ofwfb_blank_display; 87 static vi_mmap_t ofwfb_mmap; 88 static vi_ioctl_t ofwfb_ioctl; 89 static vi_clear_t ofwfb_clear; 90 static vi_fill_rect_t ofwfb_fill_rect; 91 static vi_bitblt_t ofwfb_bitblt; 92 static vi_diag_t ofwfb_diag; 93 static vi_save_cursor_palette_t ofwfb_save_cursor_palette; 94 static vi_load_cursor_palette_t ofwfb_load_cursor_palette; 95 static vi_copy_t ofwfb_copy; 96 static vi_putp_t ofwfb_putp; 97 static vi_putc_t ofwfb_putc; 98 static vi_puts_t ofwfb_puts; 99 static vi_putm_t ofwfb_putm; 100 101 static video_switch_t ofwfbvidsw = { 102 .probe = ofwfb_probe, 103 .init = ofwfb_init, 104 .get_info = ofwfb_get_info, 105 .query_mode = ofwfb_query_mode, 106 .set_mode = ofwfb_set_mode, 107 .save_font = ofwfb_save_font, 108 .load_font = ofwfb_load_font, 109 .show_font = ofwfb_show_font, 110 .save_palette = ofwfb_save_palette, 111 .load_palette = ofwfb_load_palette, 112 .set_border = ofwfb_set_border, 113 .save_state = ofwfb_save_state, 114 .load_state = ofwfb_load_state, 115 .set_win_org = ofwfb_set_win_org, 116 .read_hw_cursor = ofwfb_read_hw_cursor, 117 .set_hw_cursor = ofwfb_set_hw_cursor, 118 .set_hw_cursor_shape = ofwfb_set_hw_cursor_shape, 119 .blank_display = ofwfb_blank_display, 120 .mmap = ofwfb_mmap, 121 .ioctl = ofwfb_ioctl, 122 .clear = ofwfb_clear, 123 .fill_rect = ofwfb_fill_rect, 124 .bitblt = ofwfb_bitblt, 125 .diag = ofwfb_diag, 126 .save_cursor_palette = ofwfb_save_cursor_palette, 127 .load_cursor_palette = ofwfb_load_cursor_palette, 128 .copy = ofwfb_copy, 129 .putp = ofwfb_putp, 130 .putc = ofwfb_putc, 131 .puts = ofwfb_puts, 132 .putm = ofwfb_putm, 133 }; 134 135 /* 136 * bitmap depth-specific routines 137 */ 138 static vi_blank_display_t ofwfb_blank_display8; 139 static vi_putc_t ofwfb_putc8; 140 static vi_set_border_t ofwfb_set_border8; 141 142 static vi_blank_display_t ofwfb_blank_display32; 143 static vi_putc_t ofwfb_putc32; 144 static vi_set_border_t ofwfb_set_border32; 145 146 VIDEO_DRIVER(ofwfb, ofwfbvidsw, ofwfb_configure); 147 148 extern sc_rndr_sw_t txtrndrsw; 149 RENDERER(ofwfb, 0, txtrndrsw, gfb_set); 150 151 RENDERER_MODULE(ofwfb, gfb_set); 152 153 /* 154 * Define the iso6429-1983 colormap 155 */ 156 static struct { 157 uint8_t red; 158 uint8_t green; 159 uint8_t blue; 160 } ofwfb_cmap[16] = { /* # R G B Color */ 161 /* - - - - ----- */ 162 { 0x00, 0x00, 0x00 }, /* 0 0 0 0 Black */ 163 { 0x00, 0x00, 0xaa }, /* 1 0 0 2/3 Blue */ 164 { 0x00, 0xaa, 0x00 }, /* 2 0 2/3 0 Green */ 165 { 0x00, 0xaa, 0xaa }, /* 3 0 2/3 2/3 Cyan */ 166 { 0xaa, 0x00, 0x00 }, /* 4 2/3 0 0 Red */ 167 { 0xaa, 0x00, 0xaa }, /* 5 2/3 0 2/3 Magenta */ 168 { 0xaa, 0x55, 0x00 }, /* 6 2/3 1/3 0 Brown */ 169 { 0xaa, 0xaa, 0xaa }, /* 7 2/3 2/3 2/3 White */ 170 { 0x55, 0x55, 0x55 }, /* 8 1/3 1/3 1/3 Gray */ 171 { 0x55, 0x55, 0xff }, /* 9 1/3 1/3 1 Bright Blue */ 172 { 0x55, 0xff, 0x55 }, /* 10 1/3 1 1/3 Bright Green */ 173 { 0x55, 0xff, 0xff }, /* 11 1/3 1 1 Bright Cyan */ 174 { 0xff, 0x55, 0x55 }, /* 12 1 1/3 1/3 Bright Red */ 175 { 0xff, 0x55, 0xff }, /* 13 1 1/3 1 Bright Magenta */ 176 { 0xff, 0xff, 0x80 }, /* 14 1 1 1/3 Bright Yellow */ 177 { 0xff, 0xff, 0xff } /* 15 1 1 1 Bright White */ 178 }; 179 180 #define TODO printf("%s: unimplemented\n", __func__) 181 182 static u_int16_t ofwfb_static_window[ROW*COL]; 183 184 static struct ofwfb_softc ofwfb_softc; 185 186 static __inline int 187 ofwfb_background(uint8_t attr) 188 { 189 return (attr >> 4); 190 } 191 192 static __inline int 193 ofwfb_foreground(uint8_t attr) 194 { 195 return (attr & 0x0f); 196 } 197 198 static u_int 199 ofwfb_pix32(int attr) 200 { 201 u_int retval; 202 203 retval = (ofwfb_cmap[attr].blue << 16) | 204 (ofwfb_cmap[attr].green << 8) | 205 ofwfb_cmap[attr].red; 206 207 return (retval); 208 } 209 210 static int 211 ofwfb_configure(int flags) 212 { 213 struct ofwfb_softc *sc; 214 phandle_t chosen; 215 ihandle_t stdout; 216 phandle_t node; 217 int depth; 218 int disable; 219 int len; 220 char type[16]; 221 static int done = 0; 222 223 disable = 0; 224 TUNABLE_INT_FETCH("hw.syscons.disable", &disable); 225 if (disable != 0) 226 return (0); 227 228 if (done != 0) 229 return (0); 230 done = 1; 231 232 sc = &ofwfb_softc; 233 234 chosen = OF_finddevice("/chosen"); 235 OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)); 236 node = OF_instance_to_package(stdout); 237 if (node == -1) { 238 /* 239 * The "/chosen/stdout" does not exist try 240 * using "screen" directly. 241 */ 242 node = OF_finddevice("screen"); 243 } 244 OF_getprop(node, "device_type", type, sizeof(type)); 245 if (strcmp(type, "display") != 0) 246 return (0); 247 248 /* Only support 8 and 32-bit framebuffers */ 249 OF_getprop(node, "depth", &depth, sizeof(depth)); 250 if (depth == 8) { 251 sc->sc_blank = ofwfb_blank_display8; 252 sc->sc_putc = ofwfb_putc8; 253 sc->sc_set_border = ofwfb_set_border8; 254 } else if (depth == 32) { 255 sc->sc_blank = ofwfb_blank_display32; 256 sc->sc_putc = ofwfb_putc32; 257 sc->sc_set_border = ofwfb_set_border32; 258 } else 259 return (0); 260 261 sc->sc_depth = depth; 262 sc->sc_node = node; 263 sc->sc_console = 1; 264 OF_getprop(node, "height", &sc->sc_height, sizeof(sc->sc_height)); 265 OF_getprop(node, "width", &sc->sc_width, sizeof(sc->sc_width)); 266 OF_getprop(node, "linebytes", &sc->sc_stride, sizeof(sc->sc_stride)); 267 268 /* 269 * XXX the physical address of the frame buffer is assumed to be 270 * BAT-mapped so it can be accessed directly 271 */ 272 OF_getprop(node, "address", &sc->sc_addr, sizeof(sc->sc_addr)); 273 274 /* 275 * Get the PCI addresses of the adapter. The node may be the 276 * child of the PCI device: in that case, try the parent for 277 * the assigned-addresses property. 278 */ 279 len = OF_getprop(node, "assigned-addresses", sc->sc_pciaddrs, 280 sizeof(sc->sc_pciaddrs)); 281 if (len == -1) { 282 len = OF_getprop(OF_parent(node), "assigned-addresses", sc->sc_pciaddrs, 283 sizeof(sc->sc_pciaddrs)); 284 } 285 286 if (len != -1) { 287 sc->sc_num_pciaddrs = len / sizeof(struct ofw_pci_register); 288 } 289 290 ofwfb_init(0, &sc->sc_va, 0); 291 292 return (0); 293 } 294 295 static int 296 ofwfb_probe(int unit, video_adapter_t **adp, void *arg, int flags) 297 { 298 TODO; 299 return (0); 300 } 301 302 static int 303 ofwfb_init(int unit, video_adapter_t *adp, int flags) 304 { 305 struct ofwfb_softc *sc; 306 video_info_t *vi; 307 char name[64]; 308 ihandle_t ih; 309 int i; 310 int cborder; 311 int font_height; 312 int retval; 313 314 sc = (struct ofwfb_softc *)adp; 315 vi = &adp->va_info; 316 317 vid_init_struct(adp, "ofwfb", -1, unit); 318 319 if (sc->sc_depth == 8) { 320 /* 321 * Install the ISO6429 colormap - older OFW systems 322 * don't do this by default 323 */ 324 memset(name, 0, sizeof(name)); 325 OF_package_to_path(sc->sc_node, name, sizeof(name)); 326 ih = OF_open(name); 327 for (i = 0; i < 16; i++) { 328 OF_call_method("color!", ih, 4, 1, 329 ofwfb_cmap[i].red, 330 ofwfb_cmap[i].green, 331 ofwfb_cmap[i].blue, 332 i, 333 &retval); 334 } 335 } 336 337 /* The default font size can be overridden by loader */ 338 font_height = 16; 339 TUNABLE_INT_FETCH("hw.syscons.fsize", &font_height); 340 if (font_height == 8) { 341 sc->sc_font = dflt_font_8; 342 sc->sc_font_height = 8; 343 } else if (font_height == 14) { 344 sc->sc_font = dflt_font_14; 345 sc->sc_font_height = 14; 346 } else { 347 /* default is 8x16 */ 348 sc->sc_font = dflt_font_16; 349 sc->sc_font_height = 16; 350 } 351 352 /* The user can set a border in chars - default is 1 char width */ 353 cborder = 1; 354 TUNABLE_INT_FETCH("hw.syscons.border", &cborder); 355 356 vi->vi_cheight = sc->sc_font_height; 357 vi->vi_width = sc->sc_width/8 - 2*cborder; 358 vi->vi_height = sc->sc_height/sc->sc_font_height - 2*cborder; 359 vi->vi_cwidth = 8; 360 361 /* 362 * Clamp width/height to syscons maximums 363 */ 364 if (vi->vi_width > COL) 365 vi->vi_width = COL; 366 if (vi->vi_height > ROW) 367 vi->vi_height = ROW; 368 369 sc->sc_xmargin = (sc->sc_width - (vi->vi_width * vi->vi_cwidth)) / 2; 370 sc->sc_ymargin = (sc->sc_height - (vi->vi_height * vi->vi_cheight))/2; 371 372 /* 373 * Avoid huge amounts of conditional code in syscons by 374 * defining a dummy h/w text display buffer. 375 */ 376 adp->va_window = (vm_offset_t) ofwfb_static_window; 377 378 /* Enable future font-loading and flag color support */ 379 adp->va_flags |= V_ADP_FONT | V_ADP_COLOR; 380 381 ofwfb_blank_display(&sc->sc_va, V_DISPLAY_ON); 382 383 ofwfb_set_mode(&sc->sc_va, 0); 384 385 vid_register(&sc->sc_va); 386 387 return (0); 388 } 389 390 static int 391 ofwfb_get_info(video_adapter_t *adp, int mode, video_info_t *info) 392 { 393 bcopy(&adp->va_info, info, sizeof(*info)); 394 return (0); 395 } 396 397 static int 398 ofwfb_query_mode(video_adapter_t *adp, video_info_t *info) 399 { 400 TODO; 401 return (0); 402 } 403 404 static int 405 ofwfb_set_mode(video_adapter_t *adp, int mode) 406 { 407 408 return (0); 409 } 410 411 static int 412 ofwfb_save_font(video_adapter_t *adp, int page, int size, int width, 413 u_char *data, int c, int count) 414 { 415 TODO; 416 return (0); 417 } 418 419 static int 420 ofwfb_load_font(video_adapter_t *adp, int page, int size, int width, 421 u_char *data, int c, int count) 422 { 423 struct ofwfb_softc *sc; 424 425 sc = (struct ofwfb_softc *)adp; 426 427 /* 428 * syscons code has already determined that current width/height 429 * are unchanged for this new font 430 */ 431 sc->sc_font = data; 432 return (0); 433 } 434 435 static int 436 ofwfb_show_font(video_adapter_t *adp, int page) 437 { 438 439 return (0); 440 } 441 442 static int 443 ofwfb_save_palette(video_adapter_t *adp, u_char *palette) 444 { 445 /* TODO; */ 446 return (0); 447 } 448 449 static int 450 ofwfb_load_palette(video_adapter_t *adp, u_char *palette) 451 { 452 /* TODO; */ 453 return (0); 454 } 455 456 static int 457 ofwfb_set_border8(video_adapter_t *adp, int border) 458 { 459 struct ofwfb_softc *sc; 460 int i, j; 461 uint8_t *addr; 462 uint8_t bground; 463 464 sc = (struct ofwfb_softc *)adp; 465 466 bground = ofwfb_background(border); 467 468 /* Set top margin */ 469 addr = (uint8_t *) sc->sc_addr; 470 for (i = 0; i < sc->sc_ymargin; i++) { 471 for (j = 0; j < sc->sc_width; j++) { 472 *(addr + j) = bground; 473 } 474 addr += sc->sc_stride; 475 } 476 477 /* bottom margin */ 478 addr = (uint8_t *) sc->sc_addr + (sc->sc_height - sc->sc_ymargin)*sc->sc_stride; 479 for (i = 0; i < sc->sc_ymargin; i++) { 480 for (j = 0; j < sc->sc_width; j++) { 481 *(addr + j) = bground; 482 } 483 addr += sc->sc_stride; 484 } 485 486 /* remaining left and right borders */ 487 addr = (uint8_t *) sc->sc_addr + sc->sc_ymargin*sc->sc_stride; 488 for (i = 0; i < sc->sc_height - 2*sc->sc_xmargin; i++) { 489 for (j = 0; j < sc->sc_xmargin; j++) { 490 *(addr + j) = bground; 491 *(addr + j + sc->sc_width - sc->sc_xmargin) = bground; 492 } 493 addr += sc->sc_stride; 494 } 495 496 return (0); 497 } 498 499 static int 500 ofwfb_set_border32(video_adapter_t *adp, int border) 501 { 502 /* XXX Be lazy for now and blank entire screen */ 503 return (ofwfb_blank_display32(adp, border)); 504 } 505 506 static int 507 ofwfb_set_border(video_adapter_t *adp, int border) 508 { 509 struct ofwfb_softc *sc; 510 511 sc = (struct ofwfb_softc *)adp; 512 513 return ((*sc->sc_set_border)(adp, border)); 514 } 515 516 static int 517 ofwfb_save_state(video_adapter_t *adp, void *p, size_t size) 518 { 519 TODO; 520 return (0); 521 } 522 523 static int 524 ofwfb_load_state(video_adapter_t *adp, void *p) 525 { 526 TODO; 527 return (0); 528 } 529 530 static int 531 ofwfb_set_win_org(video_adapter_t *adp, off_t offset) 532 { 533 TODO; 534 return (0); 535 } 536 537 static int 538 ofwfb_read_hw_cursor(video_adapter_t *adp, int *col, int *row) 539 { 540 *col = 0; 541 *row = 0; 542 543 return (0); 544 } 545 546 static int 547 ofwfb_set_hw_cursor(video_adapter_t *adp, int col, int row) 548 { 549 550 return (0); 551 } 552 553 static int 554 ofwfb_set_hw_cursor_shape(video_adapter_t *adp, int base, int height, 555 int celsize, int blink) 556 { 557 return (0); 558 } 559 560 static int 561 ofwfb_blank_display8(video_adapter_t *adp, int mode) 562 { 563 struct ofwfb_softc *sc; 564 int i; 565 uint8_t *addr; 566 567 sc = (struct ofwfb_softc *)adp; 568 addr = (uint8_t *) sc->sc_addr; 569 570 /* Could be done a lot faster e.g. 32-bits, or Altivec'd */ 571 for (i = 0; i < sc->sc_stride*sc->sc_height; i++) 572 *(addr + i) = ofwfb_background(SC_NORM_ATTR); 573 574 return (0); 575 } 576 577 static int 578 ofwfb_blank_display32(video_adapter_t *adp, int mode) 579 { 580 struct ofwfb_softc *sc; 581 int i; 582 uint32_t *addr; 583 584 sc = (struct ofwfb_softc *)adp; 585 addr = (uint32_t *) sc->sc_addr; 586 587 for (i = 0; i < (sc->sc_stride/4)*sc->sc_height; i++) 588 *(addr + i) = ofwfb_pix32(ofwfb_background(SC_NORM_ATTR)); 589 590 return (0); 591 } 592 593 static int 594 ofwfb_blank_display(video_adapter_t *adp, int mode) 595 { 596 struct ofwfb_softc *sc; 597 598 sc = (struct ofwfb_softc *)adp; 599 600 return ((*sc->sc_blank)(adp, mode)); 601 } 602 603 static int 604 ofwfb_mmap(video_adapter_t *adp, vm_offset_t offset, vm_paddr_t *paddr, 605 int prot) 606 { 607 struct ofwfb_softc *sc; 608 int i; 609 610 sc = (struct ofwfb_softc *)adp; 611 612 if (sc->sc_num_pciaddrs == 0) 613 return (ENOMEM); 614 615 /* 616 * Hack for Radeon... 617 */ 618 if (ofwfb_ignore_mmap_checks) { 619 *paddr = offset; 620 return (0); 621 } 622 623 /* 624 * Make sure the requested address lies within the PCI device's assigned addrs 625 */ 626 for (i = 0; i < sc->sc_num_pciaddrs; i++) 627 if (offset >= sc->sc_pciaddrs[i].phys_lo && 628 offset < (sc->sc_pciaddrs[i].phys_lo + sc->sc_pciaddrs[i].size_lo)) { 629 *paddr = offset; 630 return (0); 631 } 632 633 /* 634 * This might be a legacy VGA mem request: if so, just point it at the 635 * framebuffer, since it shouldn't be touched 636 */ 637 if (offset < sc->sc_stride*sc->sc_height) { 638 *paddr = sc->sc_addr + offset; 639 return (0); 640 } 641 642 return (EINVAL); 643 } 644 645 static int 646 ofwfb_ioctl(video_adapter_t *adp, u_long cmd, caddr_t data) 647 { 648 649 return (0); 650 } 651 652 static int 653 ofwfb_clear(video_adapter_t *adp) 654 { 655 TODO; 656 return (0); 657 } 658 659 static int 660 ofwfb_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy) 661 { 662 TODO; 663 return (0); 664 } 665 666 static int 667 ofwfb_bitblt(video_adapter_t *adp, ...) 668 { 669 TODO; 670 return (0); 671 } 672 673 static int 674 ofwfb_diag(video_adapter_t *adp, int level) 675 { 676 TODO; 677 return (0); 678 } 679 680 static int 681 ofwfb_save_cursor_palette(video_adapter_t *adp, u_char *palette) 682 { 683 TODO; 684 return (0); 685 } 686 687 static int 688 ofwfb_load_cursor_palette(video_adapter_t *adp, u_char *palette) 689 { 690 TODO; 691 return (0); 692 } 693 694 static int 695 ofwfb_copy(video_adapter_t *adp, vm_offset_t src, vm_offset_t dst, int n) 696 { 697 TODO; 698 return (0); 699 } 700 701 static int 702 ofwfb_putp(video_adapter_t *adp, vm_offset_t off, uint32_t p, uint32_t a, 703 int size, int bpp, int bit_ltor, int byte_ltor) 704 { 705 TODO; 706 return (0); 707 } 708 709 static int 710 ofwfb_putc8(video_adapter_t *adp, vm_offset_t off, uint8_t c, uint8_t a) 711 { 712 struct ofwfb_softc *sc; 713 int row; 714 int col; 715 int i; 716 uint32_t *addr; 717 u_char *p, fg, bg; 718 union { 719 uint32_t l; 720 uint8_t c[4]; 721 } ch1, ch2; 722 723 724 sc = (struct ofwfb_softc *)adp; 725 row = (off / adp->va_info.vi_width) * adp->va_info.vi_cheight; 726 col = (off % adp->va_info.vi_width) * adp->va_info.vi_cwidth; 727 p = sc->sc_font + c*sc->sc_font_height; 728 addr = (u_int32_t *)((int)sc->sc_addr 729 + (row + sc->sc_ymargin)*sc->sc_stride 730 + col + sc->sc_xmargin); 731 732 fg = ofwfb_foreground(a); 733 bg = ofwfb_background(a); 734 735 for (i = 0; i < sc->sc_font_height; i++) { 736 u_char fline = p[i]; 737 738 /* 739 * Assume that there is more background than foreground 740 * in characters and init accordingly 741 */ 742 ch1.l = ch2.l = (bg << 24) | (bg << 16) | (bg << 8) | bg; 743 744 /* 745 * Calculate 2 x 4-chars at a time, and then 746 * write these out. 747 */ 748 if (fline & 0x80) ch1.c[0] = fg; 749 if (fline & 0x40) ch1.c[1] = fg; 750 if (fline & 0x20) ch1.c[2] = fg; 751 if (fline & 0x10) ch1.c[3] = fg; 752 753 if (fline & 0x08) ch2.c[0] = fg; 754 if (fline & 0x04) ch2.c[1] = fg; 755 if (fline & 0x02) ch2.c[2] = fg; 756 if (fline & 0x01) ch2.c[3] = fg; 757 758 addr[0] = ch1.l; 759 addr[1] = ch2.l; 760 addr += (sc->sc_stride / sizeof(u_int32_t)); 761 } 762 763 return (0); 764 } 765 766 static int 767 ofwfb_putc32(video_adapter_t *adp, vm_offset_t off, uint8_t c, uint8_t a) 768 { 769 struct ofwfb_softc *sc; 770 int row; 771 int col; 772 int i, j, k; 773 uint32_t *addr; 774 u_char *p; 775 776 sc = (struct ofwfb_softc *)adp; 777 row = (off / adp->va_info.vi_width) * adp->va_info.vi_cheight; 778 col = (off % adp->va_info.vi_width) * adp->va_info.vi_cwidth; 779 p = sc->sc_font + c*sc->sc_font_height; 780 addr = (uint32_t *)sc->sc_addr 781 + (row + sc->sc_ymargin)*(sc->sc_stride/4) 782 + col + sc->sc_xmargin; 783 784 for (i = 0; i < sc->sc_font_height; i++) { 785 for (j = 0, k = 7; j < 8; j++, k--) { 786 if ((p[i] & (1 << k)) == 0) 787 *(addr + j) = ofwfb_pix32(ofwfb_background(a)); 788 else 789 *(addr + j) = ofwfb_pix32(ofwfb_foreground(a)); 790 } 791 addr += (sc->sc_stride/4); 792 } 793 794 return (0); 795 } 796 797 static int 798 ofwfb_putc(video_adapter_t *adp, vm_offset_t off, uint8_t c, uint8_t a) 799 { 800 struct ofwfb_softc *sc; 801 802 sc = (struct ofwfb_softc *)adp; 803 804 return ((*sc->sc_putc)(adp, off, c, a)); 805 } 806 807 static int 808 ofwfb_puts(video_adapter_t *adp, vm_offset_t off, u_int16_t *s, int len) 809 { 810 int i; 811 812 for (i = 0; i < len; i++) { 813 ofwfb_putc(adp, off + i, s[i] & 0xff, (s[i] & 0xff00) >> 8); 814 } 815 return (0); 816 } 817 818 static int 819 ofwfb_putm(video_adapter_t *adp, int x, int y, uint8_t *pixel_image, 820 uint32_t pixel_mask, int size, int width) 821 { 822 struct ofwfb_softc *sc; 823 824 sc = (struct ofwfb_softc *)adp; 825 826 /* put mouse */ 827 828 return (0); 829 } 830 831 /* 832 * Define the syscons nexus device attachment 833 */ 834 static void 835 ofwfb_scidentify(driver_t *driver, device_t parent) 836 { 837 device_t child; 838 839 /* 840 * Add with a priority guaranteed to make it last on 841 * the device list 842 */ 843 child = BUS_ADD_CHILD(parent, INT_MAX, SC_DRIVER_NAME, 0); 844 } 845 846 static int 847 ofwfb_scprobe(device_t dev) 848 { 849 /* This is a fake device, so make sure there is no OF node for it */ 850 if (ofw_bus_get_node(dev) != -1) 851 return ENXIO; 852 853 device_set_desc(dev, "System console"); 854 return (sc_probe_unit(device_get_unit(dev), 855 device_get_flags(dev) | SC_AUTODETECT_KBD)); 856 } 857 858 static int 859 ofwfb_scattach(device_t dev) 860 { 861 return (sc_attach_unit(device_get_unit(dev), 862 device_get_flags(dev) | SC_AUTODETECT_KBD)); 863 } 864 865 static device_method_t ofwfb_sc_methods[] = { 866 DEVMETHOD(device_identify, ofwfb_scidentify), 867 DEVMETHOD(device_probe, ofwfb_scprobe), 868 DEVMETHOD(device_attach, ofwfb_scattach), 869 { 0, 0 } 870 }; 871 872 static driver_t ofwfb_sc_driver = { 873 SC_DRIVER_NAME, 874 ofwfb_sc_methods, 875 sizeof(sc_softc_t), 876 }; 877 878 static devclass_t sc_devclass; 879 880 DRIVER_MODULE(sc, nexus, ofwfb_sc_driver, sc_devclass, 0, 0); 881 882 /* 883 * Define a stub keyboard driver in case one hasn't been 884 * compiled into the kernel 885 */ 886 #include <sys/kbio.h> 887 #include <dev/kbd/kbdreg.h> 888 889 static int dummy_kbd_configure(int flags); 890 891 keyboard_switch_t dummysw; 892 893 static int 894 dummy_kbd_configure(int flags) 895 { 896 897 return (0); 898 } 899 KEYBOARD_DRIVER(dummy, dummysw, dummy_kbd_configure); 900 901 /* 902 * Utility routines from <dev/fb/fbreg.h> 903 */ 904 void 905 ofwfb_bcopy(const void *s, void *d, size_t c) 906 { 907 bcopy(s, d, c); 908 } 909 910 void 911 ofwfb_bzero(void *d, size_t c) 912 { 913 bzero(d, c); 914 } 915 916 void 917 ofwfb_fillw(int pat, void *base, size_t cnt) 918 { 919 u_int16_t *bptr = base; 920 921 while (cnt--) 922 *bptr++ = pat; 923 } 924 925 u_int16_t 926 ofwfb_readw(u_int16_t *addr) 927 { 928 return (*addr); 929 } 930 931 void 932 ofwfb_writew(u_int16_t *addr, u_int16_t val) 933 { 934 *addr = val; 935 } 936