1257726Sray/*- 2257726Sray * Copyright (c) 2013 The FreeBSD Foundation 3257726Sray * All rights reserved. 4257726Sray * 5257726Sray * This software was developed by Aleksandr Rybalko under sponsorship from the 6257726Sray * FreeBSD Foundation. 7257726Sray * 8257726Sray * Redistribution and use in source and binary forms, with or without 9257726Sray * modification, are permitted provided that the following conditions 10257726Sray * are met: 11257726Sray * 1. Redistributions of source code must retain the above copyright 12257726Sray * notice, this list of conditions and the following disclaimer. 13257726Sray * 2. Redistributions in binary form must reproduce the above copyright 14257726Sray * notice, this list of conditions and the following disclaimer in the 15257726Sray * documentation and/or other materials provided with the distribution. 16257726Sray * 17257726Sray * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18257726Sray * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19257726Sray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20257726Sray * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21257726Sray * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22257726Sray * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23257726Sray * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24257726Sray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25257726Sray * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26257726Sray * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27257726Sray * SUCH DAMAGE. 28257726Sray */ 29257726Sray 30257726Sray#include <sys/cdefs.h> 31257726Sray__FBSDID("$FreeBSD$"); 32257726Sray 33257726Sray#include <sys/param.h> 34257726Sray#include <sys/systm.h> 35257726Sray#include <sys/kernel.h> 36257726Sray#include <sys/fbio.h> 37257726Sray 38257726Sray#include "opt_platform.h" 39257726Sray 40257726Sray#ifdef FDT 41257726Sray#include <dev/fdt/fdt_common.h> 42257726Sray#include <dev/ofw/ofw_bus.h> 43257726Sray#include <dev/ofw/ofw_bus_subr.h> 44257726Sray#include <dev/ofw/ofw_pci.h> 45264182Srpaulo#include <machine/fdt.h> 46257726Sray#endif 47257726Sray 48257726Sray#include <dev/vt/vt.h> 49257726Sray#include <dev/vt/hw/fb/vt_fb.h> 50257726Sray#include <dev/vt/colors/vt_termcolors.h> 51257726Sray 52257726Sraystatic vd_init_t vt_efb_init; 53265397Sraystatic vd_probe_t vt_efb_probe; 54257726Sray 55257726Sraystatic struct vt_driver vt_fb_early_driver = { 56265397Sray .vd_name = "efb", 57265397Sray .vd_probe = vt_efb_probe, 58257726Sray .vd_init = vt_efb_init, 59257726Sray .vd_blank = vt_fb_blank, 60270411Sdumbbell .vd_bitblt_text = vt_fb_bitblt_text, 61270431Sdumbbell .vd_bitblt_bmp = vt_fb_bitblt_bitmap, 62271684Sdumbbell .vd_drawrect = vt_fb_drawrect, 63271684Sdumbbell .vd_setpixel = vt_fb_setpixel, 64257726Sray .vd_priority = VD_PRIORITY_GENERIC, 65257726Sray}; 66257726Sray 67265397Sraystatic struct fb_info local_info; 68265397SrayVT_DRIVER_DECLARE(vt_efb, vt_fb_early_driver); 69257726Sray 70257726Sraystatic void 71257726Sray#ifdef FDT 72257726Srayvt_efb_initialize(struct fb_info *info, phandle_t node) 73257726Sray#else 74257726Srayvt_efb_initialize(struct fb_info *info) 75257726Sray#endif 76257726Sray{ 77257726Sray#ifdef FDT 78257726Sray char name[64]; 79257726Sray cell_t retval; 80257726Sray ihandle_t ih; 81257726Sray int i; 82257726Sray 83257726Sray /* Open display device, thereby initializing it */ 84257726Sray memset(name, 0, sizeof(name)); 85257726Sray OF_package_to_path(node, name, sizeof(name)); 86257726Sray ih = OF_open(name); 87257726Sray#endif 88257726Sray 89257726Sray /* 90257726Sray * Set up the color map 91257726Sray */ 92257726Sray switch (info->fb_depth) { 93257726Sray case 8: 94269783Sdumbbell vt_generate_cons_palette(info->fb_cmap, COLOR_FORMAT_RGB, 95257726Sray 0x7, 5, 0x7, 2, 0x3, 0); 96257726Sray break; 97257726Sray case 15: 98269783Sdumbbell vt_generate_cons_palette(info->fb_cmap, COLOR_FORMAT_RGB, 99257726Sray 0x1f, 10, 0x1f, 5, 0x1f, 0); 100257726Sray break; 101257726Sray case 16: 102269783Sdumbbell vt_generate_cons_palette(info->fb_cmap, COLOR_FORMAT_RGB, 103257726Sray 0x1f, 11, 0x3f, 5, 0x1f, 0); 104257726Sray break; 105257726Sray case 24: 106257726Sray case 32: 107257726Sray#if BYTE_ORDER == BIG_ENDIAN 108269783Sdumbbell vt_generate_cons_palette(info->fb_cmap, 109269783Sdumbbell COLOR_FORMAT_RGB, 255, 0, 255, 8, 255, 16); 110269783Sdumbbell#else 111269783Sdumbbell vt_generate_cons_palette(info->fb_cmap, 112257726Sray COLOR_FORMAT_RGB, 255, 16, 255, 8, 255, 0); 113257726Sray#endif 114257726Sray#ifdef FDT 115257726Sray for (i = 0; i < 16; i++) { 116257726Sray OF_call_method("color!", ih, 4, 1, 117257726Sray (cell_t)((info->fb_cmap[i] >> 16) & 0xff), 118257726Sray (cell_t)((info->fb_cmap[i] >> 8) & 0xff), 119257726Sray (cell_t)((info->fb_cmap[i] >> 0) & 0xff), 120257726Sray (cell_t)i, &retval); 121257726Sray } 122257726Sray#endif 123257726Sray break; 124257726Sray 125257726Sray default: 126257726Sray panic("Unknown color space fb_depth %d", info->fb_depth); 127257726Sray break; 128285911Smarius } 129257726Sray} 130257726Sray 131265397Sraystatic phandle_t 132265397Srayvt_efb_get_fbnode() 133257726Sray{ 134257726Sray phandle_t chosen, node; 135257726Sray ihandle_t stdout; 136257726Sray char type[64]; 137257726Sray 138257726Sray chosen = OF_finddevice("/chosen"); 139257726Sray OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)); 140257726Sray node = OF_instance_to_package(stdout); 141265397Sray if (node != -1) { 142265397Sray /* The "/chosen/stdout" present. */ 143265397Sray OF_getprop(node, "device_type", type, sizeof(type)); 144265397Sray /* Check if it has "display" type. */ 145265397Sray if (strcmp(type, "display") == 0) 146265397Sray return (node); 147257726Sray } 148265397Sray /* Try device with name "screen". */ 149265397Sray node = OF_finddevice("screen"); 150265397Sray 151265397Sray return (node); 152265397Sray} 153265397Sray 154265397Sraystatic int 155265397Srayvt_efb_probe(struct vt_device *vd) 156265397Sray{ 157265397Sray phandle_t node; 158265397Sray 159265397Sray node = vt_efb_get_fbnode(); 160265397Sray if (node == -1) 161257726Sray return (CN_DEAD); 162257726Sray 163265397Sray if ((OF_getproplen(node, "height") <= 0) || 164265397Sray (OF_getproplen(node, "width") <= 0) || 165265397Sray (OF_getproplen(node, "depth") <= 0) || 166265397Sray (OF_getproplen(node, "linebytes") <= 0)) 167265397Sray return (CN_DEAD); 168265397Sray 169265397Sray return (CN_INTERNAL); 170265397Sray} 171265397Sray 172265397Sraystatic int 173265397Srayvt_efb_init(struct vt_device *vd) 174265397Sray{ 175265397Sray struct ofw_pci_register pciaddrs[8]; 176265397Sray struct fb_info *info; 177265397Sray int i, len, n_pciaddrs; 178265397Sray phandle_t node; 179265397Sray 180265397Sray if (vd->vd_softc == NULL) 181265397Sray vd->vd_softc = (void *)&local_info; 182265397Sray 183265397Sray info = vd->vd_softc; 184265397Sray 185265397Sray node = vt_efb_get_fbnode(); 186265397Sray if (node == -1) 187265397Sray return (CN_DEAD); 188265397Sray 189257726Sray#define GET(name, var) \ 190257726Sray if (OF_getproplen(node, (name)) != sizeof(info->fb_##var)) \ 191257726Sray return (CN_DEAD); \ 192257726Sray OF_getencprop(node, (name), &info->fb_##var, sizeof(info->fb_##var)); \ 193257726Sray if (info->fb_##var == 0) \ 194257726Sray return (CN_DEAD); 195257726Sray 196257726Sray GET("height", height) 197257726Sray GET("width", width) 198257726Sray GET("depth", depth) 199257726Sray GET("linebytes", stride) 200257726Sray#undef GET 201257726Sray 202257726Sray info->fb_size = info->fb_height * info->fb_stride; 203257726Sray 204257726Sray /* 205257726Sray * Get the PCI addresses of the adapter, if present. The node may be the 206257726Sray * child of the PCI device: in that case, try the parent for 207257726Sray * the assigned-addresses property. 208257726Sray */ 209257726Sray len = OF_getprop(node, "assigned-addresses", pciaddrs, 210257726Sray sizeof(pciaddrs)); 211257726Sray if (len == -1) { 212257726Sray len = OF_getprop(OF_parent(node), "assigned-addresses", 213257726Sray pciaddrs, sizeof(pciaddrs)); 214285911Smarius } 215285911Smarius if (len == -1) 216285911Smarius len = 0; 217257726Sray n_pciaddrs = len / sizeof(struct ofw_pci_register); 218257726Sray 219257726Sray /* 220257726Sray * Grab the physical address of the framebuffer, and then map it 221257726Sray * into our memory space. If the MMU is not yet up, it will be 222257726Sray * remapped for us when relocation turns on. 223257726Sray */ 224257726Sray if (OF_getproplen(node, "address") == sizeof(info->fb_pbase)) { 225257726Sray /* XXX We assume #address-cells is 1 at this point. */ 226257726Sray OF_getencprop(node, "address", &info->fb_pbase, 227257726Sray sizeof(info->fb_pbase)); 228257726Sray 229257726Sray #if defined(__powerpc__) 230257726Sray sc->sc_memt = &bs_be_tag; 231257726Sray bus_space_map(sc->sc_memt, info->fb_pbase, info->fb_size, 232257726Sray BUS_SPACE_MAP_PREFETCHABLE, &info->fb_vbase); 233257726Sray #elif defined(__sparc64__) 234257726Sray OF_decode_addr(node, 0, &space, &phys); 235257726Sray sc->sc_memt = &vt_efb_memt[0]; 236257726Sray info->addr = sparc64_fake_bustag(space, fb_phys, sc->sc_memt); 237257726Sray #else 238257726Sray bus_space_map(fdtbus_bs_tag, info->fb_pbase, info->fb_size, 239257726Sray BUS_SPACE_MAP_PREFETCHABLE, 240257726Sray (bus_space_handle_t *)&info->fb_vbase); 241257726Sray #endif 242257726Sray } else { 243257726Sray /* 244257726Sray * Some IBM systems don't have an address property. Try to 245257726Sray * guess the framebuffer region from the assigned addresses. 246257726Sray * This is ugly, but there doesn't seem to be an alternative. 247257726Sray * Linux does the same thing. 248257726Sray */ 249257726Sray 250257726Sray info->fb_pbase = n_pciaddrs; 251257726Sray for (i = 0; i < n_pciaddrs; i++) { 252257726Sray /* If it is too small, not the framebuffer */ 253257726Sray if (pciaddrs[i].size_lo < info->fb_size) 254257726Sray continue; 255257726Sray /* If it is not memory, it isn't either */ 256257726Sray if (!(pciaddrs[i].phys_hi & 257257726Sray OFW_PCI_PHYS_HI_SPACE_MEM32)) 258257726Sray continue; 259257726Sray 260257726Sray /* This could be the framebuffer */ 261257726Sray info->fb_pbase = i; 262257726Sray 263257726Sray /* If it is prefetchable, it certainly is */ 264257726Sray if (pciaddrs[i].phys_hi & OFW_PCI_PHYS_HI_PREFETCHABLE) 265257726Sray break; 266257726Sray } 267257726Sray 268257726Sray if (info->fb_pbase == n_pciaddrs) /* No candidates found */ 269257726Sray return (CN_DEAD); 270257726Sray 271257726Sray #if defined(__powerpc__) 272257726Sray OF_decode_addr(node, info->fb_pbase, &sc->sc_memt, 273257726Sray &info->fb_vbase); 274257726Sray #elif defined(__sparc64__) 275257726Sray OF_decode_addr(node, info->fb_pbase, &space, &info->fb_pbase); 276257726Sray sc->sc_memt = &vt_efb_memt[0]; 277257726Sray info->fb_vbase = sparc64_fake_bustag(space, info->fb_pbase, 278257726Sray sc->sc_memt); 279257726Sray #else 280257726Sray bus_space_map(fdtbus_bs_tag, info->fb_pbase, info->fb_size, 281257726Sray BUS_SPACE_MAP_PREFETCHABLE, 282257726Sray (bus_space_handle_t *)&info->fb_vbase); 283257726Sray #endif 284285911Smarius } 285257726Sray 286257726Sray /* blank full size */ 287257726Sray len = info->fb_size / 4; 288257726Sray for (i = 0; i < len; i++) { 289257726Sray ((uint32_t *)info->fb_vbase)[i] = 0; 290257726Sray } 291257726Sray 292257726Sray /* Get pixel storage size. */ 293257726Sray info->fb_bpp = info->fb_stride / info->fb_width * 8; 294257726Sray 295257726Sray#ifdef FDT 296257726Sray vt_efb_initialize(info, node); 297257726Sray#else 298257726Sray vt_efb_initialize(info); 299257726Sray#endif 300257726Sray vt_fb_init(vd); 301257726Sray 302257726Sray return (CN_INTERNAL); 303257726Sray} 304