ofwfb.c revision 257988
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: user/ed/newcons/sys/dev/vt/hw/ofwfb/ofwfb.c 219888 2011-03-22 21:31:31Z ed $"); 29 30#include <sys/param.h> 31#include <sys/kernel.h> 32#include <sys/systm.h> 33 34#include <dev/vt/vt.h> 35#include <dev/vt/colors/vt_termcolors.h> 36 37#include <vm/vm.h> 38#include <vm/pmap.h> 39 40#include <machine/bus.h> 41#ifdef __sparc64__ 42#include <machine/bus_private.h> 43#endif 44 45#include <dev/ofw/openfirm.h> 46#include <dev/ofw/ofw_bus.h> 47#include <dev/ofw/ofw_pci.h> 48 49struct ofwfb_softc { 50 phandle_t sc_node; 51 52 intptr_t sc_addr; 53 int sc_depth; 54 int sc_stride; 55 56 bus_space_tag_t sc_memt; 57 58 uint32_t sc_colormap[16]; 59}; 60 61static vd_init_t ofwfb_init; 62static vd_blank_t ofwfb_blank; 63static vd_bitbltchr_t ofwfb_bitbltchr; 64 65static const struct vt_driver vt_ofwfb_driver = { 66 .vd_init = ofwfb_init, 67 .vd_blank = ofwfb_blank, 68 .vd_bitbltchr = ofwfb_bitbltchr, 69 .vd_priority = VD_PRIORITY_GENERIC+1, 70}; 71 72static struct ofwfb_softc ofwfb_conssoftc; 73VT_CONSDEV_DECLARE(vt_ofwfb_driver, PIXEL_WIDTH(1920), PIXEL_HEIGHT(1200), 74 &ofwfb_conssoftc); 75/* XXX: hardcoded max size */ 76 77static void 78ofwfb_blank(struct vt_device *vd, term_color_t color) 79{ 80 struct ofwfb_softc *sc = vd->vd_softc; 81 u_int ofs; 82 uint32_t c; 83 84 switch (sc->sc_depth) { 85 case 8: 86 for (ofs = 0; ofs < sc->sc_stride*vd->vd_height; ofs++) 87 *(uint8_t *)(sc->sc_addr + ofs) = color; 88 break; 89 case 32: 90 c = sc->sc_colormap[color]; 91 for (ofs = 0; ofs < sc->sc_stride*vd->vd_height; ofs++) 92 *(uint32_t *)(sc->sc_addr + 4*ofs) = c; 93 break; 94 default: 95 /* panic? */ 96 break; 97 } 98} 99 100static void 101ofwfb_bitbltchr(struct vt_device *vd, const uint8_t *src, const uint8_t *mask, 102 int bpl, vt_axis_t top, vt_axis_t left, unsigned int width, 103 unsigned int height, term_color_t fg, term_color_t bg) 104{ 105 struct ofwfb_softc *sc = vd->vd_softc; 106 u_long line; 107 uint32_t fgc, bgc; 108 int c; 109 uint8_t b, m; 110 111 fgc = sc->sc_colormap[fg]; 112 bgc = sc->sc_colormap[bg]; 113 114 /* Don't try to put off screen pixels */ 115 if (((left + width) > info->fb_width) || ((top + height) > 116 info->fb_height)) 117 return; 118 119 line = (sc->sc_stride * top) + left * sc->sc_depth/8; 120 for (; height > 0; height--) { 121 for (c = 0; c < width; c++) { 122 if (c % 8 == 0) 123 b = *src++; 124 else 125 b <<= 1; 126 if (mask != NULL) { 127 if (c % 8 == 0) 128 m = *mask++; 129 else 130 m <<= 1; 131 /* Skip pixel write, if mask has no bit set. */ 132 if ((m & 0x80) == 0) 133 continue; 134 } 135 switch(sc->sc_depth) { 136 case 8: 137 *(uint8_t *)(sc->sc_addr + line + c) = 138 b & 0x80 ? fg : bg; 139 break; 140 case 32: 141 *(uint32_t *)(sc->sc_addr + line + 4*c) = 142 (b & 0x80) ? fgc : bgc; 143 break; 144 default: 145 /* panic? */ 146 break; 147 } 148 } 149 line += sc->sc_stride; 150 } 151} 152 153static void 154ofwfb_initialize(struct vt_device *vd) 155{ 156 struct ofwfb_softc *sc = vd->vd_softc; 157 char name[64]; 158 ihandle_t ih; 159 int i; 160 cell_t retval; 161 uint32_t oldpix; 162 163 /* Open display device, thereby initializing it */ 164 memset(name, 0, sizeof(name)); 165 OF_package_to_path(sc->sc_node, name, sizeof(name)); 166 ih = OF_open(name); 167 168 /* 169 * Set up the color map 170 */ 171 172 switch (sc->sc_depth) { 173 case 8: 174 vt_generate_vga_palette(sc->sc_colormap, COLOR_FORMAT_RGB, 255, 175 0, 255, 8, 255, 16); 176 177 for (i = 0; i < 16; i++) { 178 OF_call_method("color!", ih, 4, 1, 179 (cell_t)((sc->sc_colormap[i] >> 16) & 0xff), 180 (cell_t)((sc->sc_colormap[i] >> 8) & 0xff), 181 (cell_t)((sc->sc_colormap[i] >> 0) & 0xff), 182 (cell_t)i, &retval); 183 } 184 break; 185 186 case 32: 187 /* 188 * We bypass the usual bus_space_() accessors here, mostly 189 * for performance reasons. In particular, we don't want 190 * any barrier operations that may be performed and handle 191 * endianness slightly different. Figure out the host-view 192 * endianness of the frame buffer. 193 */ 194 oldpix = bus_space_read_4(sc->sc_memt, sc->sc_addr, 0); 195 bus_space_write_4(sc->sc_memt, sc->sc_addr, 0, 0xff000000); 196 if (*(uint8_t *)(sc->sc_addr) == 0xff) 197 vt_generate_vga_palette(sc->sc_colormap, 198 COLOR_FORMAT_RGB, 255, 16, 255, 8, 255, 0); 199 else 200 vt_generate_vga_palette(sc->sc_colormap, 201 COLOR_FORMAT_RGB, 255, 0, 255, 8, 255, 16); 202 bus_space_write_4(sc->sc_memt, sc->sc_addr, 0, oldpix); 203 break; 204 205 default: 206 panic("Unknown color space depth %d", sc->sc_depth); 207 break; 208 } 209 210 /* Clear the screen. */ 211 ofwfb_blank(vd, TC_BLACK); 212} 213 214static int 215ofwfb_init(struct vt_device *vd) 216{ 217 struct ofwfb_softc *sc = vd->vd_softc; 218 char type[64]; 219 phandle_t chosen; 220 ihandle_t stdout; 221 phandle_t node; 222 uint32_t depth, height, width; 223 struct ofw_pci_register pciaddrs[8]; 224 int n_pciaddrs; 225 uint32_t fb_phys; 226 int i, len; 227#ifdef __sparc64__ 228 static struct bus_space_tag ofwfb_memt[1]; 229 bus_addr_t phys; 230 int space; 231#endif 232 233 chosen = OF_finddevice("/chosen"); 234 OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)); 235 node = OF_instance_to_package(stdout); 236 if (node == -1) { 237 /* 238 * The "/chosen/stdout" does not exist try 239 * using "screen" directly. 240 */ 241 node = OF_finddevice("screen"); 242 } 243 OF_getprop(node, "device_type", type, sizeof(type)); 244 if (strcmp(type, "display") != 0) 245 return (CN_DEAD); 246 247 /* Keep track of the OF node */ 248 sc->sc_node = node; 249 250 /* Make sure we have needed properties */ 251 if (OF_getproplen(node, "height") != sizeof(height) || 252 OF_getproplen(node, "width") != sizeof(width) || 253 OF_getproplen(node, "depth") != sizeof(depth) || 254 OF_getproplen(node, "linebytes") != sizeof(sc->sc_stride)) 255 return (CN_DEAD); 256 257 /* Only support 8 and 32-bit framebuffers */ 258 OF_getprop(node, "depth", &depth, sizeof(depth)); 259 if (depth != 8 && depth != 32) 260 return (CN_DEAD); 261 sc->sc_depth = depth; 262 263 OF_getprop(node, "height", &height, sizeof(height)); 264 OF_getprop(node, "width", &width, sizeof(width)); 265 OF_getprop(node, "linebytes", &sc->sc_stride, sizeof(sc->sc_stride)); 266 267 vd->vd_height = height; 268 vd->vd_width = width; 269 270 /* 271 * Get the PCI addresses of the adapter, if present. The node may be the 272 * child of the PCI device: in that case, try the parent for 273 * the assigned-addresses property. 274 */ 275 len = OF_getprop(node, "assigned-addresses", pciaddrs, 276 sizeof(pciaddrs)); 277 if (len == -1) { 278 len = OF_getprop(OF_parent(node), "assigned-addresses", 279 pciaddrs, sizeof(pciaddrs)); 280 } 281 if (len == -1) 282 len = 0; 283 n_pciaddrs = len / sizeof(struct ofw_pci_register); 284 285 /* 286 * Grab the physical address of the framebuffer, and then map it 287 * into our memory space. If the MMU is not yet up, it will be 288 * remapped for us when relocation turns on. 289 */ 290 if (OF_getproplen(node, "address") == sizeof(fb_phys)) { 291 /* XXX We assume #address-cells is 1 at this point. */ 292 OF_getprop(node, "address", &fb_phys, sizeof(fb_phys)); 293 294 #if defined(__powerpc__) 295 sc->sc_memt = &bs_be_tag; 296 bus_space_map(sc->sc_memt, fb_phys, height * sc->sc_stride, 297 BUS_SPACE_MAP_PREFETCHABLE, &sc->sc_addr); 298 #elif defined(__sparc64__) 299 OF_decode_addr(node, 0, &space, &phys); 300 sc->sc_memt = &ofwfb_memt[0]; 301 sc->sc_addr = sparc64_fake_bustag(space, fb_phys, sc->sc_memt); 302 #else 303 #error Unsupported platform! 304 #endif 305 } else { 306 /* 307 * Some IBM systems don't have an address property. Try to 308 * guess the framebuffer region from the assigned addresses. 309 * This is ugly, but there doesn't seem to be an alternative. 310 * Linux does the same thing. 311 */ 312 313 fb_phys = n_pciaddrs; 314 for (i = 0; i < n_pciaddrs; i++) { 315 /* If it is too small, not the framebuffer */ 316 if (pciaddrs[i].size_lo < sc->sc_stride*height) 317 continue; 318 /* If it is not memory, it isn't either */ 319 if (!(pciaddrs[i].phys_hi & 320 OFW_PCI_PHYS_HI_SPACE_MEM32)) 321 continue; 322 323 /* This could be the framebuffer */ 324 fb_phys = i; 325 326 /* If it is prefetchable, it certainly is */ 327 if (pciaddrs[i].phys_hi & OFW_PCI_PHYS_HI_PREFETCHABLE) 328 break; 329 } 330 331 if (fb_phys == n_pciaddrs) /* No candidates found */ 332 return (CN_DEAD); 333 334 #if defined(__powerpc__) 335 OF_decode_addr(node, fb_phys, &sc->sc_memt, &sc->sc_addr); 336 #elif defined(__sparc64__) 337 OF_decode_addr(node, fb_phys, &space, &phys); 338 sc->sc_memt = &ofwfb_memt[0]; 339 sc->sc_addr = sparc64_fake_bustag(space, phys, sc->sc_memt); 340 #endif 341 } 342 343 ofwfb_initialize(vd); 344 345 return (CN_INTERNAL); 346} 347 348