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