1 /*- 2 * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org> 3 * Copyright (c) 2012, 2013 The FreeBSD Foundation 4 * All rights reserved. 5 * 6 * Portions of this software were developed by Oleksandr Rybalko 7 * under sponsorship from the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 */ 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/bio.h> 37 #include <sys/bus.h> 38 #include <sys/fbio.h> 39 #include <sys/kernel.h> 40 #include <sys/malloc.h> 41 #include <sys/module.h> 42 43 #include <vm/vm.h> 44 #include <vm/pmap.h> 45 46 #include <dev/fdt/fdt_common.h> 47 #include <dev/ofw/ofw_bus.h> 48 #include <dev/ofw/ofw_bus_subr.h> 49 50 #include <dev/fb/fbreg.h> 51 #include <dev/vt/vt.h> 52 #include <dev/vt/colors/vt_termcolors.h> 53 54 #include <arm/broadcom/bcm2835/bcm2835_mbox_prop.h> 55 56 #include "fb_if.h" 57 #include "mbox_if.h" 58 59 #define FB_DEPTH 24 60 61 struct bcmsc_softc { 62 struct fb_info info; 63 int fbswap; 64 struct bcm2835_fb_config fb; 65 device_t dev; 66 }; 67 68 static int bcm_fb_probe(device_t); 69 static int bcm_fb_attach(device_t); 70 71 static int 72 bcm_fb_init(struct bcmsc_softc *sc, struct bcm2835_fb_config *fb) 73 { 74 int err; 75 76 err = 0; 77 78 memset(fb, 0, sizeof(*fb)); 79 if (bcm2835_mbox_fb_get_w_h(fb) != 0) 80 return (ENXIO); 81 fb->bpp = FB_DEPTH; 82 83 if ((err = bcm2835_mbox_fb_init(fb)) != 0) { 84 device_printf(sc->dev, "bcm2835_mbox_fb_init failed, err=%d\n", err); 85 return (ENXIO); 86 } 87 88 return (0); 89 } 90 91 static int 92 bcm_fb_setup_fbd(struct bcmsc_softc *sc) 93 { 94 struct bcm2835_fb_config fb; 95 device_t fbd; 96 int err; 97 98 err = bcm_fb_init(sc, &fb); 99 if (err) 100 return (err); 101 102 memset(&sc->info, 0, sizeof(sc->info)); 103 sc->info.fb_name = device_get_nameunit(sc->dev); 104 105 sc->info.fb_vbase = (intptr_t)pmap_mapdev(fb.base, fb.size); 106 sc->info.fb_pbase = fb.base; 107 sc->info.fb_size = fb.size; 108 sc->info.fb_bpp = sc->info.fb_depth = fb.bpp; 109 sc->info.fb_stride = fb.pitch; 110 sc->info.fb_width = fb.xres; 111 sc->info.fb_height = fb.yres; 112 113 if (sc->fbswap) { 114 switch (sc->info.fb_bpp) { 115 case 24: 116 vt_generate_cons_palette(sc->info.fb_cmap, 117 COLOR_FORMAT_RGB, 0xff, 0, 0xff, 8, 0xff, 16); 118 sc->info.fb_cmsize = 16; 119 break; 120 case 32: 121 vt_generate_cons_palette(sc->info.fb_cmap, 122 COLOR_FORMAT_RGB, 0xff, 16, 0xff, 8, 0xff, 0); 123 sc->info.fb_cmsize = 16; 124 break; 125 } 126 } 127 128 fbd = device_add_child(sc->dev, "fbd", device_get_unit(sc->dev)); 129 if (fbd == NULL) { 130 device_printf(sc->dev, "Failed to add fbd child\n"); 131 pmap_unmapdev(sc->info.fb_vbase, sc->info.fb_size); 132 return (ENXIO); 133 } else if (device_probe_and_attach(fbd) != 0) { 134 device_printf(sc->dev, "Failed to attach fbd device\n"); 135 device_delete_child(sc->dev, fbd); 136 pmap_unmapdev(sc->info.fb_vbase, sc->info.fb_size); 137 return (ENXIO); 138 } 139 140 device_printf(sc->dev, "%dx%d(%dx%d@%d,%d) %dbpp\n", fb.xres, fb.yres, 141 fb.vxres, fb.vyres, fb.xoffset, fb.yoffset, fb.bpp); 142 device_printf(sc->dev, 143 "fbswap: %d, pitch %d, base 0x%08x, screen_size %d\n", 144 sc->fbswap, fb.pitch, fb.base, fb.size); 145 146 return (0); 147 } 148 149 static int 150 bcm_fb_resync_sysctl(SYSCTL_HANDLER_ARGS) 151 { 152 struct bcmsc_softc *sc = arg1; 153 struct bcm2835_fb_config fb; 154 int val; 155 int err; 156 157 val = 0; 158 err = sysctl_handle_int(oidp, &val, 0, req); 159 if (err || !req->newptr) /* error || read request */ 160 return (err); 161 162 bcm_fb_init(sc, &fb); 163 164 return (0); 165 } 166 167 static void 168 bcm_fb_sysctl_init(struct bcmsc_softc *sc) 169 { 170 struct sysctl_ctx_list *ctx; 171 struct sysctl_oid *tree_node; 172 struct sysctl_oid_list *tree; 173 174 /* 175 * Add system sysctl tree/handlers. 176 */ 177 ctx = device_get_sysctl_ctx(sc->dev); 178 tree_node = device_get_sysctl_tree(sc->dev); 179 tree = SYSCTL_CHILDREN(tree_node); 180 SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "resync", 181 CTLFLAG_RW | CTLTYPE_UINT, sc, sizeof(*sc), 182 bcm_fb_resync_sysctl, "IU", "Set to resync framebuffer with VC"); 183 } 184 185 static int 186 bcm_fb_probe(device_t dev) 187 { 188 if (!ofw_bus_is_compatible(dev, "broadcom,bcm2835-fb")) 189 return (ENXIO); 190 191 device_set_desc(dev, "BCM2835 VT framebuffer driver"); 192 193 return (BUS_PROBE_DEFAULT); 194 } 195 196 static int 197 bcm_fb_attach(device_t dev) 198 { 199 char bootargs[2048], *n, *p, *v; 200 int err; 201 phandle_t chosen; 202 struct bcmsc_softc *sc; 203 204 sc = device_get_softc(dev); 205 sc->dev = dev; 206 207 /* Newer firmware versions needs an inverted color palette. */ 208 sc->fbswap = 0; 209 chosen = OF_finddevice("/chosen"); 210 if (chosen != 0 && 211 OF_getprop(chosen, "bootargs", &bootargs, sizeof(bootargs)) > 0) { 212 p = bootargs; 213 while ((v = strsep(&p, " ")) != NULL) { 214 if (*v == '\0') 215 continue; 216 n = strsep(&v, "="); 217 if (strcmp(n, "bcm2708_fb.fbswap") == 0 && v != NULL) 218 if (*v == '1') 219 sc->fbswap = 1; 220 } 221 } 222 223 bcm_fb_sysctl_init(sc); 224 225 err = bcm_fb_setup_fbd(sc); 226 if (err) 227 return (err); 228 229 return (0); 230 } 231 232 static struct fb_info * 233 bcm_fb_helper_getinfo(device_t dev) 234 { 235 struct bcmsc_softc *sc; 236 237 sc = device_get_softc(dev); 238 239 return (&sc->info); 240 } 241 242 static device_method_t bcm_fb_methods[] = { 243 /* Device interface */ 244 DEVMETHOD(device_probe, bcm_fb_probe), 245 DEVMETHOD(device_attach, bcm_fb_attach), 246 247 /* Framebuffer service methods */ 248 DEVMETHOD(fb_getinfo, bcm_fb_helper_getinfo), 249 250 DEVMETHOD_END 251 }; 252 253 static devclass_t bcm_fb_devclass; 254 255 static driver_t bcm_fb_driver = { 256 "fb", 257 bcm_fb_methods, 258 sizeof(struct bcmsc_softc), 259 }; 260 261 DRIVER_MODULE(bcm2835fb, ofwbus, bcm_fb_driver, bcm_fb_devclass, 0, 0); 262