Home | History | Annotate | Line # | Download | only in sbus
      1 /*	$NetBSD: genfb_sbus.c,v 1.14 2025/10/04 03:58:38 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2007 Michael Lorenz
      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 /* an SBus frontend for the generic fb console driver */
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: genfb_sbus.c,v 1.14 2025/10/04 03:58:38 thorpej Exp $");
     33 
     34 #include <sys/param.h>
     35 #include <sys/types.h>
     36 
     37 #include <sys/buf.h>
     38 #include <sys/bus.h>
     39 #include <sys/conf.h>
     40 #include <sys/device.h>
     41 #include <sys/ioctl.h>
     42 #include <sys/systm.h>
     43 
     44 #include <uvm/uvm_extern.h>
     45 
     46 #include <machine/autoconf.h>
     47 
     48 #include <dev/sbus/sbusvar.h>
     49 #include <dev/sun/fbio.h>
     50 #include <dev/sun/fbvar.h>
     51 #include <dev/wsfb/genfbvar.h>
     52 
     53 struct genfb_sbus_softc {
     54 	struct genfb_softc sc_gen;
     55 	bus_space_tag_t sc_tag;
     56 	paddr_t sc_paddr;
     57 };
     58 
     59 static int	genfb_match_sbus(device_t, cfdata_t, void *);
     60 static void	genfb_attach_sbus(device_t, device_t, void *);
     61 static int	genfb_ioctl_sbus(void *, void *, u_long, void *, int,
     62 				 struct lwp*);
     63 static paddr_t	genfb_mmap_sbus(void *, void *, off_t, int);
     64 
     65 CFATTACH_DECL_NEW(genfb_sbus, sizeof(struct genfb_sbus_softc),
     66     genfb_match_sbus, genfb_attach_sbus, NULL, NULL);
     67 
     68 /*
     69  * Match a graphics device.
     70  */
     71 static int
     72 genfb_match_sbus(device_t parent, cfdata_t cf, void *aux)
     73 {
     74 	struct sbus_attach_args *sa = aux;
     75 
     76 	/* if there's no address property we can't use the device */
     77 	if (prom_getpropint(sa->sa_node, "address", -1) == -1)
     78 		return 0;
     79 	if (strcmp("display", prom_getpropstring(sa->sa_node, "device_type"))
     80 	    == 0)
     81 		return 1;
     82 
     83 	return 0;
     84 }
     85 
     86 /*
     87  * Attach a display.  We need to notice if it is the console, too.
     88  */
     89 static void
     90 genfb_attach_sbus(device_t parent, device_t self, void *args)
     91 {
     92 	struct genfb_sbus_softc *sc = device_private(self);
     93 	struct sbus_attach_args *sa = args;
     94 	static const struct genfb_ops zero_ops;
     95 	struct genfb_ops ops = zero_ops;
     96 	bus_space_handle_t bh;
     97 	paddr_t fbpa;
     98 	vaddr_t fbva;
     99 	int node = sa->sa_node;
    100 
    101 	aprint_normal("\n");
    102 	sc->sc_gen.sc_dev = self;
    103 	/* Remember cookies for genfb_mmap_sbus() */
    104 	sc->sc_tag = sa->sa_bustag;
    105 	sc->sc_paddr = sbus_bus_addr(sa->sa_bustag, sa->sa_slot, sa->sa_offset);
    106 
    107 	/* read geometry information from the device tree */
    108 	sc->sc_gen.sc_width = prom_getpropint(sa->sa_node, "width", 1152);
    109 	sc->sc_gen.sc_height = prom_getpropint(sa->sa_node, "height", 900);
    110 	sc->sc_gen.sc_depth = prom_getpropint(sa->sa_node, "depth", 8);
    111 	sc->sc_gen.sc_stride = prom_getpropint(sa->sa_node, "linebytes",
    112 	    (sc->sc_gen.sc_width * sc->sc_gen.sc_depth + 7) >> 3 );
    113 	sc->sc_gen.sc_fbsize = sc->sc_gen.sc_height * sc->sc_gen.sc_stride;
    114 	fbva = (uint32_t)prom_getpropint(sa->sa_node, "address", 0);
    115 	if (fbva == 0)
    116 		panic("this fb has no address property\n");
    117 	aprint_normal_dev(self, "%d x %d at %d bit\n",
    118 	    sc->sc_gen.sc_width, sc->sc_gen.sc_height, sc->sc_gen.sc_depth);
    119 
    120 	pmap_extract(pmap_kernel(), fbva, &fbpa);
    121 	sc->sc_gen.sc_fboffset = (fbpa & 0x01ffffff) -
    122 	    (sc->sc_paddr & 0x01ffffff);
    123 	aprint_normal_dev(self, "framebuffer at offset 0x%x\n",
    124 	    (uint32_t)sc->sc_gen.sc_fboffset);
    125 
    126 #if notyet
    127 	if (sc->sc_gen.sc_depth <= 8) {
    128 		/* setup some ANSIish colour map */
    129 		char boo[256];
    130 		snprintf(boo, 256, "\" pal!\" %x %x %x %x %x call",
    131 		    sa->sa_node, 0, 0xa0, 0xa0, 0);
    132 		prom_interpret(boo);
    133 	}
    134 #endif
    135 
    136 	device_setprop_bool(self, "is_console", fb_is_console(node));
    137 
    138 	if (sbus_bus_map(sa->sa_bustag,
    139 			 sa->sa_slot,
    140 			 sa->sa_offset + sc->sc_gen.sc_fboffset,
    141 			 sc->sc_gen.sc_fbsize,
    142 			 BUS_SPACE_MAP_LINEAR, &bh) != 0) {
    143 		aprint_error_dev(self, "cannot map framebuffer\n");
    144 		return;
    145 	}
    146 	sc->sc_gen.sc_fbaddr = (void *)bus_space_vaddr(sa->sa_bustag, bh);
    147 
    148 	ops.genfb_ioctl = genfb_ioctl_sbus;
    149 	ops.genfb_mmap = genfb_mmap_sbus;
    150 
    151 	genfb_attach(&sc->sc_gen, &ops);
    152 }
    153 
    154 static int
    155 genfb_ioctl_sbus(void *v, void *vs, u_long cmd, void *data, int flag,
    156     struct lwp *l)
    157 {
    158 
    159 	switch (cmd) {
    160 		case WSDISPLAYIO_GTYPE:
    161 			*(u_int *)data = WSDISPLAY_TYPE_GENFB;
    162 			return 0;
    163 	}
    164 
    165 	return EPASSTHROUGH;
    166 }
    167 
    168 static paddr_t
    169 genfb_mmap_sbus(void *v, void *vs, off_t offset, int prot)
    170 {
    171 	struct genfb_sbus_softc *sc = v;
    172 
    173 	/* regular fb mapping at 0 */
    174 	if ((offset >= 0) && (offset < sc->sc_gen.sc_fbsize)) {
    175 
    176 		return bus_space_mmap(sc->sc_tag, sc->sc_paddr,
    177 		    sc->sc_gen.sc_fboffset + offset, prot,
    178 		    BUS_SPACE_MAP_LINEAR);
    179 	}
    180 
    181 	return -1;
    182 }
    183