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/endian.h>
40 #include <sys/kernel.h>
41 #include <sys/kthread.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/mutex.h>
46 #include <sys/queue.h>
47 #include <sys/resource.h>
50 #include <sys/timetc.h>
52 #include <sys/consio.h>
56 #include <machine/bus.h>
57 #include <machine/cpu.h>
58 #include <machine/cpufunc.h>
59 #include <machine/fdt.h>
60 #include <machine/resource.h>
61 #include <machine/intr.h>
63 #include <dev/fdt/fdt_common.h>
64 #include <dev/ofw/ofw_bus.h>
65 #include <dev/ofw/ofw_bus_subr.h>
67 #include <dev/fb/fbreg.h>
68 #include <dev/vt/vt.h>
70 #include <arm/broadcom/bcm2835/bcm2835_mbox.h>
71 #include <arm/broadcom/bcm2835/bcm2835_vcbus.h>
80 struct bcm_fb_config {
89 /* Filled by videocore */
97 bus_dma_tag_t dma_tag;
99 struct bcm_fb_config* fb_config;
100 bus_addr_t fb_config_phys;
101 struct intr_config_hook init_hook;
104 static int bcm_fb_probe(device_t);
105 static int bcm_fb_attach(device_t);
106 static void bcm_fb_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg,
110 bcm_fb_init(void *arg)
112 volatile struct bcm_fb_config *fb_config;
113 struct bcmsc_softc *sc;
114 struct fb_info *info;
122 fb_config = sc->fb_config;
123 node = ofw_bus_get_node(sc->dev);
128 fb_config->vxres = 0;
129 fb_config->vyres = 0;
130 fb_config->xoffset = 0;
131 fb_config->yoffset = 0;
133 fb_config->pitch = 0;
134 fb_config->screen_size = 0;
136 if ((OF_getprop(node, "broadcom,width", &cell, sizeof(cell))) > 0)
137 fb_config->xres = (int)fdt32_to_cpu(cell);
138 if (fb_config->xres == 0)
139 fb_config->xres = FB_WIDTH;
141 if ((OF_getprop(node, "broadcom,height", &cell, sizeof(cell))) > 0)
142 fb_config->yres = (uint32_t)fdt32_to_cpu(cell);
143 if (fb_config->yres == 0)
144 fb_config->yres = FB_HEIGHT;
146 if ((OF_getprop(node, "broadcom,depth", &cell, sizeof(cell))) > 0)
147 fb_config->bpp = (uint32_t)fdt32_to_cpu(cell);
148 if (fb_config->bpp == 0)
149 fb_config->bpp = FB_DEPTH;
151 bus_dmamap_sync(sc->dma_tag, sc->dma_map,
152 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
154 mbox = devclass_get_device(devclass_find("mbox"), 0);
156 MBOX_WRITE(mbox, BCM2835_MBOX_CHAN_FB, sc->fb_config_phys);
157 MBOX_READ(mbox, BCM2835_MBOX_CHAN_FB, &err);
159 bus_dmamap_sync(sc->dma_tag, sc->dma_map,
160 BUS_DMASYNC_POSTREAD);
162 if (fb_config->base != 0) {
163 device_printf(sc->dev, "%dx%d(%dx%d@%d,%d) %dbpp\n",
164 fb_config->xres, fb_config->yres,
165 fb_config->vxres, fb_config->vyres,
166 fb_config->xoffset, fb_config->yoffset,
169 device_printf(sc->dev, "pitch %d, base 0x%08x, screen_size %d\n",
170 fb_config->pitch, fb_config->base,
171 fb_config->screen_size);
173 info = malloc(sizeof(struct fb_info), M_DEVBUF,
175 info->fb_name = device_get_nameunit(sc->dev);
176 info->fb_vbase = (intptr_t)pmap_mapdev(fb_config->base,
177 fb_config->screen_size);
178 info->fb_pbase = fb_config->base;
179 info->fb_size = fb_config->screen_size;
180 info->fb_bpp = info->fb_depth = fb_config->bpp;
181 info->fb_stride = fb_config->pitch;
182 info->fb_width = fb_config->xres;
183 info->fb_height = fb_config->yres;
187 fbd = device_add_child(sc->dev, "fbd",
188 device_get_unit(sc->dev));
190 device_printf(sc->dev, "Failed to add fbd child\n");
193 if (device_probe_and_attach(fbd) != 0) {
194 device_printf(sc->dev, "Failed to attach fbd device\n");
198 device_printf(sc->dev, "Failed to set framebuffer info\n");
202 config_intrhook_disestablish(&sc->init_hook);
206 bcm_fb_probe(device_t dev)
208 if (!ofw_bus_is_compatible(dev, "broadcom,bcm2835-fb"))
211 device_set_desc(dev, "BCM2835 VT framebuffer driver");
213 return (BUS_PROBE_DEFAULT);
217 bcm_fb_attach(device_t dev)
219 struct bcmsc_softc *sc = device_get_softc(dev);
220 int dma_size = sizeof(struct bcm_fb_config);
225 err = bus_dma_tag_create(
226 bus_get_dma_tag(sc->dev),
227 PAGE_SIZE, 0, /* alignment, boundary */
228 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
229 BUS_SPACE_MAXADDR, /* highaddr */
230 NULL, NULL, /* filter, filterarg */
231 dma_size, 1, /* maxsize, nsegments */
232 dma_size, 0, /* maxsegsize, flags */
233 NULL, NULL, /* lockfunc, lockarg */
236 err = bus_dmamem_alloc(sc->dma_tag, (void **)&sc->fb_config, 0,
239 device_printf(dev, "cannot allocate framebuffer\n");
243 err = bus_dmamap_load(sc->dma_tag, sc->dma_map, sc->fb_config,
244 dma_size, bcm_fb_dmamap_cb, &sc->fb_config_phys, BUS_DMA_NOWAIT);
247 device_printf(dev, "cannot load DMA map\n");
252 * We have to wait until interrupts are enabled.
253 * Mailbox relies on it to get data from VideoCore
255 sc->init_hook.ich_func = bcm_fb_init;
256 sc->init_hook.ich_arg = sc;
258 if (config_intrhook_establish(&sc->init_hook) != 0) {
259 device_printf(dev, "failed to establish intrhook\n");
270 bcm_fb_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int err)
277 addr = (bus_addr_t*)arg;
278 *addr = PHYS_TO_VCBUS(segs[0].ds_addr);
281 static struct fb_info *
282 bcm_fb_helper_getinfo(device_t dev)
284 struct bcmsc_softc *sc;
286 sc = device_get_softc(dev);
291 static device_method_t bcm_fb_methods[] = {
292 /* Device interface */
293 DEVMETHOD(device_probe, bcm_fb_probe),
294 DEVMETHOD(device_attach, bcm_fb_attach),
296 /* Framebuffer service methods */
297 DEVMETHOD(fb_getinfo, bcm_fb_helper_getinfo),
302 static devclass_t bcm_fb_devclass;
304 static driver_t bcm_fb_driver = {
307 sizeof(struct bcmsc_softc),
310 DRIVER_MODULE(bcm2835fb, ofwbus, bcm_fb_driver, bcm_fb_devclass, 0, 0);