1*5bf455f7Srin /* $NetBSD: simplefb.c,v 1.8 2019/07/23 14:34:12 rin Exp $ */ 26bc85a93Sjmcneill 36bc85a93Sjmcneill /*- 46bc85a93Sjmcneill * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca> 56bc85a93Sjmcneill * All rights reserved. 66bc85a93Sjmcneill * 76bc85a93Sjmcneill * Redistribution and use in source and binary forms, with or without 86bc85a93Sjmcneill * modification, are permitted provided that the following conditions 96bc85a93Sjmcneill * are met: 106bc85a93Sjmcneill * 1. Redistributions of source code must retain the above copyright 116bc85a93Sjmcneill * notice, this list of conditions and the following disclaimer. 126bc85a93Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright 136bc85a93Sjmcneill * notice, this list of conditions and the following disclaimer in the 146bc85a93Sjmcneill * documentation and/or other materials provided with the distribution. 156bc85a93Sjmcneill * 166bc85a93Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 176bc85a93Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 186bc85a93Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 196bc85a93Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 206bc85a93Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 216bc85a93Sjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 226bc85a93Sjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 236bc85a93Sjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 246bc85a93Sjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 256bc85a93Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 266bc85a93Sjmcneill * SUCH DAMAGE. 276bc85a93Sjmcneill */ 286bc85a93Sjmcneill 299e659622Sjmcneill #include "opt_wsdisplay_compat.h" 309e659622Sjmcneill 316bc85a93Sjmcneill #include <sys/cdefs.h> 32*5bf455f7Srin __KERNEL_RCSID(0, "$NetBSD: simplefb.c,v 1.8 2019/07/23 14:34:12 rin Exp $"); 336bc85a93Sjmcneill 346bc85a93Sjmcneill #include <sys/param.h> 356bc85a93Sjmcneill #include <sys/bus.h> 366bc85a93Sjmcneill #include <sys/device.h> 376bc85a93Sjmcneill #include <sys/systm.h> 386bc85a93Sjmcneill 396bc85a93Sjmcneill #include <dev/fdt/fdtvar.h> 406bc85a93Sjmcneill 416bc85a93Sjmcneill #include <dev/wsfb/genfbvar.h> 426bc85a93Sjmcneill 436bc85a93Sjmcneill static const char * const compatible[] = { 446bc85a93Sjmcneill "simple-framebuffer", 456bc85a93Sjmcneill NULL 466bc85a93Sjmcneill }; 476bc85a93Sjmcneill 486bc85a93Sjmcneill struct simplefb_softc { 496bc85a93Sjmcneill struct genfb_softc sc_gen; 506bc85a93Sjmcneill bus_space_tag_t sc_bst; 516bc85a93Sjmcneill bus_space_handle_t sc_bsh; 526bc85a93Sjmcneill int sc_phandle; 536bc85a93Sjmcneill 546bc85a93Sjmcneill bus_addr_t sc_paddr; 556bc85a93Sjmcneill }; 566bc85a93Sjmcneill 57886c97bdSjmcneill static int simplefb_console_phandle = -1; 58886c97bdSjmcneill 596bc85a93Sjmcneill static bool 606bc85a93Sjmcneill simplefb_shutdown(device_t self, int flags) 616bc85a93Sjmcneill { 626bc85a93Sjmcneill genfb_enable_polling(self); 636bc85a93Sjmcneill return true; 646bc85a93Sjmcneill } 656bc85a93Sjmcneill 666bc85a93Sjmcneill static int 676bc85a93Sjmcneill simplefb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, lwp_t *l) 686bc85a93Sjmcneill { 696bc85a93Sjmcneill struct simplefb_softc * const sc = v; 706bc85a93Sjmcneill struct wsdisplayio_bus_id *busid; 716bc85a93Sjmcneill struct wsdisplayio_fbinfo *fbi; 726bc85a93Sjmcneill struct rasops_info *ri; 7337ec32d2Sjmcneill u_int video; 746bc85a93Sjmcneill int error; 756bc85a93Sjmcneill 766bc85a93Sjmcneill switch (cmd) { 776bc85a93Sjmcneill case WSDISPLAYIO_GTYPE: 786bc85a93Sjmcneill *(u_int *)data = WSDISPLAY_TYPE_GENFB; 796bc85a93Sjmcneill return 0; 806bc85a93Sjmcneill case WSDISPLAYIO_GET_BUSID: 816bc85a93Sjmcneill busid = data; 826bc85a93Sjmcneill busid->bus_type = WSDISPLAYIO_BUS_SOC; 836bc85a93Sjmcneill return 0; 846bc85a93Sjmcneill case WSDISPLAYIO_GET_FBINFO: 856bc85a93Sjmcneill fbi = data; 866bc85a93Sjmcneill ri = &sc->sc_gen.vd.active->scr_ri; 876bc85a93Sjmcneill error = wsdisplayio_get_fbinfo(ri, fbi); 881834b3d6Smacallan if (error == 0) { 891834b3d6Smacallan /* 901834b3d6Smacallan * XXX 911834b3d6Smacallan * if the fb isn't page aligned, tell wsfb to skip the 921834b3d6Smacallan * unaligned part 931834b3d6Smacallan */ 941834b3d6Smacallan fbi->fbi_fboffset = sc->sc_paddr & PAGE_MASK; 956bc85a93Sjmcneill fbi->fbi_flags |= WSFB_VRAM_IS_RAM; 961834b3d6Smacallan } 976bc85a93Sjmcneill return error; 9837ec32d2Sjmcneill case WSDISPLAYIO_SVIDEO: 9937ec32d2Sjmcneill video = *(u_int *)data; 10037ec32d2Sjmcneill if (video == WSDISPLAYIO_VIDEO_OFF) 10137ec32d2Sjmcneill pmf_event_inject(NULL, PMFE_DISPLAY_OFF); 10237ec32d2Sjmcneill else if (video == WSDISPLAYIO_VIDEO_ON) 10337ec32d2Sjmcneill pmf_event_inject(NULL, PMFE_DISPLAY_ON); 10437ec32d2Sjmcneill else 10537ec32d2Sjmcneill return EINVAL; 10637ec32d2Sjmcneill return 0; 1076bc85a93Sjmcneill default: 1086bc85a93Sjmcneill return EPASSTHROUGH; 1096bc85a93Sjmcneill } 1106bc85a93Sjmcneill } 1116bc85a93Sjmcneill 1126bc85a93Sjmcneill static paddr_t 1136bc85a93Sjmcneill simplefb_mmap(void *v, void *vs, off_t off, int prot) 1146bc85a93Sjmcneill { 1156bc85a93Sjmcneill struct simplefb_softc * const sc = v; 1166bc85a93Sjmcneill 117*5bf455f7Srin if (off < 0 || off >= (sc->sc_paddr & PAGE_MASK) + 118*5bf455f7Srin sc->sc_gen.sc_fbsize) 1196bc85a93Sjmcneill return -1; 1206bc85a93Sjmcneill 121*5bf455f7Srin return bus_space_mmap(sc->sc_bst, trunc_page(sc->sc_paddr), off, prot, 1226bc85a93Sjmcneill BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE); 1236bc85a93Sjmcneill } 1246bc85a93Sjmcneill 1256bc85a93Sjmcneill static int 1266bc85a93Sjmcneill simplefb_attach_genfb(struct simplefb_softc *sc) 1276bc85a93Sjmcneill { 1286bc85a93Sjmcneill device_t dev = sc->sc_gen.sc_dev; 1296bc85a93Sjmcneill prop_dictionary_t dict = device_properties(dev); 1306bc85a93Sjmcneill const int phandle = sc->sc_phandle; 1316bc85a93Sjmcneill struct genfb_ops ops; 1326bc85a93Sjmcneill uint32_t width, height, stride; 1336bc85a93Sjmcneill uint16_t depth; 1346bc85a93Sjmcneill const char *format; 1356bc85a93Sjmcneill bus_addr_t addr; 1366bc85a93Sjmcneill bus_size_t size; 1376bc85a93Sjmcneill 1386bc85a93Sjmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 1396bc85a93Sjmcneill aprint_error(": couldn't get registers\n"); 1406bc85a93Sjmcneill return ENXIO; 1416bc85a93Sjmcneill } 1426bc85a93Sjmcneill 143886c97bdSjmcneill if (size == 0) { 144886c97bdSjmcneill aprint_naive("\n"); 145886c97bdSjmcneill aprint_normal(": disabled\n"); 146886c97bdSjmcneill return ENXIO; 147886c97bdSjmcneill } 148886c97bdSjmcneill 1496bc85a93Sjmcneill if (of_getprop_uint32(phandle, "width", &width) != 0 || 1506bc85a93Sjmcneill of_getprop_uint32(phandle, "height", &height) != 0 || 1516bc85a93Sjmcneill of_getprop_uint32(phandle, "stride", &stride) != 0 || 1526bc85a93Sjmcneill (format = fdtbus_get_string(phandle, "format")) == NULL) { 1536bc85a93Sjmcneill aprint_error(": missing property in DT\n"); 1546bc85a93Sjmcneill return ENXIO; 1556bc85a93Sjmcneill } 1566bc85a93Sjmcneill 1576bc85a93Sjmcneill if (strcmp(format, "a8b8g8r8") == 0 || 1586bc85a93Sjmcneill strcmp(format, "x8r8g8b8") == 0) { 1596bc85a93Sjmcneill depth = 32; 1606bc85a93Sjmcneill } else if (strcmp(format, "r5g6b5") == 0) { 1616bc85a93Sjmcneill depth = 16; 1626bc85a93Sjmcneill } else { 1636bc85a93Sjmcneill aprint_error(": unsupported format '%s'\n", format); 1646bc85a93Sjmcneill return ENXIO; 1656bc85a93Sjmcneill } 1666bc85a93Sjmcneill 1678694f07cSryo if (bus_space_map(sc->sc_bst, addr, size, 1688694f07cSryo BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE, 1696bc85a93Sjmcneill &sc->sc_bsh) != 0) { 1706bc85a93Sjmcneill aprint_error(": failed to map fb\n"); 1716bc85a93Sjmcneill return ENXIO; 1726bc85a93Sjmcneill } 1736bc85a93Sjmcneill 1746bc85a93Sjmcneill sc->sc_paddr = addr; 1756bc85a93Sjmcneill 1766bc85a93Sjmcneill prop_dictionary_set_uint32(dict, "width", width); 1776bc85a93Sjmcneill prop_dictionary_set_uint32(dict, "height", height); 1786bc85a93Sjmcneill prop_dictionary_set_uint8(dict, "depth", depth); 1796bc85a93Sjmcneill prop_dictionary_set_uint16(dict, "linebytes", stride); 1806bc85a93Sjmcneill prop_dictionary_set_uint32(dict, "address", addr); 1818694f07cSryo prop_dictionary_set_uint64(dict, "virtual_address", 1826bc85a93Sjmcneill (uintptr_t)bus_space_vaddr(sc->sc_bst, sc->sc_bsh)); 1836bc85a93Sjmcneill 1846bc85a93Sjmcneill genfb_init(&sc->sc_gen); 1856bc85a93Sjmcneill 1866bc85a93Sjmcneill if (sc->sc_gen.sc_width == 0 || sc->sc_gen.sc_fbsize == 0) { 1876bc85a93Sjmcneill aprint_normal(": disabled\n"); 1886bc85a93Sjmcneill return ENXIO; 1896bc85a93Sjmcneill } 1906bc85a93Sjmcneill 1916bc85a93Sjmcneill aprint_naive("\n"); 1926bc85a93Sjmcneill aprint_normal(": Simple Framebuffer (%ux%u %u-bpp @ 0x%" PRIxBUSADDR ")\n", 1936bc85a93Sjmcneill width, height, depth, addr); 1946bc85a93Sjmcneill 1956bc85a93Sjmcneill pmf_device_register1(dev, NULL, NULL, simplefb_shutdown); 1966bc85a93Sjmcneill 1976bc85a93Sjmcneill memset(&ops, 0, sizeof(ops)); 1986bc85a93Sjmcneill ops.genfb_ioctl = simplefb_ioctl; 1996bc85a93Sjmcneill ops.genfb_mmap = simplefb_mmap; 2006bc85a93Sjmcneill 2019e659622Sjmcneill #ifdef WSDISPLAY_MULTICONS 2029e659622Sjmcneill const bool is_console = true; 203e1cc6694Sjmcneill genfb_cnattach(); 2049e659622Sjmcneill #else 205886c97bdSjmcneill const bool is_console = phandle == simplefb_console_phandle; 2066bc85a93Sjmcneill if (is_console) 2076bc85a93Sjmcneill aprint_normal_dev(sc->sc_gen.sc_dev, 2086bc85a93Sjmcneill "switching to framebuffer console\n"); 2099e659622Sjmcneill #endif 2109e659622Sjmcneill 2119e659622Sjmcneill prop_dictionary_set_bool(dict, "is_console", is_console); 2126bc85a93Sjmcneill 2136bc85a93Sjmcneill genfb_attach(&sc->sc_gen, &ops); 2146bc85a93Sjmcneill 2156bc85a93Sjmcneill return 0; 2166bc85a93Sjmcneill } 2176bc85a93Sjmcneill 2186bc85a93Sjmcneill static int 2196bc85a93Sjmcneill simplefb_match(device_t parent, cfdata_t cf, void *aux) 2206bc85a93Sjmcneill { 2216bc85a93Sjmcneill struct fdt_attach_args * const faa = aux; 2226bc85a93Sjmcneill 2236bc85a93Sjmcneill return of_match_compatible(faa->faa_phandle, compatible); 2246bc85a93Sjmcneill } 2256bc85a93Sjmcneill 2266bc85a93Sjmcneill static void 2276bc85a93Sjmcneill simplefb_attach(device_t parent, device_t self, void *aux) 2286bc85a93Sjmcneill { 2296bc85a93Sjmcneill struct simplefb_softc * const sc = device_private(self); 2306bc85a93Sjmcneill struct fdt_attach_args * const faa = aux; 2316bc85a93Sjmcneill const int phandle = faa->faa_phandle; 2326bc85a93Sjmcneill 2336bc85a93Sjmcneill sc->sc_gen.sc_dev = self; 2346bc85a93Sjmcneill sc->sc_phandle = phandle; 2356bc85a93Sjmcneill sc->sc_bst = faa->faa_bst; 2366bc85a93Sjmcneill 2376bc85a93Sjmcneill if (simplefb_attach_genfb(sc) != 0) 2386bc85a93Sjmcneill return; 2396bc85a93Sjmcneill } 2406bc85a93Sjmcneill 2416bc85a93Sjmcneill CFATTACH_DECL_NEW(simplefb, sizeof(struct simplefb_softc), 2426bc85a93Sjmcneill simplefb_match, simplefb_attach, NULL, NULL); 243886c97bdSjmcneill 244886c97bdSjmcneill static int 245886c97bdSjmcneill simplefb_console_match(int phandle) 246886c97bdSjmcneill { 247886c97bdSjmcneill return of_match_compatible(phandle, compatible); 248886c97bdSjmcneill } 249886c97bdSjmcneill 250886c97bdSjmcneill static void 251886c97bdSjmcneill simplefb_console_consinit(struct fdt_attach_args *faa, u_int uart_freq) 252886c97bdSjmcneill { 253886c97bdSjmcneill simplefb_console_phandle = faa->faa_phandle; 254886c97bdSjmcneill genfb_cnattach(); 255886c97bdSjmcneill } 256886c97bdSjmcneill 257886c97bdSjmcneill static const struct fdt_console simplefb_fdt_console = { 258886c97bdSjmcneill .match = simplefb_console_match, 259886c97bdSjmcneill .consinit = simplefb_console_consinit 260886c97bdSjmcneill }; 261886c97bdSjmcneill 262886c97bdSjmcneill FDT_CONSOLE(simplefb, &simplefb_fdt_console); 263