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