Home | History | Annotate | Line # | Download | only in broadcom
      1 /* $NetBSD: bcm2835_genfb.c,v 1.12 2021/01/27 03:10:19 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2013 Jared D. 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 /*
     30  * Generic framebuffer console driver
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: bcm2835_genfb.c,v 1.12 2021/01/27 03:10:19 thorpej Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/types.h>
     38 #include <sys/bus.h>
     39 #include <sys/conf.h>
     40 #include <sys/device.h>
     41 #include <sys/endian.h>
     42 #include <sys/kmem.h>
     43 #include <sys/systm.h>
     44 
     45 #include <dev/fdt/fdtvar.h>
     46 
     47 #include <dev/wsfb/genfbvar.h>
     48 
     49 struct bcmgenfb_softc {
     50 	struct genfb_softc	sc_gen;
     51 	bus_space_tag_t		sc_iot;
     52 	bus_space_handle_t	sc_ioh;
     53 
     54 	uint32_t		sc_wstype;
     55 };
     56 
     57 static int	bcmgenfb_match(device_t, cfdata_t, void *);
     58 static void	bcmgenfb_attach(device_t, device_t, void *);
     59 
     60 static int	bcmgenfb_ioctl(void *, void *, u_long, void *, int, lwp_t *);
     61 static paddr_t	bcmgenfb_mmap(void *, void *, off_t, int);
     62 static bool	bcmgenfb_shutdown(device_t, int);
     63 
     64 void		bcmgenfb_set_console_dev(device_t);
     65 void		bcmgenfb_set_ioctl(int(*)(void *, void *, u_long, void *, int, struct lwp *));
     66 void		bcmgenfb_ddb_trap_callback(int);
     67 
     68 static device_t bcmgenfb_console_dev = NULL;
     69 int (*bcmgenfb_ioctl_handler)(void *, void *, u_long, void *, int, struct lwp *) = NULL;
     70 
     71 CFATTACH_DECL_NEW(bcmgenfb, sizeof(struct bcmgenfb_softc),
     72     bcmgenfb_match, bcmgenfb_attach, NULL, NULL);
     73 
     74 static const struct device_compatible_entry compat_data[] = {
     75 	{ .compat = "brcm,bcm2835-fb" },
     76 	DEVICE_COMPAT_EOL
     77 };
     78 
     79 static int
     80 bcmgenfb_match(device_t parent, cfdata_t match, void *aux)
     81 {
     82 	struct fdt_attach_args * const faa = aux;
     83 
     84 	return of_compatible_match(faa->faa_phandle, compat_data);
     85 }
     86 
     87 static void
     88 bcmgenfb_attach(device_t parent, device_t self, void *aux)
     89 {
     90 	struct bcmgenfb_softc *sc = device_private(self);
     91 	struct fdt_attach_args * const faa = aux;
     92 	prop_dictionary_t dict = device_properties(self);
     93 	static const struct genfb_ops zero_ops;
     94 	struct genfb_ops ops = zero_ops;
     95 	bool is_console = false;
     96 	int error;
     97 
     98 	sc->sc_gen.sc_dev = self;
     99 	sc->sc_iot = faa->faa_bst;
    100 
    101 	sc->sc_wstype = WSDISPLAY_TYPE_VC4;
    102 	prop_dictionary_get_uint32(dict, "wsdisplay_type", &sc->sc_wstype);
    103 	prop_dictionary_get_bool(dict, "is_console", &is_console);
    104 #if BYTE_ORDER == BIG_ENDIAN
    105 	prop_dictionary_set_bool(dict, "is_swapped", true);
    106 #endif
    107 
    108 	genfb_init(&sc->sc_gen);
    109 
    110 	if (sc->sc_gen.sc_width == 0 ||
    111 	    sc->sc_gen.sc_fbsize == 0) {
    112 		aprint_normal(": disabled\n");
    113 		return;
    114 	}
    115 
    116 	pmf_device_register1(self, NULL, NULL, bcmgenfb_shutdown);
    117 
    118 	error = bus_space_map(sc->sc_iot, sc->sc_gen.sc_fboffset,
    119 	    sc->sc_gen.sc_fbsize,
    120 	    BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE, &sc->sc_ioh);
    121 	if (error) {
    122 		aprint_error_dev(self, "couldn't map framebuffer (%d)\n",
    123 		    error);
    124 		return;
    125 	}
    126 	sc->sc_gen.sc_fbaddr = bus_space_vaddr(sc->sc_iot, sc->sc_ioh);
    127 
    128 	ops.genfb_ioctl = bcmgenfb_ioctl;
    129 	ops.genfb_mmap = bcmgenfb_mmap;
    130 
    131 	aprint_naive("\n");
    132 
    133 	if (is_console)
    134 		aprint_normal(": switching to framebuffer console\n");
    135 	else
    136 		aprint_normal("\n");
    137 
    138 	genfb_attach(&sc->sc_gen, &ops);
    139 }
    140 
    141 static int
    142 bcmgenfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, lwp_t *l)
    143 {
    144 	struct bcmgenfb_softc *sc = v;
    145 	struct wsdisplayio_bus_id *busid;
    146 
    147 	switch (cmd) {
    148 	case WSDISPLAYIO_GTYPE:
    149 		*(u_int *)data = sc->sc_wstype;
    150 		return 0;
    151 	case WSDISPLAYIO_GET_BUSID:
    152 		busid = data;
    153 		busid->bus_type = WSDISPLAYIO_BUS_SOC;
    154 		return 0;
    155 	case WSDISPLAYIO_GET_FBINFO:
    156 		{
    157 			struct wsdisplayio_fbinfo *fbi = data;
    158 			struct rasops_info *ri = &sc->sc_gen.vd.active->scr_ri;
    159 			int ret;
    160 
    161 			ret = wsdisplayio_get_fbinfo(ri, fbi);
    162 			fbi->fbi_flags |= WSFB_VRAM_IS_RAM;
    163 			return ret;
    164 		}
    165 	default:
    166 		if (bcmgenfb_ioctl_handler != NULL)
    167 			return bcmgenfb_ioctl_handler(v, vs, cmd, data, flag, l);
    168 		return EPASSTHROUGH;
    169 	}
    170 }
    171 
    172 static paddr_t
    173 bcmgenfb_mmap(void *v, void *vs, off_t offset, int prot)
    174 {
    175 	struct bcmgenfb_softc *sc = v;
    176 
    177 	if (offset < 0 || offset >= sc->sc_gen.sc_fbsize)
    178 		return -1;
    179 
    180 	return bus_space_mmap(sc->sc_iot, sc->sc_gen.sc_fboffset, offset,
    181 	    prot, BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE);
    182 }
    183 
    184 static bool
    185 bcmgenfb_shutdown(device_t self, int flags)
    186 {
    187 	genfb_enable_polling(self);
    188 	return true;
    189 }
    190 void
    191 bcmgenfb_set_console_dev(device_t dev)
    192 {
    193 	/* skip if already set. called from each genfb0,genfb1,... */
    194 	if (bcmgenfb_console_dev != NULL)
    195 		return;
    196 
    197 	bcmgenfb_console_dev = dev;
    198 }
    199 
    200 void
    201 bcmgenfb_set_ioctl(int(*boo)(void *, void *, u_long, void *, int, struct lwp *))
    202 {
    203 	bcmgenfb_ioctl_handler = boo;
    204 }
    205 
    206 void
    207 bcmgenfb_ddb_trap_callback(int where)
    208 {
    209 	if (bcmgenfb_console_dev == NULL)
    210 		return;
    211 
    212 	if (where) {
    213 		genfb_enable_polling(bcmgenfb_console_dev);
    214 	} else {
    215 		genfb_disable_polling(bcmgenfb_console_dev);
    216 	}
    217 }
    218