Path: blob/main/sys/arm/broadcom/bcm2835/bcm2835_fbd.c
39566 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2012 Oleksandr Tymoshenko <[email protected]>4* Copyright (c) 2012, 2013 The FreeBSD Foundation5* All rights reserved.6*7* Portions of this software were developed by Oleksandr Rybalko8* under sponsorship from the FreeBSD Foundation.9*10* Redistribution and use in source and binary forms, with or without11* modification, are permitted provided that the following conditions12* are met:13* 1. Redistributions of source code must retain the above copyright14* notice, this list of conditions and the following disclaimer.15* 2. Redistributions in binary form must reproduce the above copyright16* notice, this list of conditions and the following disclaimer in the17* documentation and/or other materials provided with the distribution.18*19* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND20* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE21* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE22* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE23* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL24* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS25* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)26* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT27* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY28* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF29* SUCH DAMAGE.30*31*/3233#include <sys/param.h>34#include <sys/systm.h>35#include <sys/bio.h>36#include <sys/bus.h>37#include <sys/fbio.h>38#include <sys/kernel.h>39#include <sys/malloc.h>40#include <sys/module.h>4142#include <vm/vm.h>43#include <vm/pmap.h>4445#include <dev/ofw/ofw_bus.h>46#include <dev/ofw/ofw_bus_subr.h>4748#include <dev/fb/fbreg.h>49#include <dev/vt/vt.h>50#include <dev/vt/colors/vt_termcolors.h>5152#include <arm/broadcom/bcm2835/bcm2835_mbox_prop.h>5354#include "fb_if.h"55#include "mbox_if.h"5657#define FB_DEPTH 245859struct bcmsc_softc {60struct fb_info info;61int fbswap;62struct bcm2835_fb_config fb;63device_t dev;64};6566static struct ofw_compat_data compat_data[] = {67{"broadcom,bcm2835-fb", 1},68{"brcm,bcm2708-fb", 1},69{NULL, 0}70};7172static int bcm_fb_probe(device_t);73static int bcm_fb_attach(device_t);7475static int76bcm_fb_init(struct bcmsc_softc *sc, struct bcm2835_fb_config *fb)77{78int err;7980err = 0;8182memset(fb, 0, sizeof(*fb));83if (bcm2835_mbox_fb_get_w_h(fb) != 0)84return (ENXIO);85if (bcm2835_mbox_fb_get_bpp(fb) != 0)86return (ENXIO);87if (fb->bpp < FB_DEPTH) {88device_printf(sc->dev, "changing fb bpp from %d to %d\n",89fb->bpp, FB_DEPTH);90fb->bpp = FB_DEPTH;91} else92device_printf(sc->dev, "keeping existing fb bpp of %d\n",93fb->bpp);9495fb->vxres = fb->xres;96fb->vyres = fb->yres;97fb->xoffset = fb->yoffset = 0;9899if ((err = bcm2835_mbox_fb_init(fb)) != 0) {100device_printf(sc->dev, "bcm2835_mbox_fb_init failed, err=%d\n",101err);102return (ENXIO);103}104105return (0);106}107108static int109bcm_fb_setup_fbd(struct bcmsc_softc *sc)110{111struct bcm2835_fb_config fb;112device_t fbd;113int err;114115err = bcm_fb_init(sc, &fb);116if (err)117return (err);118119memset(&sc->info, 0, sizeof(sc->info));120sc->info.fb_name = device_get_nameunit(sc->dev);121122sc->info.fb_vbase = (intptr_t)pmap_mapdev(fb.base, fb.size);123sc->info.fb_pbase = fb.base;124sc->info.fb_size = fb.size;125sc->info.fb_bpp = sc->info.fb_depth = fb.bpp;126sc->info.fb_stride = fb.pitch;127sc->info.fb_width = fb.xres;128sc->info.fb_height = fb.yres;129#ifdef VM_MEMATTR_WRITE_COMBINING130sc->info.fb_flags = FB_FLAG_MEMATTR;131sc->info.fb_memattr = VM_MEMATTR_WRITE_COMBINING;132#endif133134if (sc->fbswap) {135switch (sc->info.fb_bpp) {136case 24:137vt_config_cons_colors(&sc->info,138COLOR_FORMAT_RGB, 0xff, 0, 0xff, 8, 0xff, 16);139sc->info.fb_cmsize = 16;140break;141case 32:142vt_config_cons_colors(&sc->info,143COLOR_FORMAT_RGB, 0xff, 16, 0xff, 8, 0xff, 0);144sc->info.fb_cmsize = 16;145break;146}147}148149fbd = device_add_child(sc->dev, "fbd", device_get_unit(sc->dev));150if (fbd == NULL) {151device_printf(sc->dev, "Failed to add fbd child\n");152pmap_unmapdev((void *)sc->info.fb_vbase, sc->info.fb_size);153return (ENXIO);154} else if (device_probe_and_attach(fbd) != 0) {155device_printf(sc->dev, "Failed to attach fbd device\n");156device_delete_child(sc->dev, fbd);157pmap_unmapdev((void *)sc->info.fb_vbase, sc->info.fb_size);158return (ENXIO);159}160161device_printf(sc->dev, "%dx%d(%dx%d@%d,%d) %dbpp\n", fb.xres, fb.yres,162fb.vxres, fb.vyres, fb.xoffset, fb.yoffset, fb.bpp);163device_printf(sc->dev,164"fbswap: %d, pitch %d, base 0x%08x, screen_size %d\n",165sc->fbswap, fb.pitch, fb.base, fb.size);166167return (0);168}169170static int171bcm_fb_resync_sysctl(SYSCTL_HANDLER_ARGS)172{173struct bcmsc_softc *sc = arg1;174struct bcm2835_fb_config fb;175int val;176int err;177178val = 0;179err = sysctl_handle_int(oidp, &val, 0, req);180if (err || !req->newptr) /* error || read request */181return (err);182183bcm_fb_init(sc, &fb);184185return (0);186}187188static void189bcm_fb_sysctl_init(struct bcmsc_softc *sc)190{191struct sysctl_ctx_list *ctx;192struct sysctl_oid *tree_node;193struct sysctl_oid_list *tree;194195/*196* Add system sysctl tree/handlers.197*/198ctx = device_get_sysctl_ctx(sc->dev);199tree_node = device_get_sysctl_tree(sc->dev);200tree = SYSCTL_CHILDREN(tree_node);201SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "resync",202CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_NEEDGIANT, sc, sizeof(*sc),203bcm_fb_resync_sysctl, "IU", "Set to resync framebuffer with VC");204}205206static int207bcm_fb_probe(device_t dev)208{209210if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)211return (ENXIO);212213device_set_desc(dev, "BCM2835 VT framebuffer driver");214215return (BUS_PROBE_DEFAULT);216}217218static int219bcm_fb_attach(device_t dev)220{221char bootargs[2048], *n, *p, *v;222int err;223phandle_t chosen;224struct bcmsc_softc *sc;225226sc = device_get_softc(dev);227sc->dev = dev;228229/* Newer firmware versions needs an inverted color palette. */230sc->fbswap = 0;231chosen = OF_finddevice("/chosen");232if (chosen != -1 &&233OF_getprop(chosen, "bootargs", &bootargs, sizeof(bootargs)) > 0) {234p = bootargs;235while ((v = strsep(&p, " ")) != NULL) {236if (*v == '\0')237continue;238n = strsep(&v, "=");239if (strcmp(n, "bcm2708_fb.fbswap") == 0 && v != NULL)240if (*v == '1')241sc->fbswap = 1;242}243}244245bcm_fb_sysctl_init(sc);246247err = bcm_fb_setup_fbd(sc);248if (err)249return (err);250251return (0);252}253254static struct fb_info *255bcm_fb_helper_getinfo(device_t dev)256{257struct bcmsc_softc *sc;258259sc = device_get_softc(dev);260261return (&sc->info);262}263264static device_method_t bcm_fb_methods[] = {265/* Device interface */266DEVMETHOD(device_probe, bcm_fb_probe),267DEVMETHOD(device_attach, bcm_fb_attach),268269/* Framebuffer service methods */270DEVMETHOD(fb_getinfo, bcm_fb_helper_getinfo),271272DEVMETHOD_END273};274275static driver_t bcm_fb_driver = {276"fb",277bcm_fb_methods,278sizeof(struct bcmsc_softc),279};280281DRIVER_MODULE(bcm2835fb, ofwbus, bcm_fb_driver, 0, 0);282DRIVER_MODULE(bcm2835fb, simplebus, bcm_fb_driver, 0, 0);283284285