Home | History | Annotate | Line # | Download | only in sbus
genfb_sbus.c revision 1.1
      1  1.1  macallan /*	$NetBSD: genfb_sbus.c,v 1.1 2007/04/11 04:45:45 macallan Exp $ */
      2  1.1  macallan 
      3  1.1  macallan /*-
      4  1.1  macallan  * Copyright (c) 2007 Michael Lorenz
      5  1.1  macallan  * All rights reserved.
      6  1.1  macallan  *
      7  1.1  macallan  * Redistribution and use in source and binary forms, with or without
      8  1.1  macallan  * modification, are permitted provided that the following conditions
      9  1.1  macallan  * are met:
     10  1.1  macallan  * 1. Redistributions of source code must retain the above copyright
     11  1.1  macallan  *    notice, this list of conditions and the following disclaimer.
     12  1.1  macallan  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  macallan  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  macallan  *    documentation and/or other materials provided with the distribution.
     15  1.1  macallan  * 3. Neither the name of The NetBSD Foundation nor the names of its
     16  1.1  macallan  *    contributors may be used to endorse or promote products derived
     17  1.1  macallan  *    from this software without specific prior written permission.
     18  1.1  macallan  *
     19  1.1  macallan  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  macallan  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  macallan  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  macallan  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  macallan  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  macallan  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  macallan  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  macallan  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  macallan  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  macallan  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  macallan  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  macallan  */
     31  1.1  macallan 
     32  1.1  macallan /* an SBus frontend for the generic fb console driver */
     33  1.1  macallan 
     34  1.1  macallan #include <sys/cdefs.h>
     35  1.1  macallan __KERNEL_RCSID(0, "$NetBSD: genfb_sbus.c,v 1.1 2007/04/11 04:45:45 macallan Exp $");
     36  1.1  macallan 
     37  1.1  macallan #include <sys/param.h>
     38  1.1  macallan #include <sys/systm.h>
     39  1.1  macallan #include <sys/buf.h>
     40  1.1  macallan #include <sys/device.h>
     41  1.1  macallan #include <sys/ioctl.h>
     42  1.1  macallan #include <sys/conf.h>
     43  1.1  macallan 
     44  1.1  macallan #include <uvm/uvm.h>
     45  1.1  macallan 
     46  1.1  macallan #include <machine/bus.h>
     47  1.1  macallan #include <machine/autoconf.h>
     48  1.1  macallan #include <machine/pmap.h>
     49  1.1  macallan 
     50  1.1  macallan #include <dev/sbus/sbusvar.h>
     51  1.1  macallan #include <dev/sun/fbio.h>
     52  1.1  macallan #include <dev/sun/fbvar.h>
     53  1.1  macallan 
     54  1.1  macallan #include <dev/wsfb/genfbvar.h>
     55  1.1  macallan 
     56  1.1  macallan struct genfb_sbus_softc {
     57  1.1  macallan 	struct genfb_softc sc_gen;
     58  1.1  macallan 	struct sbusdev sc_sd;
     59  1.1  macallan 	bus_space_tag_t sc_tag;
     60  1.1  macallan 	paddr_t sc_paddr;
     61  1.1  macallan };
     62  1.1  macallan 
     63  1.1  macallan static int	genfb_match_sbus(struct device *, struct cfdata *, void *);
     64  1.1  macallan static void	genfb_attach_sbus(struct device *, struct device *, void *);
     65  1.1  macallan static int	genfb_ioctl_sbus(void *, void *, u_long, void *, int,
     66  1.1  macallan 				 struct lwp*);
     67  1.1  macallan static paddr_t	genfb_mmap_sbus(void *, void *, off_t, int);
     68  1.1  macallan 
     69  1.1  macallan CFATTACH_DECL(genfb_sbus, sizeof(struct genfb_sbus_softc),
     70  1.1  macallan     genfb_match_sbus, genfb_attach_sbus, NULL, NULL);
     71  1.1  macallan 
     72  1.1  macallan /*
     73  1.1  macallan  * Match a graphics device.
     74  1.1  macallan  */
     75  1.1  macallan static int
     76  1.1  macallan genfb_match_sbus(struct device *parent,	struct cfdata *cf, void *aux)
     77  1.1  macallan {
     78  1.1  macallan 	struct sbus_attach_args *sa = aux;
     79  1.1  macallan 
     80  1.1  macallan 	/* if there's no address property we can't use the device */
     81  1.1  macallan 	if (prom_getpropint(sa->sa_node, "address", -1) == -1)
     82  1.1  macallan 		return 0;
     83  1.1  macallan 	if (strcmp("display", prom_getpropstring(sa->sa_node, "device_type"))
     84  1.1  macallan 	    == 0)
     85  1.1  macallan 		return 1;
     86  1.1  macallan 
     87  1.1  macallan 	return 0;
     88  1.1  macallan }
     89  1.1  macallan 
     90  1.1  macallan /*
     91  1.1  macallan  * Attach a display.  We need to notice if it is the console, too.
     92  1.1  macallan  */
     93  1.1  macallan static void
     94  1.1  macallan genfb_attach_sbus(struct device *parent, struct device *self, void *args)
     95  1.1  macallan {
     96  1.1  macallan 	struct genfb_sbus_softc *sc = (struct genfb_sbus_softc *)self;
     97  1.1  macallan 	struct sbusdev *sd = &sc->sc_sd;
     98  1.1  macallan 	struct sbus_attach_args *sa = args;
     99  1.1  macallan 	struct genfb_ops ops;
    100  1.1  macallan 	prop_dictionary_t dict;
    101  1.1  macallan 	bus_space_handle_t bh;
    102  1.1  macallan 	paddr_t fbpa;
    103  1.1  macallan 	vaddr_t fbva;
    104  1.1  macallan 	int node = sa->sa_node;
    105  1.1  macallan 	int isconsole;
    106  1.1  macallan 
    107  1.1  macallan 	aprint_normal("\n");
    108  1.1  macallan 	/* Remember cookies for genfb_mmap_sbus() */
    109  1.1  macallan 	sc->sc_tag = sa->sa_bustag;
    110  1.1  macallan 	sc->sc_paddr = sbus_bus_addr(sa->sa_bustag, sa->sa_slot, sa->sa_offset);
    111  1.1  macallan 
    112  1.1  macallan 	/* read geometry information from the device tree */
    113  1.1  macallan 	sc->sc_gen.sc_width = prom_getpropint(sa->sa_node, "width", 1152);
    114  1.1  macallan 	sc->sc_gen.sc_height = prom_getpropint(sa->sa_node, "height", 900);
    115  1.1  macallan 	sc->sc_gen.sc_depth = prom_getpropint(sa->sa_node, "depth", 8);
    116  1.1  macallan 	sc->sc_gen.sc_stride = prom_getpropint(sa->sa_node, "linebytes",
    117  1.1  macallan 	    (sc->sc_gen.sc_width * sc->sc_gen.sc_depth + 7) >> 3 );
    118  1.1  macallan 	sc->sc_gen.sc_fbsize = sc->sc_gen.sc_width * sc->sc_gen.sc_stride;
    119  1.1  macallan 	fbva = (uint32_t)prom_getpropint(sa->sa_node, "address", 0);
    120  1.1  macallan 	if (fbva == 0)
    121  1.1  macallan 		panic("this fb has no address property\n");
    122  1.1  macallan 	aprint_normal("%s: %d x %d at %d bit\n", self->dv_xname,
    123  1.1  macallan 	    sc->sc_gen.sc_width, sc->sc_gen.sc_height, sc->sc_gen.sc_depth);
    124  1.1  macallan 
    125  1.1  macallan 	pmap_extract(pmap_kernel(), fbva, &fbpa);
    126  1.1  macallan 	sc->sc_gen.sc_fboffset = (fbpa & 0x01ffffff) -
    127  1.1  macallan 	    (sc->sc_paddr & 0x01ffffff);
    128  1.1  macallan 	aprint_normal("%s: framebuffer at offset 0x%x\n", self->dv_xname,
    129  1.1  macallan 	    (uint32_t)sc->sc_gen.sc_fboffset);
    130  1.1  macallan 
    131  1.1  macallan #if notyet
    132  1.1  macallan 	if (sc->sc_gen.sc_depth <= 8) {
    133  1.1  macallan 		/* setup some ANSIish colour map */
    134  1.1  macallan 		char boo[256];
    135  1.1  macallan 		snprintf(boo, 256, "\" pal!\" %x %x %x %x %x call",
    136  1.1  macallan 		    sa->sa_node, 0, 0xa0, 0xa0, 0);
    137  1.1  macallan 		prom_interpret(boo);
    138  1.1  macallan 	}
    139  1.1  macallan #endif
    140  1.1  macallan 
    141  1.1  macallan 	isconsole = fb_is_console(node);
    142  1.1  macallan 	dict = device_properties(self);
    143  1.1  macallan 	prop_dictionary_set_bool(dict, "is_console", isconsole);
    144  1.1  macallan 
    145  1.1  macallan 	if (sbus_bus_map(sa->sa_bustag,
    146  1.1  macallan 			 sa->sa_slot,
    147  1.1  macallan 			 sa->sa_offset + sc->sc_gen.sc_fboffset,
    148  1.1  macallan 			 sc->sc_gen.sc_fbsize,
    149  1.1  macallan 			 BUS_SPACE_MAP_LINEAR, &bh) != 0) {
    150  1.1  macallan 		printf("%s: cannot map framebuffer\n", self->dv_xname);
    151  1.1  macallan 		return;
    152  1.1  macallan 	}
    153  1.1  macallan 	sc->sc_gen.sc_fbaddr = (void *)bus_space_vaddr(sa->sa_bustag, bh);
    154  1.1  macallan 
    155  1.1  macallan 	sbus_establish(sd, self);
    156  1.1  macallan 	ops.genfb_ioctl = genfb_ioctl_sbus;
    157  1.1  macallan 	ops.genfb_mmap = genfb_mmap_sbus;
    158  1.1  macallan 
    159  1.1  macallan 	genfb_attach(&sc->sc_gen, &ops);
    160  1.1  macallan }
    161  1.1  macallan 
    162  1.1  macallan static int
    163  1.1  macallan genfb_ioctl_sbus(void *v, void *vs, u_long cmd, void *data, int flag,
    164  1.1  macallan     struct lwp *l)
    165  1.1  macallan {
    166  1.1  macallan 
    167  1.1  macallan 	switch (cmd) {
    168  1.1  macallan 		case WSDISPLAYIO_GTYPE:
    169  1.1  macallan 			*(u_int *)data = WSDISPLAY_TYPE_GENFB;
    170  1.1  macallan 			return 0;
    171  1.1  macallan 	}
    172  1.1  macallan 
    173  1.1  macallan 	return EPASSTHROUGH;
    174  1.1  macallan }
    175  1.1  macallan 
    176  1.1  macallan static paddr_t
    177  1.1  macallan genfb_mmap_sbus(void *v, void *vs, off_t offset, int prot)
    178  1.1  macallan {
    179  1.1  macallan 	struct genfb_sbus_softc *sc = v;
    180  1.1  macallan 
    181  1.1  macallan 	/* regular fb mapping at 0 */
    182  1.1  macallan 	if ((offset >= 0) && (offset < sc->sc_gen.sc_fbsize)) {
    183  1.1  macallan 
    184  1.1  macallan 		return bus_space_mmap(sc->sc_tag, sc->sc_paddr,
    185  1.1  macallan 		    sc->sc_gen.sc_fboffset + offset, prot,
    186  1.1  macallan 		    BUS_SPACE_MAP_LINEAR);
    187  1.1  macallan 	}
    188  1.1  macallan 
    189  1.1  macallan 	return -1;
    190  1.1  macallan }
    191