]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/broadcom/bcm2835/bcm2835_fbd.c
MFV r361280:
[FreeBSD/FreeBSD.git] / sys / arm / broadcom / bcm2835 / bcm2835_fbd.c
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         if (bcm2835_mbox_fb_get_bpp(fb) != 0)
89                 return (ENXIO);
90         if (fb->bpp < FB_DEPTH) {
91                 device_printf(sc->dev, "changing fb bpp from %d to %d\n", fb->bpp, FB_DEPTH);
92                 fb->bpp = FB_DEPTH;
93         } else
94                 device_printf(sc->dev, "keeping existing fb bpp of %d\n", fb->bpp);
95
96         fb->vxres = fb->xres;
97         fb->vyres = fb->yres;
98         fb->xoffset = fb->yoffset = 0;
99
100         if ((err = bcm2835_mbox_fb_init(fb)) != 0) {
101                 device_printf(sc->dev, "bcm2835_mbox_fb_init failed, err=%d\n", err);
102                 return (ENXIO);
103         }
104
105         return (0);
106 }
107
108 static int
109 bcm_fb_setup_fbd(struct bcmsc_softc *sc)
110 {
111         struct bcm2835_fb_config fb;
112         device_t fbd;
113         int err;
114
115         err = bcm_fb_init(sc, &fb);
116         if (err)
117                 return (err);
118
119         memset(&sc->info, 0, sizeof(sc->info));
120         sc->info.fb_name = device_get_nameunit(sc->dev);
121
122         sc->info.fb_vbase = (intptr_t)pmap_mapdev(fb.base, fb.size);
123         sc->info.fb_pbase = fb.base;
124         sc->info.fb_size = fb.size;
125         sc->info.fb_bpp = sc->info.fb_depth = fb.bpp;
126         sc->info.fb_stride = fb.pitch;
127         sc->info.fb_width = fb.xres;
128         sc->info.fb_height = fb.yres;
129 #ifdef VM_MEMATTR_WRITE_COMBINING
130         sc->info.fb_flags = FB_FLAG_MEMATTR;
131         sc->info.fb_memattr = VM_MEMATTR_WRITE_COMBINING;
132 #endif
133
134         if (sc->fbswap) {
135                 switch (sc->info.fb_bpp) {
136                 case 24:
137                         vt_generate_cons_palette(sc->info.fb_cmap,
138                             COLOR_FORMAT_RGB, 0xff, 0, 0xff, 8, 0xff, 16);
139                         sc->info.fb_cmsize = 16;
140                         break;
141                 case 32:
142                         vt_generate_cons_palette(sc->info.fb_cmap,
143                             COLOR_FORMAT_RGB, 0xff, 16, 0xff, 8, 0xff, 0);
144                         sc->info.fb_cmsize = 16;
145                         break;
146                 }
147         }
148
149         fbd = device_add_child(sc->dev, "fbd", device_get_unit(sc->dev));
150         if (fbd == NULL) {
151                 device_printf(sc->dev, "Failed to add fbd child\n");
152                 pmap_unmapdev(sc->info.fb_vbase, sc->info.fb_size);
153                 return (ENXIO);
154         } else if (device_probe_and_attach(fbd) != 0) {
155                 device_printf(sc->dev, "Failed to attach fbd device\n");
156                 device_delete_child(sc->dev, fbd);
157                 pmap_unmapdev(sc->info.fb_vbase, sc->info.fb_size);
158                 return (ENXIO);
159         }
160
161         device_printf(sc->dev, "%dx%d(%dx%d@%d,%d) %dbpp\n", fb.xres, fb.yres,
162             fb.vxres, fb.vyres, fb.xoffset, fb.yoffset, fb.bpp);
163         device_printf(sc->dev,
164             "fbswap: %d, pitch %d, base 0x%08x, screen_size %d\n",
165             sc->fbswap, fb.pitch, fb.base, fb.size);
166
167         return (0);
168 }
169
170 static int
171 bcm_fb_resync_sysctl(SYSCTL_HANDLER_ARGS)
172 {
173         struct bcmsc_softc *sc = arg1;
174         struct bcm2835_fb_config fb;
175         int val;
176         int err;
177
178         val = 0;
179         err = sysctl_handle_int(oidp, &val, 0, req);
180         if (err || !req->newptr) /* error || read request */
181                 return (err);
182
183         bcm_fb_init(sc, &fb);
184
185         return (0);
186 }
187
188 static void
189 bcm_fb_sysctl_init(struct bcmsc_softc *sc)
190 {
191         struct sysctl_ctx_list *ctx;
192         struct sysctl_oid *tree_node;
193         struct sysctl_oid_list *tree;
194
195         /*
196          * Add system sysctl tree/handlers.
197          */
198         ctx = device_get_sysctl_ctx(sc->dev);
199         tree_node = device_get_sysctl_tree(sc->dev);
200         tree = SYSCTL_CHILDREN(tree_node);
201         SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "resync",
202             CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_NEEDGIANT, sc, sizeof(*sc),
203             bcm_fb_resync_sysctl, "IU", "Set to resync framebuffer with VC");
204 }
205
206 static int
207 bcm_fb_probe(device_t dev)
208 {
209
210         if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
211                 return (ENXIO);
212
213         device_set_desc(dev, "BCM2835 VT framebuffer driver");
214
215         return (BUS_PROBE_DEFAULT);
216 }
217
218 static int
219 bcm_fb_attach(device_t dev)
220 {
221         char bootargs[2048], *n, *p, *v;
222         int err;
223         phandle_t chosen;
224         struct bcmsc_softc *sc;
225
226         sc = device_get_softc(dev);
227         sc->dev = dev;
228
229         /* Newer firmware versions needs an inverted color palette. */
230         sc->fbswap = 0;
231         chosen = OF_finddevice("/chosen");
232         if (chosen != -1 &&
233             OF_getprop(chosen, "bootargs", &bootargs, sizeof(bootargs)) > 0) {
234                 p = bootargs;
235                 while ((v = strsep(&p, " ")) != NULL) {
236                         if (*v == '\0')
237                                 continue;
238                         n = strsep(&v, "=");
239                         if (strcmp(n, "bcm2708_fb.fbswap") == 0 && v != NULL)
240                                 if (*v == '1')
241                                         sc->fbswap = 1;
242                 }
243         }
244
245         bcm_fb_sysctl_init(sc);
246
247         err = bcm_fb_setup_fbd(sc);
248         if (err)
249                 return (err);
250
251         return (0);
252 }
253
254 static struct fb_info *
255 bcm_fb_helper_getinfo(device_t dev)
256 {
257         struct bcmsc_softc *sc;
258
259         sc = device_get_softc(dev);
260
261         return (&sc->info);
262 }
263
264 static device_method_t bcm_fb_methods[] = {
265         /* Device interface */
266         DEVMETHOD(device_probe,         bcm_fb_probe),
267         DEVMETHOD(device_attach,        bcm_fb_attach),
268
269         /* Framebuffer service methods */
270         DEVMETHOD(fb_getinfo,           bcm_fb_helper_getinfo),
271
272         DEVMETHOD_END
273 };
274
275 static devclass_t bcm_fb_devclass;
276
277 static driver_t bcm_fb_driver = {
278         "fb",
279         bcm_fb_methods,
280         sizeof(struct bcmsc_softc),
281 };
282
283 DRIVER_MODULE(bcm2835fb, ofwbus, bcm_fb_driver, bcm_fb_devclass, 0, 0);
284 DRIVER_MODULE(bcm2835fb, simplebus, bcm_fb_driver, bcm_fb_devclass, 0, 0);