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