1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2011 Nathan Whitehorn 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 AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/kernel.h> 34 #include <sys/systm.h> 35 #include <sys/fbio.h> 36 37 #include <dev/vt/vt.h> 38 #include <dev/vt/hw/fb/vt_fb.h> 39 #include <dev/vt/colors/vt_termcolors.h> 40 41 #include <vm/vm.h> 42 #include <vm/pmap.h> 43 44 #include <machine/bus.h> 45 #ifdef __sparc64__ 46 #include <machine/bus_private.h> 47 #endif 48 #include <machine/cpu.h> 49 50 #include <dev/ofw/openfirm.h> 51 #include <dev/ofw/ofw_bus.h> 52 #include <dev/ofw/ofw_pci.h> 53 54 struct ofwfb_softc { 55 struct fb_info fb; 56 57 phandle_t sc_node; 58 ihandle_t sc_handle; 59 bus_space_tag_t sc_memt; 60 int iso_palette; 61 }; 62 63 static void ofwfb_initialize(struct vt_device *vd); 64 static vd_probe_t ofwfb_probe; 65 static vd_init_t ofwfb_init; 66 static vd_bitblt_text_t ofwfb_bitblt_text; 67 static vd_bitblt_bmp_t ofwfb_bitblt_bitmap; 68 69 static const struct vt_driver vt_ofwfb_driver = { 70 .vd_name = "ofwfb", 71 .vd_probe = ofwfb_probe, 72 .vd_init = ofwfb_init, 73 .vd_blank = vt_fb_blank, 74 .vd_bitblt_text = ofwfb_bitblt_text, 75 .vd_bitblt_bmp = ofwfb_bitblt_bitmap, 76 .vd_fb_ioctl = vt_fb_ioctl, 77 .vd_fb_mmap = vt_fb_mmap, 78 .vd_priority = VD_PRIORITY_GENERIC+1, 79 }; 80 81 static unsigned char ofw_colors[16] = { 82 /* See "16-color Text Extension" Open Firmware document, page 4 */ 83 0, 4, 2, 6, 1, 5, 3, 7, 84 8, 12, 10, 14, 9, 13, 11, 15 85 }; 86 87 static struct ofwfb_softc ofwfb_conssoftc; 88 VT_DRIVER_DECLARE(vt_ofwfb, vt_ofwfb_driver); 89 90 static int 91 ofwfb_probe(struct vt_device *vd) 92 { 93 phandle_t chosen, node; 94 ihandle_t stdout; 95 char type[64]; 96 97 chosen = OF_finddevice("/chosen"); 98 if (chosen == -1) 99 return (CN_DEAD); 100 101 node = -1; 102 if (OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)) == 103 sizeof(stdout)) 104 node = OF_instance_to_package(stdout); 105 if (node == -1) { 106 /* 107 * The "/chosen/stdout" does not exist try 108 * using "screen" directly. 109 */ 110 node = OF_finddevice("screen"); 111 } 112 OF_getprop(node, "device_type", type, sizeof(type)); 113 if (strcmp(type, "display") != 0) 114 return (CN_DEAD); 115 116 /* Looks OK... */ 117 return (CN_INTERNAL); 118 } 119 120 static void 121 ofwfb_bitblt_bitmap(struct vt_device *vd, const struct vt_window *vw, 122 const uint8_t *pattern, const uint8_t *mask, 123 unsigned int width, unsigned int height, 124 unsigned int x, unsigned int y, term_color_t fg, term_color_t bg) 125 { 126 struct fb_info *sc = vd->vd_softc; 127 u_long line; 128 uint32_t fgc, bgc; 129 int c, l; 130 uint8_t b, m; 131 union { 132 uint32_t l; 133 uint8_t c[4]; 134 } ch1, ch2; 135 136 #ifdef __powerpc__ 137 /* Deal with unmapped framebuffers */ 138 if (sc->fb_flags & FB_FLAG_NOWRITE) { 139 if (pmap_bootstrapped) { 140 sc->fb_flags &= ~FB_FLAG_NOWRITE; 141 ofwfb_initialize(vd); 142 vd->vd_driver->vd_blank(vd, TC_BLACK); 143 } else { 144 return; 145 } 146 } 147 #endif 148 149 fgc = sc->fb_cmap[fg]; 150 bgc = sc->fb_cmap[bg]; 151 b = m = 0; 152 153 if (((struct ofwfb_softc *)vd->vd_softc)->iso_palette) { 154 fg = ofw_colors[fg]; 155 bg = ofw_colors[bg]; 156 } 157 158 line = (sc->fb_stride * y) + x * sc->fb_bpp/8; 159 if (mask == NULL && sc->fb_bpp == 8 && (width % 8 == 0)) { 160 /* Don't try to put off screen pixels */ 161 if (((x + width) > vd->vd_width) || ((y + height) > 162 vd->vd_height)) 163 return; 164 165 for (; height > 0; height--) { 166 for (c = 0; c < width; c += 8) { 167 b = *pattern++; 168 169 /* 170 * Assume that there is more background than 171 * foreground in characters and init accordingly 172 */ 173 ch1.l = ch2.l = (bg << 24) | (bg << 16) | 174 (bg << 8) | bg; 175 176 /* 177 * Calculate 2 x 4-chars at a time, and then 178 * write these out. 179 */ 180 if (b & 0x80) ch1.c[0] = fg; 181 if (b & 0x40) ch1.c[1] = fg; 182 if (b & 0x20) ch1.c[2] = fg; 183 if (b & 0x10) ch1.c[3] = fg; 184 185 if (b & 0x08) ch2.c[0] = fg; 186 if (b & 0x04) ch2.c[1] = fg; 187 if (b & 0x02) ch2.c[2] = fg; 188 if (b & 0x01) ch2.c[3] = fg; 189 190 *(uint32_t *)(sc->fb_vbase + line + c) = ch1.l; 191 *(uint32_t *)(sc->fb_vbase + line + c + 4) = 192 ch2.l; 193 } 194 line += sc->fb_stride; 195 } 196 } else { 197 for (l = 0; 198 l < height && y + l < vw->vw_draw_area.tr_end.tp_row; 199 l++) { 200 for (c = 0; 201 c < width && x + c < vw->vw_draw_area.tr_end.tp_col; 202 c++) { 203 if (c % 8 == 0) 204 b = *pattern++; 205 else 206 b <<= 1; 207 if (mask != NULL) { 208 if (c % 8 == 0) 209 m = *mask++; 210 else 211 m <<= 1; 212 /* Skip pixel write, if mask not set. */ 213 if ((m & 0x80) == 0) 214 continue; 215 } 216 switch(sc->fb_bpp) { 217 case 8: 218 *(uint8_t *)(sc->fb_vbase + line + c) = 219 b & 0x80 ? fg : bg; 220 break; 221 case 32: 222 *(uint32_t *)(sc->fb_vbase + line + 4*c) 223 = (b & 0x80) ? fgc : bgc; 224 break; 225 default: 226 /* panic? */ 227 break; 228 } 229 } 230 line += sc->fb_stride; 231 } 232 } 233 } 234 235 void 236 ofwfb_bitblt_text(struct vt_device *vd, const struct vt_window *vw, 237 const term_rect_t *area) 238 { 239 unsigned int col, row, x, y; 240 struct vt_font *vf; 241 term_char_t c; 242 term_color_t fg, bg; 243 const uint8_t *pattern; 244 245 vf = vw->vw_font; 246 247 for (row = area->tr_begin.tp_row; row < area->tr_end.tp_row; ++row) { 248 for (col = area->tr_begin.tp_col; col < area->tr_end.tp_col; 249 ++col) { 250 x = col * vf->vf_width + 251 vw->vw_draw_area.tr_begin.tp_col; 252 y = row * vf->vf_height + 253 vw->vw_draw_area.tr_begin.tp_row; 254 255 c = VTBUF_GET_FIELD(&vw->vw_buf, row, col); 256 pattern = vtfont_lookup(vf, c); 257 vt_determine_colors(c, 258 VTBUF_ISCURSOR(&vw->vw_buf, row, col), &fg, &bg); 259 260 ofwfb_bitblt_bitmap(vd, vw, 261 pattern, NULL, vf->vf_width, vf->vf_height, 262 x, y, fg, bg); 263 } 264 } 265 266 #ifndef SC_NO_CUTPASTE 267 if (!vd->vd_mshown) 268 return; 269 270 term_rect_t drawn_area; 271 272 drawn_area.tr_begin.tp_col = area->tr_begin.tp_col * vf->vf_width; 273 drawn_area.tr_begin.tp_row = area->tr_begin.tp_row * vf->vf_height; 274 drawn_area.tr_end.tp_col = area->tr_end.tp_col * vf->vf_width; 275 drawn_area.tr_end.tp_row = area->tr_end.tp_row * vf->vf_height; 276 277 if (vt_is_cursor_in_area(vd, &drawn_area)) { 278 ofwfb_bitblt_bitmap(vd, vw, 279 vd->vd_mcursor->map, vd->vd_mcursor->mask, 280 vd->vd_mcursor->width, vd->vd_mcursor->height, 281 vd->vd_mx_drawn + vw->vw_draw_area.tr_begin.tp_col, 282 vd->vd_my_drawn + vw->vw_draw_area.tr_begin.tp_row, 283 vd->vd_mcursor_fg, vd->vd_mcursor_bg); 284 } 285 #endif 286 } 287 288 static void 289 ofwfb_initialize(struct vt_device *vd) 290 { 291 struct ofwfb_softc *sc = vd->vd_softc; 292 int i, err; 293 cell_t retval; 294 uint32_t oldpix; 295 296 sc->fb.fb_cmsize = 16; 297 298 if (sc->fb.fb_flags & FB_FLAG_NOWRITE) 299 return; 300 301 /* 302 * Set up the color map 303 */ 304 305 sc->iso_palette = 0; 306 switch (sc->fb.fb_bpp) { 307 case 8: 308 vt_generate_cons_palette(sc->fb.fb_cmap, COLOR_FORMAT_RGB, 255, 309 16, 255, 8, 255, 0); 310 311 for (i = 0; i < 16; i++) { 312 err = OF_call_method("color!", sc->sc_handle, 4, 1, 313 (cell_t)((sc->fb.fb_cmap[i] >> 16) & 0xff), 314 (cell_t)((sc->fb.fb_cmap[i] >> 8) & 0xff), 315 (cell_t)((sc->fb.fb_cmap[i] >> 0) & 0xff), 316 (cell_t)i, &retval); 317 if (err) 318 break; 319 } 320 if (i != 16) 321 sc->iso_palette = 1; 322 323 break; 324 325 case 32: 326 /* 327 * We bypass the usual bus_space_() accessors here, mostly 328 * for performance reasons. In particular, we don't want 329 * any barrier operations that may be performed and handle 330 * endianness slightly different. Figure out the host-view 331 * endianness of the frame buffer. 332 */ 333 oldpix = bus_space_read_4(sc->sc_memt, sc->fb.fb_vbase, 0); 334 bus_space_write_4(sc->sc_memt, sc->fb.fb_vbase, 0, 0xff000000); 335 if (*(uint8_t *)(sc->fb.fb_vbase) == 0xff) 336 vt_generate_cons_palette(sc->fb.fb_cmap, 337 COLOR_FORMAT_RGB, 255, 0, 255, 8, 255, 16); 338 else 339 vt_generate_cons_palette(sc->fb.fb_cmap, 340 COLOR_FORMAT_RGB, 255, 16, 255, 8, 255, 0); 341 bus_space_write_4(sc->sc_memt, sc->fb.fb_vbase, 0, oldpix); 342 break; 343 344 default: 345 panic("Unknown color space depth %d", sc->fb.fb_bpp); 346 break; 347 } 348 } 349 350 static int 351 ofwfb_init(struct vt_device *vd) 352 { 353 struct ofwfb_softc *sc; 354 char type[64]; 355 phandle_t chosen; 356 phandle_t node; 357 uint32_t depth, height, width, stride; 358 uint32_t fb_phys; 359 int i, len; 360 #ifdef __sparc64__ 361 static struct bus_space_tag ofwfb_memt[1]; 362 bus_addr_t phys; 363 int space; 364 #endif 365 366 /* Initialize softc */ 367 vd->vd_softc = sc = &ofwfb_conssoftc; 368 369 chosen = OF_finddevice("/chosen"); 370 OF_getprop(chosen, "stdout", &sc->sc_handle, sizeof(ihandle_t)); 371 node = OF_instance_to_package(sc->sc_handle); 372 if (node == -1) { 373 /* 374 * The "/chosen/stdout" does not exist try 375 * using "screen" directly. 376 */ 377 node = OF_finddevice("screen"); 378 sc->sc_handle = OF_open("screen"); 379 } 380 OF_getprop(node, "device_type", type, sizeof(type)); 381 if (strcmp(type, "display") != 0) 382 return (CN_DEAD); 383 384 /* Keep track of the OF node */ 385 sc->sc_node = node; 386 387 /* 388 * Try to use a 32-bit framebuffer if possible. This may be 389 * unimplemented and fail. That's fine -- it just means we are 390 * stuck with the defaults. 391 */ 392 OF_call_method("set-depth", sc->sc_handle, 1, 1, (cell_t)32, &i); 393 394 /* Make sure we have needed properties */ 395 if (OF_getproplen(node, "height") != sizeof(height) || 396 OF_getproplen(node, "width") != sizeof(width) || 397 OF_getproplen(node, "depth") != sizeof(depth)) 398 return (CN_DEAD); 399 400 /* Only support 8 and 32-bit framebuffers */ 401 OF_getprop(node, "depth", &depth, sizeof(depth)); 402 if (depth != 8 && depth != 32) 403 return (CN_DEAD); 404 sc->fb.fb_bpp = sc->fb.fb_depth = depth; 405 406 OF_getprop(node, "height", &height, sizeof(height)); 407 OF_getprop(node, "width", &width, sizeof(width)); 408 if (OF_getprop(node, "linebytes", &stride, sizeof(stride)) != 409 sizeof(stride)) 410 stride = width*depth/8; 411 412 sc->fb.fb_height = height; 413 sc->fb.fb_width = width; 414 sc->fb.fb_stride = stride; 415 sc->fb.fb_size = sc->fb.fb_height * sc->fb.fb_stride; 416 417 /* 418 * Grab the physical address of the framebuffer, and then map it 419 * into our memory space. If the MMU is not yet up, it will be 420 * remapped for us when relocation turns on. 421 */ 422 if (OF_getproplen(node, "address") == sizeof(fb_phys)) { 423 /* XXX We assume #address-cells is 1 at this point. */ 424 OF_getprop(node, "address", &fb_phys, sizeof(fb_phys)); 425 426 #if defined(__powerpc__) 427 sc->sc_memt = &bs_be_tag; 428 bus_space_map(sc->sc_memt, fb_phys, sc->fb.fb_size, 429 BUS_SPACE_MAP_PREFETCHABLE, &sc->fb.fb_vbase); 430 #elif defined(__sparc64__) 431 OF_decode_addr(node, 0, &space, &phys); 432 sc->sc_memt = &ofwfb_memt[0]; 433 sc->fb.fb_vbase = 434 sparc64_fake_bustag(space, fb_phys, sc->sc_memt); 435 #elif defined(__arm__) 436 sc->sc_memt = fdtbus_bs_tag; 437 bus_space_map(sc->sc_memt, sc->fb.fb_pbase, sc->fb.fb_size, 438 BUS_SPACE_MAP_PREFETCHABLE, 439 (bus_space_handle_t *)&sc->fb.fb_vbase); 440 #else 441 #error Unsupported platform! 442 #endif 443 444 sc->fb.fb_pbase = fb_phys; 445 } else { 446 /* 447 * Some IBM systems don't have an address property. Try to 448 * guess the framebuffer region from the assigned addresses. 449 * This is ugly, but there doesn't seem to be an alternative. 450 * Linux does the same thing. 451 */ 452 453 struct ofw_pci_register pciaddrs[8]; 454 int num_pciaddrs = 0; 455 456 /* 457 * Get the PCI addresses of the adapter, if present. The node 458 * may be the child of the PCI device: in that case, try the 459 * parent for the assigned-addresses property. 460 */ 461 len = OF_getprop(node, "assigned-addresses", pciaddrs, 462 sizeof(pciaddrs)); 463 if (len == -1) { 464 len = OF_getprop(OF_parent(node), "assigned-addresses", 465 pciaddrs, sizeof(pciaddrs)); 466 } 467 if (len == -1) 468 len = 0; 469 num_pciaddrs = len / sizeof(struct ofw_pci_register); 470 471 fb_phys = num_pciaddrs; 472 for (i = 0; i < num_pciaddrs; i++) { 473 /* If it is too small, not the framebuffer */ 474 if (pciaddrs[i].size_lo < sc->fb.fb_stride * height) 475 continue; 476 /* If it is not memory, it isn't either */ 477 if (!(pciaddrs[i].phys_hi & 478 OFW_PCI_PHYS_HI_SPACE_MEM32)) 479 continue; 480 481 /* This could be the framebuffer */ 482 fb_phys = i; 483 484 /* If it is prefetchable, it certainly is */ 485 if (pciaddrs[i].phys_hi & OFW_PCI_PHYS_HI_PREFETCHABLE) 486 break; 487 } 488 489 if (fb_phys == num_pciaddrs) /* No candidates found */ 490 return (CN_DEAD); 491 492 #if defined(__powerpc__) 493 OF_decode_addr(node, fb_phys, &sc->sc_memt, &sc->fb.fb_vbase, 494 NULL); 495 sc->fb.fb_pbase = sc->fb.fb_vbase & ~DMAP_BASE_ADDRESS; 496 #else 497 /* No ability to interpret assigned-addresses otherwise */ 498 return (CN_DEAD); 499 #endif 500 } 501 502 503 #if defined(__powerpc__) 504 /* 505 * If we are running on PowerPC in real mode (supported only on AIM 506 * CPUs), the frame buffer may be inaccessible (real mode does not 507 * necessarily cover all RAM) and may also be mapped with the wrong 508 * cache properties (all real mode accesses are assumed cacheable). 509 * Just don't write to it for the time being. 510 */ 511 if (!(cpu_features & PPC_FEATURE_BOOKE) && !(mfmsr() & PSL_DR)) 512 sc->fb.fb_flags |= FB_FLAG_NOWRITE; 513 #endif 514 ofwfb_initialize(vd); 515 vt_fb_init(vd); 516 517 return (CN_INTERNAL); 518 } 519 520