1 1.13 riastrad /* $NetBSD: genfb_sbus.c,v 1.13 2020/09/06 17:22:44 riastradh 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 * 16 1.1 macallan * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 1.1 macallan * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 1.1 macallan * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 1.1 macallan * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 1.1 macallan * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 1.1 macallan * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 1.1 macallan * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 1.1 macallan * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 1.1 macallan * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 1.1 macallan * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 1.1 macallan * POSSIBILITY OF SUCH DAMAGE. 27 1.1 macallan */ 28 1.1 macallan 29 1.1 macallan /* an SBus frontend for the generic fb console driver */ 30 1.1 macallan 31 1.1 macallan #include <sys/cdefs.h> 32 1.13 riastrad __KERNEL_RCSID(0, "$NetBSD: genfb_sbus.c,v 1.13 2020/09/06 17:22:44 riastradh Exp $"); 33 1.1 macallan 34 1.1 macallan #include <sys/param.h> 35 1.13 riastrad #include <sys/types.h> 36 1.13 riastrad 37 1.1 macallan #include <sys/buf.h> 38 1.13 riastrad #include <sys/bus.h> 39 1.13 riastrad #include <sys/conf.h> 40 1.1 macallan #include <sys/device.h> 41 1.1 macallan #include <sys/ioctl.h> 42 1.13 riastrad #include <sys/systm.h> 43 1.13 riastrad 44 1.13 riastrad #include <uvm/uvm_extern.h> 45 1.1 macallan 46 1.1 macallan #include <machine/autoconf.h> 47 1.1 macallan 48 1.1 macallan #include <dev/sbus/sbusvar.h> 49 1.1 macallan #include <dev/sun/fbio.h> 50 1.1 macallan #include <dev/sun/fbvar.h> 51 1.1 macallan #include <dev/wsfb/genfbvar.h> 52 1.1 macallan 53 1.1 macallan struct genfb_sbus_softc { 54 1.1 macallan struct genfb_softc sc_gen; 55 1.1 macallan bus_space_tag_t sc_tag; 56 1.1 macallan paddr_t sc_paddr; 57 1.1 macallan }; 58 1.1 macallan 59 1.7 cegger static int genfb_match_sbus(device_t, cfdata_t, void *); 60 1.7 cegger static void genfb_attach_sbus(device_t, device_t, void *); 61 1.1 macallan static int genfb_ioctl_sbus(void *, void *, u_long, void *, int, 62 1.1 macallan struct lwp*); 63 1.1 macallan static paddr_t genfb_mmap_sbus(void *, void *, off_t, int); 64 1.1 macallan 65 1.10 macallan CFATTACH_DECL_NEW(genfb_sbus, sizeof(struct genfb_sbus_softc), 66 1.1 macallan genfb_match_sbus, genfb_attach_sbus, NULL, NULL); 67 1.1 macallan 68 1.1 macallan /* 69 1.1 macallan * Match a graphics device. 70 1.1 macallan */ 71 1.1 macallan static int 72 1.9 tsutsui genfb_match_sbus(device_t parent, cfdata_t cf, void *aux) 73 1.1 macallan { 74 1.1 macallan struct sbus_attach_args *sa = aux; 75 1.1 macallan 76 1.1 macallan /* if there's no address property we can't use the device */ 77 1.1 macallan if (prom_getpropint(sa->sa_node, "address", -1) == -1) 78 1.1 macallan return 0; 79 1.1 macallan if (strcmp("display", prom_getpropstring(sa->sa_node, "device_type")) 80 1.1 macallan == 0) 81 1.1 macallan return 1; 82 1.1 macallan 83 1.1 macallan return 0; 84 1.1 macallan } 85 1.1 macallan 86 1.1 macallan /* 87 1.1 macallan * Attach a display. We need to notice if it is the console, too. 88 1.1 macallan */ 89 1.1 macallan static void 90 1.7 cegger genfb_attach_sbus(device_t parent, device_t self, void *args) 91 1.1 macallan { 92 1.9 tsutsui struct genfb_sbus_softc *sc = device_private(self); 93 1.1 macallan struct sbus_attach_args *sa = args; 94 1.11 riastrad static const struct genfb_ops zero_ops; 95 1.11 riastrad struct genfb_ops ops = zero_ops; 96 1.1 macallan prop_dictionary_t dict; 97 1.1 macallan bus_space_handle_t bh; 98 1.1 macallan paddr_t fbpa; 99 1.1 macallan vaddr_t fbva; 100 1.1 macallan int node = sa->sa_node; 101 1.1 macallan int isconsole; 102 1.1 macallan 103 1.1 macallan aprint_normal("\n"); 104 1.10 macallan sc->sc_gen.sc_dev = self; 105 1.1 macallan /* Remember cookies for genfb_mmap_sbus() */ 106 1.1 macallan sc->sc_tag = sa->sa_bustag; 107 1.1 macallan sc->sc_paddr = sbus_bus_addr(sa->sa_bustag, sa->sa_slot, sa->sa_offset); 108 1.1 macallan 109 1.1 macallan /* read geometry information from the device tree */ 110 1.1 macallan sc->sc_gen.sc_width = prom_getpropint(sa->sa_node, "width", 1152); 111 1.1 macallan sc->sc_gen.sc_height = prom_getpropint(sa->sa_node, "height", 900); 112 1.1 macallan sc->sc_gen.sc_depth = prom_getpropint(sa->sa_node, "depth", 8); 113 1.1 macallan sc->sc_gen.sc_stride = prom_getpropint(sa->sa_node, "linebytes", 114 1.1 macallan (sc->sc_gen.sc_width * sc->sc_gen.sc_depth + 7) >> 3 ); 115 1.2 macallan sc->sc_gen.sc_fbsize = sc->sc_gen.sc_height * sc->sc_gen.sc_stride; 116 1.1 macallan fbva = (uint32_t)prom_getpropint(sa->sa_node, "address", 0); 117 1.1 macallan if (fbva == 0) 118 1.1 macallan panic("this fb has no address property\n"); 119 1.4 cegger aprint_normal_dev(self, "%d x %d at %d bit\n", 120 1.1 macallan sc->sc_gen.sc_width, sc->sc_gen.sc_height, sc->sc_gen.sc_depth); 121 1.1 macallan 122 1.1 macallan pmap_extract(pmap_kernel(), fbva, &fbpa); 123 1.1 macallan sc->sc_gen.sc_fboffset = (fbpa & 0x01ffffff) - 124 1.1 macallan (sc->sc_paddr & 0x01ffffff); 125 1.4 cegger aprint_normal_dev(self, "framebuffer at offset 0x%x\n", 126 1.1 macallan (uint32_t)sc->sc_gen.sc_fboffset); 127 1.1 macallan 128 1.1 macallan #if notyet 129 1.1 macallan if (sc->sc_gen.sc_depth <= 8) { 130 1.1 macallan /* setup some ANSIish colour map */ 131 1.1 macallan char boo[256]; 132 1.1 macallan snprintf(boo, 256, "\" pal!\" %x %x %x %x %x call", 133 1.1 macallan sa->sa_node, 0, 0xa0, 0xa0, 0); 134 1.1 macallan prom_interpret(boo); 135 1.1 macallan } 136 1.1 macallan #endif 137 1.1 macallan 138 1.1 macallan isconsole = fb_is_console(node); 139 1.1 macallan dict = device_properties(self); 140 1.1 macallan prop_dictionary_set_bool(dict, "is_console", isconsole); 141 1.1 macallan 142 1.1 macallan if (sbus_bus_map(sa->sa_bustag, 143 1.1 macallan sa->sa_slot, 144 1.1 macallan sa->sa_offset + sc->sc_gen.sc_fboffset, 145 1.1 macallan sc->sc_gen.sc_fbsize, 146 1.1 macallan BUS_SPACE_MAP_LINEAR, &bh) != 0) { 147 1.4 cegger aprint_error_dev(self, "cannot map framebuffer\n"); 148 1.1 macallan return; 149 1.1 macallan } 150 1.1 macallan sc->sc_gen.sc_fbaddr = (void *)bus_space_vaddr(sa->sa_bustag, bh); 151 1.1 macallan 152 1.1 macallan ops.genfb_ioctl = genfb_ioctl_sbus; 153 1.1 macallan ops.genfb_mmap = genfb_mmap_sbus; 154 1.1 macallan 155 1.1 macallan genfb_attach(&sc->sc_gen, &ops); 156 1.1 macallan } 157 1.1 macallan 158 1.1 macallan static int 159 1.1 macallan genfb_ioctl_sbus(void *v, void *vs, u_long cmd, void *data, int flag, 160 1.1 macallan struct lwp *l) 161 1.1 macallan { 162 1.1 macallan 163 1.1 macallan switch (cmd) { 164 1.1 macallan case WSDISPLAYIO_GTYPE: 165 1.1 macallan *(u_int *)data = WSDISPLAY_TYPE_GENFB; 166 1.1 macallan return 0; 167 1.1 macallan } 168 1.1 macallan 169 1.1 macallan return EPASSTHROUGH; 170 1.1 macallan } 171 1.1 macallan 172 1.1 macallan static paddr_t 173 1.1 macallan genfb_mmap_sbus(void *v, void *vs, off_t offset, int prot) 174 1.1 macallan { 175 1.1 macallan struct genfb_sbus_softc *sc = v; 176 1.1 macallan 177 1.1 macallan /* regular fb mapping at 0 */ 178 1.1 macallan if ((offset >= 0) && (offset < sc->sc_gen.sc_fbsize)) { 179 1.1 macallan 180 1.1 macallan return bus_space_mmap(sc->sc_tag, sc->sc_paddr, 181 1.1 macallan sc->sc_gen.sc_fboffset + offset, prot, 182 1.1 macallan BUS_SPACE_MAP_LINEAR); 183 1.1 macallan } 184 1.1 macallan 185 1.1 macallan return -1; 186 1.1 macallan } 187