1/* $NetBSD: simplefb.c,v 1.15 2021/08/30 22:47:24 jmcneill Exp $ */ 2 3/*- 4 * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca> 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 ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * 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 "opt_wsdisplay_compat.h" 30 31#include <sys/cdefs.h> 32__KERNEL_RCSID(0, "$NetBSD: simplefb.c,v 1.15 2021/08/30 22:47:24 jmcneill Exp $"); 33 34#include <sys/param.h> 35#include <sys/bus.h> 36#include <sys/device.h> 37#include <sys/systm.h> 38 39#include <dev/fdt/fdtvar.h> 40 41#include <dev/wsfb/genfbvar.h> 42 43static const struct device_compatible_entry compat_data[] = { 44 { .compat = "simple-framebuffer" }, 45 DEVICE_COMPAT_EOL 46}; 47 48struct simplefb_softc { 49 struct genfb_softc sc_gen; 50 bus_space_tag_t sc_bst; 51 bus_space_handle_t sc_bsh; 52 int sc_phandle; 53 54 bus_addr_t sc_paddr; 55}; 56 57static int simplefb_console_phandle = -1; 58 59static bool 60simplefb_shutdown(device_t self, int flags) 61{ 62 genfb_enable_polling(self); 63 return true; 64} 65 66static int 67simplefb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, lwp_t *l) 68{ 69 struct simplefb_softc * const sc = v; 70 struct wsdisplayio_bus_id *busid; 71 struct wsdisplayio_fbinfo *fbi; 72 struct rasops_info *ri; 73 u_int video; 74 int error; 75 76 switch (cmd) { 77 case WSDISPLAYIO_GTYPE: 78 *(u_int *)data = WSDISPLAY_TYPE_GENFB; 79 return 0; 80 case WSDISPLAYIO_GET_BUSID: 81 busid = data; 82 busid->bus_type = WSDISPLAYIO_BUS_SOC; 83 return 0; 84 case WSDISPLAYIO_GET_FBINFO: 85 fbi = data; 86 ri = &sc->sc_gen.vd.active->scr_ri; 87 error = wsdisplayio_get_fbinfo(ri, fbi); 88 if (error == 0) { 89 /* 90 * XXX 91 * if the fb isn't page aligned, tell wsfb to skip the 92 * unaligned part 93 */ 94 fbi->fbi_fboffset = sc->sc_paddr & PAGE_MASK; 95 fbi->fbi_flags |= WSFB_VRAM_IS_RAM; 96 } 97 return error; 98 case WSDISPLAYIO_SVIDEO: 99 video = *(u_int *)data; 100 if (video == WSDISPLAYIO_VIDEO_OFF) 101 pmf_event_inject(NULL, PMFE_DISPLAY_OFF); 102 else if (video == WSDISPLAYIO_VIDEO_ON) 103 pmf_event_inject(NULL, PMFE_DISPLAY_ON); 104 else 105 return EINVAL; 106 return 0; 107 default: 108 return EPASSTHROUGH; 109 } 110} 111 112static paddr_t 113simplefb_mmap(void *v, void *vs, off_t off, int prot) 114{ 115 struct simplefb_softc * const sc = v; 116 117 if (off < 0 || off >= (sc->sc_paddr & PAGE_MASK) + 118 sc->sc_gen.sc_fbsize) 119 return -1; 120 121 return bus_space_mmap(sc->sc_bst, trunc_page(sc->sc_paddr), off, prot, 122 BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE); 123} 124 125static int 126simplefb_attach_genfb(struct simplefb_softc *sc) 127{ 128 device_t dev = sc->sc_gen.sc_dev; 129 prop_dictionary_t dict = device_properties(dev); 130 const int phandle = sc->sc_phandle; 131 struct genfb_ops ops; 132 uint32_t width, height, stride; 133 uint16_t depth; 134 const char *format; 135 uint64_t sfb_addr; 136 bus_addr_t addr; 137 bus_size_t size; 138 139 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 140 aprint_error(": couldn't get registers\n"); 141 return ENXIO; 142 } 143 144 if (size == 0) { 145 aprint_naive("\n"); 146 aprint_normal(": disabled\n"); 147 return ENXIO; 148 } 149 150 if (of_getprop_uint32(phandle, "width", &width) != 0 || 151 of_getprop_uint32(phandle, "height", &height) != 0 || 152 of_getprop_uint32(phandle, "stride", &stride) != 0 || 153 (format = fdtbus_get_string(phandle, "format")) == NULL) { 154 aprint_error(": missing property in DT\n"); 155 return ENXIO; 156 } 157 158 if (strcmp(format, "a8b8g8r8") == 0 || 159 strcmp(format, "x8r8g8b8") == 0) { 160 depth = 32; 161 } else if (strcmp(format, "r8g8b8a8") == 0 || 162 strcmp(format, "b8g8r8x8") == 0) { 163 depth = 32; 164 prop_dictionary_set_bool(dict, "is_swapped", true); 165 } else if (strcmp(format, "x2r10g10b10") == 0) { 166 depth = 32; 167 prop_dictionary_set_bool(dict, "is_10bit", true); 168 } else if (strcmp(format, "r5g6b5") == 0) { 169 depth = 16; 170 } else { 171 aprint_error(": unsupported format '%s'\n", format); 172 return ENXIO; 173 } 174 175 if (size < stride * height) { 176 aprint_error(": incorrect size\n"); 177 return ENXIO; 178 } 179 180 /* Device may have been reconfigured. MD code will tell us. */ 181 if (prop_dictionary_get_uint64(dict, "simplefb-physaddr", &sfb_addr) && 182 sfb_addr != 0) { 183 addr = (bus_addr_t)sfb_addr; 184 } 185 186 if (bus_space_map(sc->sc_bst, addr, size, 187 BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE, 188 &sc->sc_bsh) != 0) { 189 aprint_error(": failed to map fb\n"); 190 return ENXIO; 191 } 192 193 sc->sc_paddr = addr; 194 195 prop_dictionary_set_uint32(dict, "width", width); 196 prop_dictionary_set_uint32(dict, "height", height); 197 prop_dictionary_set_uint8(dict, "depth", depth); 198 prop_dictionary_set_uint16(dict, "linebytes", stride); 199 prop_dictionary_set_uint32(dict, "address", addr); 200 prop_dictionary_set_uint64(dict, "virtual_address", 201 (uintptr_t)bus_space_vaddr(sc->sc_bst, sc->sc_bsh)); 202 203 genfb_init(&sc->sc_gen); 204 205 if (sc->sc_gen.sc_width == 0 || sc->sc_gen.sc_fbsize == 0) { 206 aprint_normal(": disabled\n"); 207 return ENXIO; 208 } 209 210 aprint_naive("\n"); 211 aprint_normal(": Simple Framebuffer (%ux%u %u-bpp @ 0x%" PRIxBUSADDR ")\n", 212 width, height, depth, addr); 213 214 pmf_device_register1(dev, NULL, NULL, simplefb_shutdown); 215 216 memset(&ops, 0, sizeof(ops)); 217 ops.genfb_ioctl = simplefb_ioctl; 218 ops.genfb_mmap = simplefb_mmap; 219 220#ifdef WSDISPLAY_MULTICONS 221 const bool is_console = true; 222 genfb_cnattach(); 223#else 224 const bool is_console = phandle == simplefb_console_phandle; 225 if (is_console) 226 aprint_normal_dev(sc->sc_gen.sc_dev, 227 "switching to framebuffer console\n"); 228#endif 229 230 prop_dictionary_set_bool(dict, "is_console", is_console); 231 232 genfb_attach(&sc->sc_gen, &ops); 233 234 return 0; 235} 236 237static int 238simplefb_match(device_t parent, cfdata_t cf, void *aux) 239{ 240 struct fdt_attach_args * const faa = aux; 241 242 return of_compatible_match(faa->faa_phandle, compat_data); 243} 244 245static void 246simplefb_attach(device_t parent, device_t self, void *aux) 247{ 248 struct simplefb_softc * const sc = device_private(self); 249 struct fdt_attach_args * const faa = aux; 250 const int phandle = faa->faa_phandle; 251 252 sc->sc_gen.sc_dev = self; 253 sc->sc_phandle = phandle; 254 sc->sc_bst = faa->faa_bst; 255 256 if (simplefb_attach_genfb(sc) != 0) 257 return; 258} 259 260CFATTACH_DECL_NEW(simplefb, sizeof(struct simplefb_softc), 261 simplefb_match, simplefb_attach, NULL, NULL); 262 263static int 264simplefb_console_match(int phandle) 265{ 266 return of_compatible_match(phandle, compat_data); 267} 268 269static void 270simplefb_console_consinit(struct fdt_attach_args *faa, u_int uart_freq) 271{ 272 simplefb_console_phandle = faa->faa_phandle; 273 genfb_cnattach(); 274} 275 276static const struct fdt_console simplefb_fdt_console = { 277 .match = simplefb_console_match, 278 .consinit = simplefb_console_consinit 279}; 280 281FDT_CONSOLE(simplefb, &simplefb_fdt_console); 282