arm_simplefb.c revision 1.5
1/* $NetBSD: arm_simplefb.c,v 1.5 2021/01/17 14:28:25 jmcneill Exp $ */ 2 3/*- 4 * Copyright (c) 2019 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jared McNeill <jmcneill@invisible.ca>. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32#include "pci.h" 33#include "opt_pci.h" 34#include "opt_vcons.h" 35 36#include <sys/cdefs.h> 37__KERNEL_RCSID(0, "$NetBSD: arm_simplefb.c,v 1.5 2021/01/17 14:28:25 jmcneill Exp $"); 38 39#include <sys/param.h> 40#include <sys/bus.h> 41#include <sys/cpu.h> 42#include <sys/device.h> 43 44#include <dev/fdt/fdtvar.h> 45#include <arm/fdt/arm_fdtvar.h> 46 47#include <dev/wscons/wsconsio.h> 48#include <dev/wscons/wsdisplayvar.h> 49#include <dev/rasops/rasops.h> 50#include <dev/wsfont/wsfont.h> 51#include <dev/wscons/wsdisplay_vconsvar.h> 52 53#if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE) 54#include <dev/pci/pcireg.h> 55#include <dev/pci/pcivar.h> 56#include <dev/pci/pciconf.h> 57#endif 58 59#include <arm/fdt/arm_simplefb.h> 60 61#include <libfdt.h> 62 63extern struct bus_space arm_generic_bs_tag; 64 65static struct arm_simplefb_softc { 66 uint32_t sc_width; 67 uint32_t sc_height; 68 uint32_t sc_stride; 69 uint16_t sc_depth; 70 bool sc_swapped; 71 void *sc_bits; 72} arm_simplefb_softc; 73 74static struct wsscreen_descr arm_simplefb_stdscreen = { 75 .name = "std", 76 .ncols = 0, 77 .nrows = 0, 78 .textops = NULL, 79 .fontwidth = 0, 80 .fontheight = 0, 81 .capabilities = 0, 82 .modecookie = NULL 83}; 84 85static struct wsdisplay_accessops arm_simplefb_accessops; 86static struct vcons_data arm_simplefb_vcons_data; 87static struct vcons_screen arm_simplefb_screen; 88 89static bus_addr_t arm_simplefb_addr; 90static bus_size_t arm_simplefb_size; 91static bus_space_handle_t arm_simplefb_bsh; 92 93static int 94arm_simplefb_find_node(void) 95{ 96 static const char * simplefb_compatible[] = { "simple-framebuffer", NULL }; 97 int chosen_phandle, child; 98 99 chosen_phandle = OF_finddevice("/chosen"); 100 if (chosen_phandle == -1) 101 return -1; 102 103 for (child = OF_child(chosen_phandle); child; child = OF_peer(child)) { 104 if (!fdtbus_status_okay(child)) 105 continue; 106 if (!of_match_compatible(child, simplefb_compatible)) 107 continue; 108 109 return child; 110 } 111 112 return -1; 113} 114 115static void 116arm_simplefb_init_screen(void *cookie, struct vcons_screen *scr, 117 int existing, long *defattr) 118{ 119 struct arm_simplefb_softc * const sc = cookie; 120 struct rasops_info *ri = &scr->scr_ri; 121 122 ri->ri_width = sc->sc_width; 123 ri->ri_height = sc->sc_height; 124 ri->ri_depth = sc->sc_depth; 125 ri->ri_stride = sc->sc_stride; 126 ri->ri_bits = sc->sc_bits; 127 ri->ri_flg = RI_CENTER | RI_FULLCLEAR | RI_CLEAR; 128 129 if (sc->sc_swapped) { 130 KASSERT(ri->ri_depth == 32); 131 ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 8; 132 ri->ri_rpos = 8; 133 ri->ri_gpos = 16; 134 ri->ri_bpos = 24; 135 } 136 137 scr->scr_flags |= VCONS_LOADFONT; 138 scr->scr_flags |= VCONS_DONT_READ; 139 140 rasops_init(ri, ri->ri_height / 8, ri->ri_width / 8); 141 ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE; 142 rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight, 143 sc->sc_width / ri->ri_font->fontwidth); 144 145 ri->ri_hw = scr; 146} 147 148static int 149arm_simplefb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, lwp_t *l) 150{ 151 return EPASSTHROUGH; 152} 153 154static paddr_t 155arm_simplefb_mmap(void *v, void *vs, off_t offset, int prot) 156{ 157 return -1; 158} 159 160static void 161arm_simplefb_pollc(void *v, int on) 162{ 163} 164 165#if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE) 166static void 167arm_simplefb_reconfig(void *arg, uint64_t new_addr) 168{ 169 struct arm_simplefb_softc * const sc = &arm_simplefb_softc; 170 struct rasops_info *ri = &arm_simplefb_screen.scr_ri; 171 bus_space_tag_t bst = &arm_generic_bs_tag; 172 173 bus_space_unmap(bst, arm_simplefb_bsh, arm_simplefb_size); 174 bus_space_map(bst, new_addr, arm_simplefb_size, 175 BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE, &arm_simplefb_bsh); 176 177 sc->sc_bits = bus_space_vaddr(bst, arm_simplefb_bsh); 178 ri->ri_bits = sc->sc_bits; 179 180 arm_simplefb_addr = (bus_addr_t)new_addr; 181} 182#endif 183 184uint64_t 185arm_simplefb_physaddr(void) 186{ 187 return arm_simplefb_addr; 188} 189 190void 191arm_simplefb_preattach(void) 192{ 193 struct arm_simplefb_softc * const sc = &arm_simplefb_softc; 194 struct rasops_info *ri = &arm_simplefb_screen.scr_ri; 195 bus_space_tag_t bst = &arm_generic_bs_tag; 196 bus_space_handle_t bsh; 197 uint32_t width, height, stride; 198 const char *format; 199 bus_addr_t addr; 200 bus_size_t size; 201 uint16_t depth; 202 long defattr; 203 bool swapped = false; 204 205 const int phandle = arm_simplefb_find_node(); 206 if (phandle == -1) 207 return; 208 209 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0 || size == 0) 210 return; 211 212 if (of_getprop_uint32(phandle, "width", &width) != 0 || 213 of_getprop_uint32(phandle, "height", &height) != 0 || 214 of_getprop_uint32(phandle, "stride", &stride) != 0 || 215 (format = fdtbus_get_string(phandle, "format")) == NULL) 216 return; 217 218 if (width == 0 || height == 0) 219 return; 220 221 if (strcmp(format, "a8b8g8r8") == 0 || 222 strcmp(format, "x8r8g8b8") == 0) { 223 depth = 32; 224 } else if (strcmp(format, "r8g8b8a8") == 0 || 225 strcmp(format, "b8g8r8x8") == 0) { 226 depth = 32; 227 swapped = true; 228 } else if (strcmp(format, "r5g6b5") == 0) { 229 depth = 16; 230 } else { 231 return; 232 } 233 234 if (bus_space_map(bst, addr, size, 235 BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE, &bsh) != 0) 236 return; 237 238 arm_simplefb_addr = addr; 239 arm_simplefb_size = size; 240 arm_simplefb_bsh = bsh; 241 242 sc->sc_width = width; 243 sc->sc_height = height; 244 sc->sc_depth = depth; 245 sc->sc_stride = stride; 246 sc->sc_bits = bus_space_vaddr(bst, bsh); 247 sc->sc_swapped = swapped; 248 249 wsfont_init(); 250 251 arm_simplefb_accessops.ioctl = arm_simplefb_ioctl; 252 arm_simplefb_accessops.mmap = arm_simplefb_mmap; 253 arm_simplefb_accessops.pollc = arm_simplefb_pollc; 254 255 vcons_init(&arm_simplefb_vcons_data, sc, &arm_simplefb_stdscreen, 256 &arm_simplefb_accessops); 257 arm_simplefb_vcons_data.init_screen = arm_simplefb_init_screen; 258#ifdef VCONS_DRAW_INTR 259 arm_simplefb_vcons_data.use_intr = 0; 260#endif 261 vcons_init_screen(&arm_simplefb_vcons_data, &arm_simplefb_screen, 1, &defattr); 262 arm_simplefb_screen.scr_flags |= VCONS_SCREEN_IS_STATIC; 263 264 if (ri->ri_rows < 1 || ri->ri_cols < 1) 265 return; 266 267 arm_simplefb_stdscreen.nrows = ri->ri_rows; 268 arm_simplefb_stdscreen.ncols = ri->ri_cols; 269 arm_simplefb_stdscreen.textops = &ri->ri_ops; 270 arm_simplefb_stdscreen.capabilities = ri->ri_caps; 271 272 wsdisplay_preattach(&arm_simplefb_stdscreen, ri, 0, 0, defattr); 273 274 vcons_replay_msgbuf(&arm_simplefb_screen); 275 276#if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE) 277 /* 278 * Let the PCI resource allocator know about our framebuffer. This 279 * lets us know if the FB base address changes so we can remap the 280 * framebuffer if necessary. 281 */ 282 pciconf_resource_reserve(PCI_CONF_MAP_MEM, addr, size, 283 arm_simplefb_reconfig, NULL); 284#endif 285} 286