1 /*- 2 * Copyright (c) 2011 Nathan Whitehorn 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/kernel.h> 32 #include <sys/systm.h> 33 #include <sys/fbio.h> 34 35 #include <dev/vt/vt.h> 36 #include <dev/vt/hw/fb/vt_fb.h> 37 #include <dev/vt/colors/vt_termcolors.h> 38 39 #include <vm/vm.h> 40 #include <vm/pmap.h> 41 42 #include <machine/bus.h> 43 #ifdef __sparc64__ 44 #include <machine/bus_private.h> 45 #endif 46 47 #include <dev/ofw/openfirm.h> 48 #include <dev/ofw/ofw_bus.h> 49 #include <dev/ofw/ofw_pci.h> 50 51 struct ofwfb_softc { 52 struct fb_info fb; 53 54 phandle_t sc_node; 55 bus_space_tag_t sc_memt; 56 57 struct ofw_pci_register sc_pciaddrs[8]; 58 int sc_num_pciaddrs; 59 }; 60 61 static vd_probe_t ofwfb_probe; 62 static vd_init_t ofwfb_init; 63 static vd_bitbltchr_t ofwfb_bitbltchr; 64 static vd_fb_mmap_t ofwfb_mmap; 65 66 static const struct vt_driver vt_ofwfb_driver = { 67 .vd_name = "ofwfb", 68 .vd_probe = ofwfb_probe, 69 .vd_init = ofwfb_init, 70 .vd_blank = vt_fb_blank, 71 .vd_bitbltchr = ofwfb_bitbltchr, 72 .vd_maskbitbltchr = ofwfb_bitbltchr, 73 .vd_fb_mmap = ofwfb_mmap, 74 .vd_priority = VD_PRIORITY_GENERIC+1, 75 }; 76 77 static struct ofwfb_softc ofwfb_conssoftc; 78 VT_DRIVER_DECLARE(vt_ofwfb, vt_ofwfb_driver); 79 80 static int 81 ofwfb_probe(struct vt_device *vd) 82 { 83 phandle_t chosen, node; 84 ihandle_t stdout; 85 char type[64]; 86 87 chosen = OF_finddevice("/chosen"); 88 OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)); 89 node = OF_instance_to_package(stdout); 90 if (node == -1) { 91 /* 92 * The "/chosen/stdout" does not exist try 93 * using "screen" directly. 94 */ 95 node = OF_finddevice("screen"); 96 } 97 OF_getprop(node, "device_type", type, sizeof(type)); 98 if (strcmp(type, "display") != 0) 99 return (CN_DEAD); 100 101 /* Looks OK... */ 102 return (CN_INTERNAL); 103 } 104 105 static void 106 ofwfb_bitbltchr(struct vt_device *vd, const uint8_t *src, const uint8_t *mask, 107 int bpl, vt_axis_t top, vt_axis_t left, unsigned int width, 108 unsigned int height, term_color_t fg, term_color_t bg) 109 { 110 struct fb_info *sc = vd->vd_softc; 111 u_long line; 112 uint32_t fgc, bgc; 113 int c; 114 uint8_t b, m; 115 union { 116 uint32_t l; 117 uint8_t c[4]; 118 } ch1, ch2; 119 120 fgc = sc->fb_cmap[fg]; 121 bgc = sc->fb_cmap[bg]; 122 b = m = 0; 123 124 /* Don't try to put off screen pixels */ 125 if (((left + width) > vd->vd_width) || ((top + height) > 126 vd->vd_height)) 127 return; 128 129 line = (sc->fb_stride * top) + left * sc->fb_bpp/8; 130 if (mask == NULL && sc->fb_bpp == 8 && (width % 8 == 0)) { 131 for (; height > 0; height--) { 132 for (c = 0; c < width; c += 8) { 133 b = *src++; 134 135 /* 136 * Assume that there is more background than 137 * foreground in characters and init accordingly 138 */ 139 ch1.l = ch2.l = (bg << 24) | (bg << 16) | 140 (bg << 8) | bg; 141 142 /* 143 * Calculate 2 x 4-chars at a time, and then 144 * write these out. 145 */ 146 if (b & 0x80) ch1.c[0] = fg; 147 if (b & 0x40) ch1.c[1] = fg; 148 if (b & 0x20) ch1.c[2] = fg; 149 if (b & 0x10) ch1.c[3] = fg; 150 151 if (b & 0x08) ch2.c[0] = fg; 152 if (b & 0x04) ch2.c[1] = fg; 153 if (b & 0x02) ch2.c[2] = fg; 154 if (b & 0x01) ch2.c[3] = fg; 155 156 *(uint32_t *)(sc->fb_vbase + line + c) = ch1.l; 157 *(uint32_t *)(sc->fb_vbase + line + c + 4) = 158 ch2.l; 159 } 160 line += sc->fb_stride; 161 } 162 } else { 163 for (; height > 0; height--) { 164 for (c = 0; c < width; c++) { 165 if (c % 8 == 0) 166 b = *src++; 167 else 168 b <<= 1; 169 if (mask != NULL) { 170 if (c % 8 == 0) 171 m = *mask++; 172 else 173 m <<= 1; 174 /* Skip pixel write, if mask not set. */ 175 if ((m & 0x80) == 0) 176 continue; 177 } 178 switch(sc->fb_bpp) { 179 case 8: 180 *(uint8_t *)(sc->fb_vbase + line + c) = 181 b & 0x80 ? fg : bg; 182 break; 183 case 32: 184 *(uint32_t *)(sc->fb_vbase + line + 4*c) 185 = (b & 0x80) ? fgc : bgc; 186 break; 187 default: 188 /* panic? */ 189 break; 190 } 191 } 192 line += sc->fb_stride; 193 } 194 } 195 } 196 197 static void 198 ofwfb_initialize(struct vt_device *vd) 199 { 200 struct ofwfb_softc *sc = vd->vd_softc; 201 char name[64]; 202 ihandle_t ih; 203 int i; 204 cell_t retval; 205 uint32_t oldpix; 206 207 /* Open display device, thereby initializing it */ 208 memset(name, 0, sizeof(name)); 209 OF_package_to_path(sc->sc_node, name, sizeof(name)); 210 ih = OF_open(name); 211 212 /* 213 * Set up the color map 214 */ 215 216 switch (sc->fb.fb_bpp) { 217 case 8: 218 vt_generate_vga_palette(sc->fb.fb_cmap, COLOR_FORMAT_RGB, 255, 219 0, 255, 8, 255, 16); 220 221 for (i = 0; i < 16; i++) { 222 OF_call_method("color!", ih, 4, 1, 223 (cell_t)((sc->fb.fb_cmap[i] >> 16) & 0xff), 224 (cell_t)((sc->fb.fb_cmap[i] >> 8) & 0xff), 225 (cell_t)((sc->fb.fb_cmap[i] >> 0) & 0xff), 226 (cell_t)i, &retval); 227 } 228 break; 229 230 case 32: 231 /* 232 * We bypass the usual bus_space_() accessors here, mostly 233 * for performance reasons. In particular, we don't want 234 * any barrier operations that may be performed and handle 235 * endianness slightly different. Figure out the host-view 236 * endianness of the frame buffer. 237 */ 238 oldpix = bus_space_read_4(sc->sc_memt, sc->fb.fb_vbase, 0); 239 bus_space_write_4(sc->sc_memt, sc->fb.fb_vbase, 0, 0xff000000); 240 if (*(uint8_t *)(sc->fb.fb_vbase) == 0xff) 241 vt_generate_vga_palette(sc->fb.fb_cmap, 242 COLOR_FORMAT_RGB, 255, 16, 255, 8, 255, 0); 243 else 244 vt_generate_vga_palette(sc->fb.fb_cmap, 245 COLOR_FORMAT_RGB, 255, 0, 255, 8, 255, 16); 246 bus_space_write_4(sc->sc_memt, sc->fb.fb_vbase, 0, oldpix); 247 break; 248 249 default: 250 panic("Unknown color space depth %d", sc->fb.fb_bpp); 251 break; 252 } 253 254 sc->fb.fb_cmsize = 16; 255 } 256 257 static int 258 ofwfb_init(struct vt_device *vd) 259 { 260 struct ofwfb_softc *sc; 261 char type[64]; 262 phandle_t chosen; 263 ihandle_t stdout; 264 phandle_t node; 265 uint32_t depth, height, width, stride; 266 uint32_t fb_phys; 267 int i, len; 268 #ifdef __sparc64__ 269 static struct bus_space_tag ofwfb_memt[1]; 270 bus_addr_t phys; 271 int space; 272 #endif 273 274 /* Initialize softc */ 275 vd->vd_softc = sc = &ofwfb_conssoftc; 276 277 chosen = OF_finddevice("/chosen"); 278 OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)); 279 node = OF_instance_to_package(stdout); 280 if (node == -1) { 281 /* 282 * The "/chosen/stdout" does not exist try 283 * using "screen" directly. 284 */ 285 node = OF_finddevice("screen"); 286 } 287 OF_getprop(node, "device_type", type, sizeof(type)); 288 if (strcmp(type, "display") != 0) 289 return (CN_DEAD); 290 291 /* Keep track of the OF node */ 292 sc->sc_node = node; 293 294 /* Make sure we have needed properties */ 295 if (OF_getproplen(node, "height") != sizeof(height) || 296 OF_getproplen(node, "width") != sizeof(width) || 297 OF_getproplen(node, "depth") != sizeof(depth) || 298 OF_getproplen(node, "linebytes") != sizeof(sc->fb.fb_stride)) 299 return (CN_DEAD); 300 301 /* Only support 8 and 32-bit framebuffers */ 302 OF_getprop(node, "depth", &depth, sizeof(depth)); 303 if (depth != 8 && depth != 32) 304 return (CN_DEAD); 305 sc->fb.fb_bpp = depth; 306 307 OF_getprop(node, "height", &height, sizeof(height)); 308 OF_getprop(node, "width", &width, sizeof(width)); 309 OF_getprop(node, "linebytes", &stride, sizeof(stride)); 310 311 sc->fb.fb_height = height; 312 sc->fb.fb_width = width; 313 sc->fb.fb_stride = stride; 314 sc->fb.fb_size = sc->fb.fb_height * sc->fb.fb_stride; 315 316 /* 317 * Get the PCI addresses of the adapter, if present. The node may be the 318 * child of the PCI device: in that case, try the parent for 319 * the assigned-addresses property. 320 */ 321 len = OF_getprop(node, "assigned-addresses", sc->sc_pciaddrs, 322 sizeof(sc->sc_pciaddrs)); 323 if (len == -1) { 324 len = OF_getprop(OF_parent(node), "assigned-addresses", 325 sc->sc_pciaddrs, sizeof(sc->sc_pciaddrs)); 326 } 327 if (len == -1) 328 len = 0; 329 sc->sc_num_pciaddrs = len / sizeof(struct ofw_pci_register); 330 331 /* 332 * Grab the physical address of the framebuffer, and then map it 333 * into our memory space. If the MMU is not yet up, it will be 334 * remapped for us when relocation turns on. 335 */ 336 if (OF_getproplen(node, "address") == sizeof(fb_phys)) { 337 /* XXX We assume #address-cells is 1 at this point. */ 338 OF_getprop(node, "address", &fb_phys, sizeof(fb_phys)); 339 340 #if defined(__powerpc__) 341 sc->sc_memt = &bs_be_tag; 342 bus_space_map(sc->sc_memt, fb_phys, height * sc->fb.fb_stride, 343 BUS_SPACE_MAP_PREFETCHABLE, &sc->fb.fb_vbase); 344 #elif defined(__sparc64__) 345 OF_decode_addr(node, 0, &space, &phys); 346 sc->sc_memt = &ofwfb_memt[0]; 347 sc->fb.fb_vbase = 348 sparc64_fake_bustag(space, fb_phys, sc->sc_memt); 349 #else 350 #error Unsupported platform! 351 #endif 352 } else { 353 /* 354 * Some IBM systems don't have an address property. Try to 355 * guess the framebuffer region from the assigned addresses. 356 * This is ugly, but there doesn't seem to be an alternative. 357 * Linux does the same thing. 358 */ 359 360 fb_phys = sc->sc_num_pciaddrs; 361 for (i = 0; i < sc->sc_num_pciaddrs; i++) { 362 /* If it is too small, not the framebuffer */ 363 if (sc->sc_pciaddrs[i].size_lo < 364 sc->fb.fb_stride * height) 365 continue; 366 /* If it is not memory, it isn't either */ 367 if (!(sc->sc_pciaddrs[i].phys_hi & 368 OFW_PCI_PHYS_HI_SPACE_MEM32)) 369 continue; 370 371 /* This could be the framebuffer */ 372 fb_phys = i; 373 374 /* If it is prefetchable, it certainly is */ 375 if (sc->sc_pciaddrs[i].phys_hi & 376 OFW_PCI_PHYS_HI_PREFETCHABLE) 377 break; 378 } 379 380 if (fb_phys == sc->sc_num_pciaddrs) /* No candidates found */ 381 return (CN_DEAD); 382 383 #if defined(__powerpc__) 384 OF_decode_addr(node, fb_phys, &sc->sc_memt, &sc->fb.fb_vbase); 385 #elif defined(__sparc64__) 386 OF_decode_addr(node, fb_phys, &space, &phys); 387 sc->sc_memt = &ofwfb_memt[0]; 388 sc->sc_addr = sparc64_fake_bustag(space, phys, sc->sc_memt); 389 #endif 390 } 391 392 sc->fb.fb_pbase = fb_phys; 393 394 ofwfb_initialize(vd); 395 fb_probe(&sc->fb); 396 vt_fb_init(vd); 397 398 return (CN_INTERNAL); 399 } 400 401 static int 402 ofwfb_mmap(struct vt_device *vd, vm_ooffset_t offset, vm_paddr_t *paddr, 403 int prot, vm_memattr_t *memattr) 404 { 405 struct ofwfb_softc *sc = vd->vd_softc; 406 int i; 407 408 /* 409 * Make sure the requested address lies within the PCI device's 410 * assigned addrs 411 */ 412 for (i = 0; i < sc->sc_num_pciaddrs; i++) 413 if (offset >= sc->sc_pciaddrs[i].phys_lo && 414 offset < (sc->sc_pciaddrs[i].phys_lo + sc->sc_pciaddrs[i].size_lo)) 415 { 416 #ifdef VM_MEMATTR_WRITE_COMBINING 417 /* 418 * If this is a prefetchable BAR, we can (and should) 419 * enable write-combining. 420 */ 421 if (sc->sc_pciaddrs[i].phys_hi & 422 OFW_PCI_PHYS_HI_PREFETCHABLE) 423 *memattr = VM_MEMATTR_WRITE_COMBINING; 424 #endif 425 426 *paddr = offset; 427 return (0); 428 } 429 430 /* 431 * Hack for Radeon... 432 */ 433 *paddr = offset; 434 return (0); 435 } 436 437