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