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