Home | History | Annotate | Line # | Download | only in broadcom
bcm2835_genfb.c revision 1.9
      1 /* $NetBSD: bcm2835_genfb.c,v 1.9 2018/04/01 04:35:03 ryo 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.9 2018/04/01 04:35:03 ryo Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/types.h>
     38 #include <sys/systm.h>
     39 #include <sys/device.h>
     40 #include <sys/conf.h>
     41 #include <sys/bus.h>
     42 #include <sys/kmem.h>
     43 
     44 #include <dev/fdt/fdtvar.h>
     45 
     46 #include <dev/wsfb/genfbvar.h>
     47 
     48 struct bcmgenfb_softc {
     49 	struct genfb_softc	sc_gen;
     50 	bus_space_tag_t		sc_iot;
     51 	bus_space_handle_t	sc_ioh;
     52 
     53 	uint32_t		sc_wstype;
     54 };
     55 
     56 static int	bcmgenfb_match(device_t, cfdata_t, void *);
     57 static void	bcmgenfb_attach(device_t, device_t, void *);
     58 
     59 static int	bcmgenfb_ioctl(void *, void *, u_long, void *, int, lwp_t *);
     60 static paddr_t	bcmgenfb_mmap(void *, void *, off_t, int);
     61 static bool	bcmgenfb_shutdown(device_t, int);
     62 
     63 void		bcmgenfb_set_console_dev(device_t);
     64 void		bcmgenfb_set_ioctl(int(*)(void *, void *, u_long, void *, int, struct lwp *));
     65 void		bcmgenfb_ddb_trap_callback(int);
     66 
     67 static device_t bcmgenfb_console_dev = NULL;
     68 int (*bcmgenfb_ioctl_handler)(void *, void *, u_long, void *, int, struct lwp *) = NULL;
     69 
     70 CFATTACH_DECL_NEW(bcmgenfb, sizeof(struct bcmgenfb_softc),
     71     bcmgenfb_match, bcmgenfb_attach, NULL, NULL);
     72 
     73 static int
     74 bcmgenfb_match(device_t parent, cfdata_t match, void *aux)
     75 {
     76 	const char * const compatible[] = { "brcm,bcm2835-fb", NULL };
     77 	struct fdt_attach_args * const faa = aux;
     78 
     79 	return of_match_compatible(faa->faa_phandle, compatible);
     80 }
     81 
     82 static void
     83 bcmgenfb_attach(device_t parent, device_t self, void *aux)
     84 {
     85 	struct bcmgenfb_softc *sc = device_private(self);
     86 	struct fdt_attach_args * const faa = aux;
     87 	prop_dictionary_t dict = device_properties(self);
     88 	static const struct genfb_ops zero_ops;
     89 	struct genfb_ops ops = zero_ops;
     90 	bool is_console = false;
     91 	int error;
     92 
     93 	sc->sc_gen.sc_dev = self;
     94 	sc->sc_iot = faa->faa_bst;
     95 
     96 	sc->sc_wstype = WSDISPLAY_TYPE_VC4;
     97 	prop_dictionary_get_uint32(dict, "wsdisplay_type", &sc->sc_wstype);
     98 	prop_dictionary_get_bool(dict, "is_console", &is_console);
     99 
    100 	genfb_init(&sc->sc_gen);
    101 
    102 	if (sc->sc_gen.sc_width == 0 ||
    103 	    sc->sc_gen.sc_fbsize == 0) {
    104 		aprint_normal(": disabled\n");
    105 		return;
    106 	}
    107 
    108 	pmf_device_register1(self, NULL, NULL, bcmgenfb_shutdown);
    109 
    110 	error = bus_space_map(sc->sc_iot, sc->sc_gen.sc_fboffset,
    111 	    sc->sc_gen.sc_fbsize,
    112 	    BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE, &sc->sc_ioh);
    113 	if (error) {
    114 		aprint_error_dev(self, "couldn't map framebuffer (%d)\n",
    115 		    error);
    116 		return;
    117 	}
    118 	sc->sc_gen.sc_fbaddr = bus_space_vaddr(sc->sc_iot, sc->sc_ioh);
    119 
    120 	ops.genfb_ioctl = bcmgenfb_ioctl;
    121 	ops.genfb_mmap = bcmgenfb_mmap;
    122 
    123 	aprint_naive("\n");
    124 
    125 	if (is_console)
    126 		aprint_normal(": switching to framebuffer console\n");
    127 	else
    128 		aprint_normal("\n");
    129 
    130 	genfb_attach(&sc->sc_gen, &ops);
    131 }
    132 
    133 static int
    134 bcmgenfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, lwp_t *l)
    135 {
    136 	struct bcmgenfb_softc *sc = v;
    137 	struct wsdisplayio_bus_id *busid;
    138 
    139 	switch (cmd) {
    140 	case WSDISPLAYIO_GTYPE:
    141 		*(u_int *)data = sc->sc_wstype;
    142 		return 0;
    143 	case WSDISPLAYIO_GET_BUSID:
    144 		busid = data;
    145 		busid->bus_type = WSDISPLAYIO_BUS_SOC;
    146 		return 0;
    147 	case WSDISPLAYIO_GET_FBINFO:
    148 		{
    149 			struct wsdisplayio_fbinfo *fbi = data;
    150 			struct rasops_info *ri = &sc->sc_gen.vd.active->scr_ri;
    151 			int ret;
    152 
    153 			ret = wsdisplayio_get_fbinfo(ri, fbi);
    154 			fbi->fbi_flags |= WSFB_VRAM_IS_RAM;
    155 			return ret;
    156 		}
    157 	default:
    158 		if (bcmgenfb_ioctl_handler != NULL)
    159 			return bcmgenfb_ioctl_handler(v, vs, cmd, data, flag, l);
    160 		return EPASSTHROUGH;
    161 	}
    162 }
    163 
    164 static paddr_t
    165 bcmgenfb_mmap(void *v, void *vs, off_t offset, int prot)
    166 {
    167 	struct bcmgenfb_softc *sc = v;
    168 
    169 	if (offset < 0 || offset >= sc->sc_gen.sc_fbsize)
    170 		return -1;
    171 
    172 	return bus_space_mmap(sc->sc_iot, sc->sc_gen.sc_fboffset, offset,
    173 	    prot, BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE);
    174 }
    175 
    176 static bool
    177 bcmgenfb_shutdown(device_t self, int flags)
    178 {
    179 	genfb_enable_polling(self);
    180 	return true;
    181 }
    182 void
    183 bcmgenfb_set_console_dev(device_t dev)
    184 {
    185 	/* skip if already set. called from each genfb0,genfb1,... */
    186 	if (bcmgenfb_console_dev != NULL)
    187 		return;
    188 
    189 	bcmgenfb_console_dev = dev;
    190 }
    191 
    192 void
    193 bcmgenfb_set_ioctl(int(*boo)(void *, void *, u_long, void *, int, struct lwp *))
    194 {
    195 	bcmgenfb_ioctl_handler = boo;
    196 }
    197 
    198 void
    199 bcmgenfb_ddb_trap_callback(int where)
    200 {
    201 	if (bcmgenfb_console_dev == NULL)
    202 		return;
    203 
    204 	if (where) {
    205 		genfb_enable_polling(bcmgenfb_console_dev);
    206 	} else {
    207 		genfb_disable_polling(bcmgenfb_console_dev);
    208 	}
    209 }
    210