2 * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org>
3 * Copyright (c) 2012, 2013 The FreeBSD Foundation
6 * Portions of this software were developed by Oleksandr Rybalko
7 * under sponsorship from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
46 #include <dev/fdt/fdt_common.h>
47 #include <dev/ofw/ofw_bus.h>
48 #include <dev/ofw/ofw_bus_subr.h>
50 #include <dev/fb/fbreg.h>
51 #include <dev/vt/vt.h>
52 #include <dev/vt/colors/vt_termcolors.h>
54 #include <arm/broadcom/bcm2835/bcm2835_mbox_prop.h>
64 struct bcm2835_fb_config fb;
68 static struct ofw_compat_data compat_data[] = {
69 {"broadcom,bcm2835-fb", 1},
70 {"brcm,bcm2708-fb", 1},
74 static int bcm_fb_probe(device_t);
75 static int bcm_fb_attach(device_t);
78 bcm_fb_init(struct bcmsc_softc *sc, struct bcm2835_fb_config *fb)
84 memset(fb, 0, sizeof(*fb));
85 if (bcm2835_mbox_fb_get_w_h(fb) != 0)
91 fb->xoffset = fb->yoffset = 0;
93 if ((err = bcm2835_mbox_fb_init(fb)) != 0) {
94 device_printf(sc->dev, "bcm2835_mbox_fb_init failed, err=%d\n", err);
102 bcm_fb_setup_fbd(struct bcmsc_softc *sc)
104 struct bcm2835_fb_config fb;
108 err = bcm_fb_init(sc, &fb);
112 memset(&sc->info, 0, sizeof(sc->info));
113 sc->info.fb_name = device_get_nameunit(sc->dev);
115 sc->info.fb_vbase = (intptr_t)pmap_mapdev(fb.base, fb.size);
116 sc->info.fb_pbase = fb.base;
117 sc->info.fb_size = fb.size;
118 sc->info.fb_bpp = sc->info.fb_depth = fb.bpp;
119 sc->info.fb_stride = fb.pitch;
120 sc->info.fb_width = fb.xres;
121 sc->info.fb_height = fb.yres;
122 #ifdef VM_MEMATTR_WRITE_COMBINING
123 sc->info.fb_flags = FB_FLAG_MEMATTR;
124 sc->info.fb_memattr = VM_MEMATTR_WRITE_COMBINING;
128 switch (sc->info.fb_bpp) {
130 vt_generate_cons_palette(sc->info.fb_cmap,
131 COLOR_FORMAT_RGB, 0xff, 0, 0xff, 8, 0xff, 16);
132 sc->info.fb_cmsize = 16;
135 vt_generate_cons_palette(sc->info.fb_cmap,
136 COLOR_FORMAT_RGB, 0xff, 16, 0xff, 8, 0xff, 0);
137 sc->info.fb_cmsize = 16;
142 fbd = device_add_child(sc->dev, "fbd", device_get_unit(sc->dev));
144 device_printf(sc->dev, "Failed to add fbd child\n");
145 pmap_unmapdev(sc->info.fb_vbase, sc->info.fb_size);
147 } else if (device_probe_and_attach(fbd) != 0) {
148 device_printf(sc->dev, "Failed to attach fbd device\n");
149 device_delete_child(sc->dev, fbd);
150 pmap_unmapdev(sc->info.fb_vbase, sc->info.fb_size);
154 device_printf(sc->dev, "%dx%d(%dx%d@%d,%d) %dbpp\n", fb.xres, fb.yres,
155 fb.vxres, fb.vyres, fb.xoffset, fb.yoffset, fb.bpp);
156 device_printf(sc->dev,
157 "fbswap: %d, pitch %d, base 0x%08x, screen_size %d\n",
158 sc->fbswap, fb.pitch, fb.base, fb.size);
164 bcm_fb_resync_sysctl(SYSCTL_HANDLER_ARGS)
166 struct bcmsc_softc *sc = arg1;
167 struct bcm2835_fb_config fb;
172 err = sysctl_handle_int(oidp, &val, 0, req);
173 if (err || !req->newptr) /* error || read request */
176 bcm_fb_init(sc, &fb);
182 bcm_fb_sysctl_init(struct bcmsc_softc *sc)
184 struct sysctl_ctx_list *ctx;
185 struct sysctl_oid *tree_node;
186 struct sysctl_oid_list *tree;
189 * Add system sysctl tree/handlers.
191 ctx = device_get_sysctl_ctx(sc->dev);
192 tree_node = device_get_sysctl_tree(sc->dev);
193 tree = SYSCTL_CHILDREN(tree_node);
194 SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "resync",
195 CTLFLAG_RW | CTLTYPE_UINT, sc, sizeof(*sc),
196 bcm_fb_resync_sysctl, "IU", "Set to resync framebuffer with VC");
200 bcm_fb_probe(device_t dev)
203 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
206 device_set_desc(dev, "BCM2835 VT framebuffer driver");
208 return (BUS_PROBE_DEFAULT);
212 bcm_fb_attach(device_t dev)
214 char bootargs[2048], *n, *p, *v;
217 struct bcmsc_softc *sc;
219 sc = device_get_softc(dev);
222 /* Newer firmware versions needs an inverted color palette. */
224 chosen = OF_finddevice("/chosen");
226 OF_getprop(chosen, "bootargs", &bootargs, sizeof(bootargs)) > 0) {
228 while ((v = strsep(&p, " ")) != NULL) {
232 if (strcmp(n, "bcm2708_fb.fbswap") == 0 && v != NULL)
238 bcm_fb_sysctl_init(sc);
240 err = bcm_fb_setup_fbd(sc);
247 static struct fb_info *
248 bcm_fb_helper_getinfo(device_t dev)
250 struct bcmsc_softc *sc;
252 sc = device_get_softc(dev);
257 static device_method_t bcm_fb_methods[] = {
258 /* Device interface */
259 DEVMETHOD(device_probe, bcm_fb_probe),
260 DEVMETHOD(device_attach, bcm_fb_attach),
262 /* Framebuffer service methods */
263 DEVMETHOD(fb_getinfo, bcm_fb_helper_getinfo),
268 static devclass_t bcm_fb_devclass;
270 static driver_t bcm_fb_driver = {
273 sizeof(struct bcmsc_softc),
276 DRIVER_MODULE(bcm2835fb, ofwbus, bcm_fb_driver, bcm_fb_devclass, 0, 0);
277 DRIVER_MODULE(bcm2835fb, simplebus, bcm_fb_driver, bcm_fb_devclass, 0, 0);