xref: /freebsd/sys/arm/broadcom/bcm2835/bcm2835_fbd.c (revision 788ca347b816afd83b2885e0c79aeeb88649b2ab)
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/conf.h>
39 #include <sys/endian.h>
40 #include <sys/kernel.h>
41 #include <sys/kthread.h>
42 #include <sys/lock.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>
48 #include <sys/rman.h>
49 #include <sys/time.h>
50 #include <sys/timetc.h>
51 #include <sys/fbio.h>
52 #include <sys/consio.h>
53 
54 #include <sys/kdb.h>
55 
56 #include <vm/vm.h>
57 #include <vm/pmap.h>
58 
59 #include <machine/bus.h>
60 #include <machine/cpu.h>
61 #include <machine/cpufunc.h>
62 #include <machine/fdt.h>
63 #include <machine/resource.h>
64 #include <machine/intr.h>
65 
66 #include <dev/fdt/fdt_common.h>
67 #include <dev/ofw/ofw_bus.h>
68 #include <dev/ofw/ofw_bus_subr.h>
69 
70 #include <dev/fb/fbreg.h>
71 #include <dev/vt/vt.h>
72 
73 #include <arm/broadcom/bcm2835/bcm2835_mbox.h>
74 #include <arm/broadcom/bcm2835/bcm2835_vcbus.h>
75 
76 #include "fb_if.h"
77 #include "mbox_if.h"
78 
79 #define FB_WIDTH		640
80 #define FB_HEIGHT		480
81 #define FB_DEPTH		24
82 
83 struct bcm_fb_config {
84 	uint32_t	xres;
85 	uint32_t	yres;
86 	uint32_t	vxres;
87 	uint32_t	vyres;
88 	uint32_t	pitch;
89 	uint32_t	bpp;
90 	uint32_t	xoffset;
91 	uint32_t	yoffset;
92 	/* Filled by videocore */
93 	uint32_t	base;
94 	uint32_t	screen_size;
95 };
96 
97 struct bcmsc_softc {
98 	device_t		dev;
99 	struct fb_info 		*info;
100 	bus_dma_tag_t		dma_tag;
101 	bus_dmamap_t		dma_map;
102 	struct bcm_fb_config*	fb_config;
103 	bus_addr_t		fb_config_phys;
104 	struct intr_config_hook	init_hook;
105 };
106 
107 static int bcm_fb_probe(device_t);
108 static int bcm_fb_attach(device_t);
109 static void bcm_fb_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg,
110     int err);
111 
112 static void
113 bcm_fb_init(void *arg)
114 {
115 	volatile struct bcm_fb_config *fb_config;
116 	struct bcmsc_softc *sc;
117 	struct fb_info *info;
118 	phandle_t node;
119 	pcell_t cell;
120 	device_t mbox;
121 	device_t fbd;
122 	int err = 0;
123 
124 	sc = arg;
125 	fb_config = sc->fb_config;
126 	node = ofw_bus_get_node(sc->dev);
127 
128 	fb_config->xres = 0;
129 	fb_config->yres = 0;
130 	fb_config->bpp = 0;
131 	fb_config->vxres = 0;
132 	fb_config->vyres = 0;
133 	fb_config->xoffset = 0;
134 	fb_config->yoffset = 0;
135 	fb_config->base = 0;
136 	fb_config->pitch = 0;
137 	fb_config->screen_size = 0;
138 
139 	if ((OF_getprop(node, "broadcom,width", &cell, sizeof(cell))) > 0)
140 		fb_config->xres = (int)fdt32_to_cpu(cell);
141 	if (fb_config->xres == 0)
142 		fb_config->xres = FB_WIDTH;
143 
144 	if ((OF_getprop(node, "broadcom,height", &cell, sizeof(cell))) > 0)
145 		fb_config->yres = (uint32_t)fdt32_to_cpu(cell);
146 	if (fb_config->yres == 0)
147 		fb_config->yres = FB_HEIGHT;
148 
149 	if ((OF_getprop(node, "broadcom,depth", &cell, sizeof(cell))) > 0)
150 		fb_config->bpp = (uint32_t)fdt32_to_cpu(cell);
151 	if (fb_config->bpp == 0)
152 		fb_config->bpp = FB_DEPTH;
153 
154 	bus_dmamap_sync(sc->dma_tag, sc->dma_map,
155 		BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
156 
157 	mbox = devclass_get_device(devclass_find("mbox"), 0);
158 	if (mbox) {
159 		MBOX_WRITE(mbox, BCM2835_MBOX_CHAN_FB, sc->fb_config_phys);
160 		MBOX_READ(mbox, BCM2835_MBOX_CHAN_FB, &err);
161 	}
162 	bus_dmamap_sync(sc->dma_tag, sc->dma_map,
163 		BUS_DMASYNC_POSTREAD);
164 
165 	if (fb_config->base != 0) {
166 		device_printf(sc->dev, "%dx%d(%dx%d@%d,%d) %dbpp\n",
167 			fb_config->xres, fb_config->yres,
168 			fb_config->vxres, fb_config->vyres,
169 			fb_config->xoffset, fb_config->yoffset,
170 			fb_config->bpp);
171 
172 		device_printf(sc->dev, "pitch %d, base 0x%08x, screen_size %d\n",
173 			fb_config->pitch, fb_config->base,
174 			fb_config->screen_size);
175 
176 		info = malloc(sizeof(struct fb_info), M_DEVBUF,
177 		    M_WAITOK | M_ZERO);
178 		info->fb_name = device_get_nameunit(sc->dev);
179 		info->fb_vbase = (intptr_t)pmap_mapdev(fb_config->base,
180 		    fb_config->screen_size);
181 		info->fb_pbase = fb_config->base;
182 		info->fb_size = fb_config->screen_size;
183 		info->fb_bpp = info->fb_depth = fb_config->bpp;
184 		info->fb_stride = fb_config->pitch;
185 		info->fb_width = fb_config->xres;
186 		info->fb_height = fb_config->yres;
187 
188 		sc->info = info;
189 
190 		fbd = device_add_child(sc->dev, "fbd",
191 		    device_get_unit(sc->dev));
192 		if (fbd == NULL)
193 			device_printf(sc->dev, "Failed to add fbd child\n");
194 		else if (device_probe_and_attach(fbd) != 0)
195 			device_printf(sc->dev, "Failed to attach fbd device\n");
196 	} else {
197 		device_printf(sc->dev, "Failed to set framebuffer info\n");
198 	}
199 
200 	config_intrhook_disestablish(&sc->init_hook);
201 }
202 
203 static int
204 bcm_fb_probe(device_t dev)
205 {
206 	if (!ofw_bus_is_compatible(dev, "broadcom,bcm2835-fb"))
207 		return (ENXIO);
208 
209 	device_set_desc(dev, "BCM2835 VT framebuffer driver");
210 
211 	return (BUS_PROBE_DEFAULT);
212 }
213 
214 static int
215 bcm_fb_attach(device_t dev)
216 {
217 	struct bcmsc_softc *sc = device_get_softc(dev);
218 	int dma_size = sizeof(struct bcm_fb_config);
219 	int err;
220 
221 	sc->dev = dev;
222 
223 	err = bus_dma_tag_create(
224 	    bus_get_dma_tag(sc->dev),
225 	    PAGE_SIZE, 0,		/* alignment, boundary */
226 	    BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
227 	    BUS_SPACE_MAXADDR,		/* highaddr */
228 	    NULL, NULL,			/* filter, filterarg */
229 	    dma_size, 1,		/* maxsize, nsegments */
230 	    dma_size, 0,		/* maxsegsize, flags */
231 	    NULL, NULL,			/* lockfunc, lockarg */
232 	    &sc->dma_tag);
233 
234 	err = bus_dmamem_alloc(sc->dma_tag, (void **)&sc->fb_config, 0,
235 	    &sc->dma_map);
236 	if (err) {
237 		device_printf(dev, "cannot allocate framebuffer\n");
238 		goto fail;
239 	}
240 
241 	err = bus_dmamap_load(sc->dma_tag, sc->dma_map, sc->fb_config,
242 	    dma_size, bcm_fb_dmamap_cb, &sc->fb_config_phys, BUS_DMA_NOWAIT);
243 
244 	if (err) {
245 		device_printf(dev, "cannot load DMA map\n");
246 		goto fail;
247 	}
248 
249 	/*
250 	 * We have to wait until interrupts are enabled.
251 	 * Mailbox relies on it to get data from VideoCore
252 	 */
253         sc->init_hook.ich_func = bcm_fb_init;
254         sc->init_hook.ich_arg = sc;
255 
256         if (config_intrhook_establish(&sc->init_hook) != 0) {
257 		device_printf(dev, "failed to establish intrhook\n");
258                 return (ENOMEM);
259 	}
260 
261 	return (0);
262 
263 fail:
264 	return (ENXIO);
265 }
266 
267 static void
268 bcm_fb_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int err)
269 {
270 	bus_addr_t *addr;
271 
272 	if (err)
273 		return;
274 
275 	addr = (bus_addr_t*)arg;
276 	*addr = PHYS_TO_VCBUS(segs[0].ds_addr);
277 }
278 
279 static struct fb_info *
280 bcm_fb_helper_getinfo(device_t dev)
281 {
282 	struct bcmsc_softc *sc;
283 
284 	sc = device_get_softc(dev);
285 
286 	return (sc->info);
287 }
288 
289 static device_method_t bcm_fb_methods[] = {
290 	/* Device interface */
291 	DEVMETHOD(device_probe,		bcm_fb_probe),
292 	DEVMETHOD(device_attach,	bcm_fb_attach),
293 
294 	/* Framebuffer service methods */
295 	DEVMETHOD(fb_getinfo,		bcm_fb_helper_getinfo),
296 
297 	DEVMETHOD_END
298 };
299 
300 static devclass_t bcm_fb_devclass;
301 
302 static driver_t bcm_fb_driver = {
303 	"fb",
304 	bcm_fb_methods,
305 	sizeof(struct bcmsc_softc),
306 };
307 
308 DRIVER_MODULE(bcm2835fb, ofwbus, bcm_fb_driver, bcm_fb_devclass, 0, 0);
309