2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org>
5 * Copyright (c) 2012, 2013 The FreeBSD Foundation
8 * Portions of this software were developed by Oleksandr Rybalko
9 * under sponsorship from the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/module.h>
48 #include <dev/ofw/ofw_bus.h>
49 #include <dev/ofw/ofw_bus_subr.h>
51 #include <dev/fb/fbreg.h>
52 #include <dev/vt/vt.h>
53 #include <dev/vt/colors/vt_termcolors.h>
55 #include <arm/broadcom/bcm2835/bcm2835_mbox_prop.h>
65 struct bcm2835_fb_config fb;
69 static struct ofw_compat_data compat_data[] = {
70 {"broadcom,bcm2835-fb", 1},
71 {"brcm,bcm2708-fb", 1},
75 static int bcm_fb_probe(device_t);
76 static int bcm_fb_attach(device_t);
79 bcm_fb_init(struct bcmsc_softc *sc, struct bcm2835_fb_config *fb)
85 memset(fb, 0, sizeof(*fb));
86 if (bcm2835_mbox_fb_get_w_h(fb) != 0)
88 if (bcm2835_mbox_fb_get_bpp(fb) != 0)
90 if (fb->bpp < FB_DEPTH) {
91 device_printf(sc->dev, "changing fb bpp from %d to %d\n",
95 device_printf(sc->dev, "keeping existing fb bpp of %d\n",
100 fb->xoffset = fb->yoffset = 0;
102 if ((err = bcm2835_mbox_fb_init(fb)) != 0) {
103 device_printf(sc->dev, "bcm2835_mbox_fb_init failed, err=%d\n",
112 bcm_fb_setup_fbd(struct bcmsc_softc *sc)
114 struct bcm2835_fb_config fb;
118 err = bcm_fb_init(sc, &fb);
122 memset(&sc->info, 0, sizeof(sc->info));
123 sc->info.fb_name = device_get_nameunit(sc->dev);
125 sc->info.fb_vbase = (intptr_t)pmap_mapdev(fb.base, fb.size);
126 sc->info.fb_pbase = fb.base;
127 sc->info.fb_size = fb.size;
128 sc->info.fb_bpp = sc->info.fb_depth = fb.bpp;
129 sc->info.fb_stride = fb.pitch;
130 sc->info.fb_width = fb.xres;
131 sc->info.fb_height = fb.yres;
132 #ifdef VM_MEMATTR_WRITE_COMBINING
133 sc->info.fb_flags = FB_FLAG_MEMATTR;
134 sc->info.fb_memattr = VM_MEMATTR_WRITE_COMBINING;
138 switch (sc->info.fb_bpp) {
140 vt_generate_cons_palette(sc->info.fb_cmap,
141 COLOR_FORMAT_RGB, 0xff, 0, 0xff, 8, 0xff, 16);
142 sc->info.fb_cmsize = 16;
145 vt_generate_cons_palette(sc->info.fb_cmap,
146 COLOR_FORMAT_RGB, 0xff, 16, 0xff, 8, 0xff, 0);
147 sc->info.fb_cmsize = 16;
152 fbd = device_add_child(sc->dev, "fbd", device_get_unit(sc->dev));
154 device_printf(sc->dev, "Failed to add fbd child\n");
155 pmap_unmapdev(sc->info.fb_vbase, sc->info.fb_size);
157 } else if (device_probe_and_attach(fbd) != 0) {
158 device_printf(sc->dev, "Failed to attach fbd device\n");
159 device_delete_child(sc->dev, fbd);
160 pmap_unmapdev(sc->info.fb_vbase, sc->info.fb_size);
164 device_printf(sc->dev, "%dx%d(%dx%d@%d,%d) %dbpp\n", fb.xres, fb.yres,
165 fb.vxres, fb.vyres, fb.xoffset, fb.yoffset, fb.bpp);
166 device_printf(sc->dev,
167 "fbswap: %d, pitch %d, base 0x%08x, screen_size %d\n",
168 sc->fbswap, fb.pitch, fb.base, fb.size);
174 bcm_fb_resync_sysctl(SYSCTL_HANDLER_ARGS)
176 struct bcmsc_softc *sc = arg1;
177 struct bcm2835_fb_config fb;
182 err = sysctl_handle_int(oidp, &val, 0, req);
183 if (err || !req->newptr) /* error || read request */
186 bcm_fb_init(sc, &fb);
192 bcm_fb_sysctl_init(struct bcmsc_softc *sc)
194 struct sysctl_ctx_list *ctx;
195 struct sysctl_oid *tree_node;
196 struct sysctl_oid_list *tree;
199 * Add system sysctl tree/handlers.
201 ctx = device_get_sysctl_ctx(sc->dev);
202 tree_node = device_get_sysctl_tree(sc->dev);
203 tree = SYSCTL_CHILDREN(tree_node);
204 SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "resync",
205 CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_NEEDGIANT, sc, sizeof(*sc),
206 bcm_fb_resync_sysctl, "IU", "Set to resync framebuffer with VC");
210 bcm_fb_probe(device_t dev)
213 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
216 device_set_desc(dev, "BCM2835 VT framebuffer driver");
218 return (BUS_PROBE_DEFAULT);
222 bcm_fb_attach(device_t dev)
224 char bootargs[2048], *n, *p, *v;
227 struct bcmsc_softc *sc;
229 sc = device_get_softc(dev);
232 /* Newer firmware versions needs an inverted color palette. */
234 chosen = OF_finddevice("/chosen");
236 OF_getprop(chosen, "bootargs", &bootargs, sizeof(bootargs)) > 0) {
238 while ((v = strsep(&p, " ")) != NULL) {
242 if (strcmp(n, "bcm2708_fb.fbswap") == 0 && v != NULL)
248 bcm_fb_sysctl_init(sc);
250 err = bcm_fb_setup_fbd(sc);
257 static struct fb_info *
258 bcm_fb_helper_getinfo(device_t dev)
260 struct bcmsc_softc *sc;
262 sc = device_get_softc(dev);
267 static device_method_t bcm_fb_methods[] = {
268 /* Device interface */
269 DEVMETHOD(device_probe, bcm_fb_probe),
270 DEVMETHOD(device_attach, bcm_fb_attach),
272 /* Framebuffer service methods */
273 DEVMETHOD(fb_getinfo, bcm_fb_helper_getinfo),
278 static devclass_t bcm_fb_devclass;
280 static driver_t bcm_fb_driver = {
283 sizeof(struct bcmsc_softc),
286 DRIVER_MODULE(bcm2835fb, ofwbus, bcm_fb_driver, bcm_fb_devclass, 0, 0);
287 DRIVER_MODULE(bcm2835fb, simplebus, bcm_fb_driver, bcm_fb_devclass, 0, 0);