xref: /freebsd/sys/arm/broadcom/bcm2835/bcm2835_fbd.c (revision 8ef24a0d4b28fe230e20637f56869cc4148cd2ca)
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/fbio.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 
43 #include <vm/vm.h>
44 #include <vm/pmap.h>
45 
46 #include <dev/fdt/fdt_common.h>
47 #include <dev/ofw/ofw_bus.h>
48 #include <dev/ofw/ofw_bus_subr.h>
49 
50 #include <dev/fb/fbreg.h>
51 #include <dev/vt/vt.h>
52 #include <dev/vt/colors/vt_termcolors.h>
53 
54 #include <arm/broadcom/bcm2835/bcm2835_mbox_prop.h>
55 
56 #include "fb_if.h"
57 #include "mbox_if.h"
58 
59 #define	FB_DEPTH		24
60 
61 struct bcmsc_softc {
62 	struct fb_info 			info;
63 	int				fbswap;
64 	struct bcm2835_fb_config	fb;
65 	device_t			dev;
66 };
67 
68 static int bcm_fb_probe(device_t);
69 static int bcm_fb_attach(device_t);
70 
71 static int
72 bcm_fb_init(struct bcmsc_softc *sc, struct bcm2835_fb_config *fb)
73 {
74 	int err;
75 
76 	err = 0;
77 
78 	memset(fb, 0, sizeof(*fb));
79 	if (bcm2835_mbox_fb_get_w_h(fb) != 0)
80 		return (ENXIO);
81 	fb->bpp = FB_DEPTH;
82 
83 	fb->vxres = fb->xres;
84 	fb->vyres = fb->yres;
85 	fb->xoffset = fb->yoffset = 0;
86 
87 	if ((err = bcm2835_mbox_fb_init(fb)) != 0) {
88 		device_printf(sc->dev, "bcm2835_mbox_fb_init failed, err=%d\n", err);
89 		return (ENXIO);
90 	}
91 
92 	return (0);
93 }
94 
95 static int
96 bcm_fb_setup_fbd(struct bcmsc_softc *sc)
97 {
98 	struct bcm2835_fb_config fb;
99 	device_t fbd;
100 	int err;
101 
102 	err = bcm_fb_init(sc, &fb);
103 	if (err)
104 		return (err);
105 
106 	memset(&sc->info, 0, sizeof(sc->info));
107 	sc->info.fb_name = device_get_nameunit(sc->dev);
108 
109 	sc->info.fb_vbase = (intptr_t)pmap_mapdev(fb.base, fb.size);
110 	sc->info.fb_pbase = fb.base;
111 	sc->info.fb_size = fb.size;
112 	sc->info.fb_bpp = sc->info.fb_depth = fb.bpp;
113 	sc->info.fb_stride = fb.pitch;
114 	sc->info.fb_width = fb.xres;
115 	sc->info.fb_height = fb.yres;
116 
117 	if (sc->fbswap) {
118 		switch (sc->info.fb_bpp) {
119 		case 24:
120 			vt_generate_cons_palette(sc->info.fb_cmap,
121 			    COLOR_FORMAT_RGB, 0xff, 0, 0xff, 8, 0xff, 16);
122 			sc->info.fb_cmsize = 16;
123 			break;
124 		case 32:
125 			vt_generate_cons_palette(sc->info.fb_cmap,
126 			    COLOR_FORMAT_RGB, 0xff, 16, 0xff, 8, 0xff, 0);
127 			sc->info.fb_cmsize = 16;
128 			break;
129 		}
130 	}
131 
132 	fbd = device_add_child(sc->dev, "fbd", device_get_unit(sc->dev));
133 	if (fbd == NULL) {
134 		device_printf(sc->dev, "Failed to add fbd child\n");
135 		pmap_unmapdev(sc->info.fb_vbase, sc->info.fb_size);
136 		return (ENXIO);
137 	} else if (device_probe_and_attach(fbd) != 0) {
138 		device_printf(sc->dev, "Failed to attach fbd device\n");
139 		device_delete_child(sc->dev, fbd);
140 		pmap_unmapdev(sc->info.fb_vbase, sc->info.fb_size);
141 		return (ENXIO);
142 	}
143 
144 	device_printf(sc->dev, "%dx%d(%dx%d@%d,%d) %dbpp\n", fb.xres, fb.yres,
145 	    fb.vxres, fb.vyres, fb.xoffset, fb.yoffset, fb.bpp);
146 	device_printf(sc->dev,
147 	    "fbswap: %d, pitch %d, base 0x%08x, screen_size %d\n",
148 	    sc->fbswap, fb.pitch, fb.base, fb.size);
149 
150 	return (0);
151 }
152 
153 static int
154 bcm_fb_resync_sysctl(SYSCTL_HANDLER_ARGS)
155 {
156 	struct bcmsc_softc *sc = arg1;
157 	struct bcm2835_fb_config fb;
158 	int val;
159 	int err;
160 
161 	val = 0;
162 	err = sysctl_handle_int(oidp, &val, 0, req);
163 	if (err || !req->newptr) /* error || read request */
164 		return (err);
165 
166 	bcm_fb_init(sc, &fb);
167 
168 	return (0);
169 }
170 
171 static void
172 bcm_fb_sysctl_init(struct bcmsc_softc *sc)
173 {
174 	struct sysctl_ctx_list *ctx;
175 	struct sysctl_oid *tree_node;
176 	struct sysctl_oid_list *tree;
177 
178 	/*
179 	 * Add system sysctl tree/handlers.
180 	 */
181 	ctx = device_get_sysctl_ctx(sc->dev);
182 	tree_node = device_get_sysctl_tree(sc->dev);
183 	tree = SYSCTL_CHILDREN(tree_node);
184 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "resync",
185 	    CTLFLAG_RW | CTLTYPE_UINT, sc, sizeof(*sc),
186 	    bcm_fb_resync_sysctl, "IU", "Set to resync framebuffer with VC");
187 }
188 
189 static int
190 bcm_fb_probe(device_t dev)
191 {
192 	if (!ofw_bus_is_compatible(dev, "broadcom,bcm2835-fb"))
193 		return (ENXIO);
194 
195 	device_set_desc(dev, "BCM2835 VT framebuffer driver");
196 
197 	return (BUS_PROBE_DEFAULT);
198 }
199 
200 static int
201 bcm_fb_attach(device_t dev)
202 {
203 	char bootargs[2048], *n, *p, *v;
204 	int err;
205 	phandle_t chosen;
206 	struct bcmsc_softc *sc;
207 
208 	sc = device_get_softc(dev);
209 	sc->dev = dev;
210 
211 	/* Newer firmware versions needs an inverted color palette. */
212 	sc->fbswap = 0;
213 	chosen = OF_finddevice("/chosen");
214 	if (chosen != 0 &&
215 	    OF_getprop(chosen, "bootargs", &bootargs, sizeof(bootargs)) > 0) {
216 		p = bootargs;
217 		while ((v = strsep(&p, " ")) != NULL) {
218 			if (*v == '\0')
219 				continue;
220 			n = strsep(&v, "=");
221 			if (strcmp(n, "bcm2708_fb.fbswap") == 0 && v != NULL)
222 				if (*v == '1')
223 					sc->fbswap = 1;
224                 }
225         }
226 
227 	bcm_fb_sysctl_init(sc);
228 
229 	err = bcm_fb_setup_fbd(sc);
230 	if (err)
231 		return (err);
232 
233 	return (0);
234 }
235 
236 static struct fb_info *
237 bcm_fb_helper_getinfo(device_t dev)
238 {
239 	struct bcmsc_softc *sc;
240 
241 	sc = device_get_softc(dev);
242 
243 	return (&sc->info);
244 }
245 
246 static device_method_t bcm_fb_methods[] = {
247 	/* Device interface */
248 	DEVMETHOD(device_probe,		bcm_fb_probe),
249 	DEVMETHOD(device_attach,	bcm_fb_attach),
250 
251 	/* Framebuffer service methods */
252 	DEVMETHOD(fb_getinfo,		bcm_fb_helper_getinfo),
253 
254 	DEVMETHOD_END
255 };
256 
257 static devclass_t bcm_fb_devclass;
258 
259 static driver_t bcm_fb_driver = {
260 	"fb",
261 	bcm_fb_methods,
262 	sizeof(struct bcmsc_softc),
263 };
264 
265 DRIVER_MODULE(bcm2835fb, ofwbus, bcm_fb_driver, bcm_fb_devclass, 0, 0);
266