Home | History | Annotate | Line # | Download | only in broadcom
bcm2835_genfb.c revision 1.2
      1 /* $NetBSD: bcm2835_genfb.c,v 1.2 2013/01/09 23:58:40 jmcneill 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.2 2013/01/09 23:58:40 jmcneill 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 <arm/broadcom/bcm_amba.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 
     62 CFATTACH_DECL_NEW(bcmgenfb, sizeof(struct bcmgenfb_softc),
     63     bcmgenfb_match, bcmgenfb_attach, NULL, NULL);
     64 
     65 static int
     66 bcmgenfb_match(device_t parent, cfdata_t match, void *aux)
     67 {
     68 	struct amba_attach_args *aaa = aux;
     69 
     70 	return !strcmp(aaa->aaa_name, "fb");
     71 }
     72 
     73 static void
     74 bcmgenfb_attach(device_t parent, device_t self, void *aux)
     75 {
     76 	struct bcmgenfb_softc *sc = device_private(self);
     77 	struct amba_attach_args *aaa = aux;
     78 	prop_dictionary_t dict = device_properties(self);
     79 	struct genfb_ops ops;
     80 	int error;
     81 
     82 	sc->sc_gen.sc_dev = self;
     83 	sc->sc_iot = aaa->aaa_iot;
     84 
     85 	sc->sc_wstype = WSDISPLAY_TYPE_VC4;
     86 	prop_dictionary_get_uint32(dict, "wsdisplay_type", &sc->sc_wstype);
     87 
     88 	genfb_init(&sc->sc_gen);
     89 
     90 	if (sc->sc_gen.sc_width == 0 ||
     91 	    sc->sc_gen.sc_fbsize == 0) {
     92 		aprint_normal(": disabled\n");
     93 		return;
     94 	}
     95 
     96 	error = bus_space_map(sc->sc_iot, sc->sc_gen.sc_fboffset,
     97 	    sc->sc_gen.sc_fbsize,
     98 	    BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE, &sc->sc_ioh);
     99 	if (error) {
    100 		aprint_error_dev(self, "couldn't map framebuffer (%d)\n",
    101 		    error);
    102 		return;
    103 	}
    104 	sc->sc_gen.sc_fbaddr = bus_space_vaddr(sc->sc_iot, sc->sc_ioh);
    105 
    106 	memset(&ops, 0, sizeof(ops));
    107 	ops.genfb_ioctl = bcmgenfb_ioctl;
    108 	ops.genfb_mmap = bcmgenfb_mmap;
    109 
    110 	aprint_naive("\n");
    111 	aprint_normal(": switching to framebuffer console\n");
    112 
    113 	genfb_attach(&sc->sc_gen, &ops);
    114 }
    115 
    116 static int
    117 bcmgenfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, lwp_t *l)
    118 {
    119 	struct bcmgenfb_softc *sc = v;
    120 	struct wsdisplayio_bus_id *busid;
    121 
    122 	switch (cmd) {
    123 	case WSDISPLAYIO_GTYPE:
    124 		*(u_int *)data = sc->sc_wstype;
    125 		return 0;
    126 	case WSDISPLAYIO_GET_BUSID:
    127 		busid = data;
    128 		busid->bus_type = WSDISPLAYIO_BUS_SOC;
    129 		return 0;
    130 	default:
    131 		return EPASSTHROUGH;
    132 	}
    133 }
    134 
    135 static paddr_t
    136 bcmgenfb_mmap(void *v, void *vs, off_t offset, int prot)
    137 {
    138 	struct bcmgenfb_softc *sc = v;
    139 
    140 	if (offset < 0 || offset >= sc->sc_gen.sc_fbsize)
    141 		return -1;
    142 
    143 	return bus_space_mmap(sc->sc_iot, sc->sc_gen.sc_fboffset, offset,
    144 	    prot, BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE);
    145 }
    146