xref: /freebsd/sys/dev/vt/hw/simplefb/simplefb.c (revision bdd1243d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2014 The FreeBSD Foundation
5  * Copyright (c) 2021 Andrew Turner
6  *
7  * Portions of this software was developed by Aleksandr Rybalko under
8  * sponsorship from the FreeBSD Foundation.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/fbio.h>
39 
40 #include <vm/vm.h>
41 #include <vm/pmap.h>
42 
43 #include <dev/vt/vt.h>
44 #include <dev/vt/hw/fb/vt_fb.h>
45 #include <dev/vt/colors/vt_termcolors.h>
46 
47 #include <dev/ofw/openfirm.h>
48 #include <dev/ofw/ofw_bus.h>
49 #include <dev/ofw/ofw_subr.h>
50 #include <dev/ofw/ofw_bus_subr.h>
51 
52 static vd_init_t vt_simplefb_init;
53 static vd_fini_t vt_simplefb_fini;
54 static vd_probe_t vt_simplefb_probe;
55 
56 static struct vt_driver vt_simplefb_driver = {
57 	.vd_name = "simplefb",
58 	.vd_probe = vt_simplefb_probe,
59 	.vd_init = vt_simplefb_init,
60 	.vd_fini = vt_simplefb_fini,
61 	.vd_blank = vt_fb_blank,
62 	.vd_bitblt_text = vt_fb_bitblt_text,
63 	.vd_invalidate_text = vt_fb_invalidate_text,
64 	.vd_bitblt_bmp = vt_fb_bitblt_bitmap,
65 	.vd_drawrect = vt_fb_drawrect,
66 	.vd_setpixel = vt_fb_setpixel,
67 	.vd_fb_ioctl = vt_fb_ioctl,
68 	.vd_fb_mmap = vt_fb_mmap,
69 	.vd_suspend = vt_suspend,
70 	.vd_resume = vt_resume,
71 	/* Better than efifb, but still generic driver. */
72 	.vd_priority = VD_PRIORITY_GENERIC + 2,
73 };
74 
75 struct {
76 	const char *name;
77 	int rbits, rshift;
78 	int gbits, gshift;
79 	int bbits, bshift;
80 	int depth;
81 	enum vt_color_format format;
82 } simplefb_formats[] = {
83 	{
84 		.name = "r5g6b5",
85 		.rbits = 5, .rshift = 11,
86 		.gbits = 6, .gshift = 5,
87 		.bbits = 5, .bshift = 0,
88 		.depth = 16, .format = COLOR_FORMAT_RGB,
89 	},
90 	{
91 		.name = "r8g8b8",
92 		.rbits = 8, .rshift = 16,
93 		.gbits = 8, .gshift = 8,
94 		.bbits = 8, .bshift = 0,
95 		.depth = 24, .format = COLOR_FORMAT_RGB,
96 	},
97 	{
98 		.name = "a8r8g8b8",
99 		.rbits = 8, .rshift = 16,
100 		.gbits = 8, .gshift = 8,
101 		.bbits = 8, .bshift = 0,
102 		.depth = 32, .format = COLOR_FORMAT_RGB,
103 	},
104 	{
105 		.name = "x8r8g8b8",
106 		.rbits = 8, .rshift = 16,
107 		.gbits = 8, .gshift = 8,
108 		.bbits = 8, .bshift = 0,
109 		.depth = 32, .format = COLOR_FORMAT_RGB,
110 	},
111 	{
112 		.name = "x2r10g10b10",
113 		.rbits = 10, .rshift = 20,
114 		.gbits = 10, .gshift = 10,
115 		.bbits = 10, .bshift = 0,
116 		.depth = 32, .format = COLOR_FORMAT_RGB,
117 	},
118 };
119 
120 static struct fb_info local_info;
121 VT_DRIVER_DECLARE(vt_simplefb, vt_simplefb_driver);
122 
123 static bool
124 vt_simplefb_node(phandle_t *nodep)
125 {
126 	phandle_t chosen, node;
127 
128 	chosen = OF_finddevice("/chosen");
129 	if (chosen == -1)
130 		return (false);
131 
132 	for (node = OF_child(chosen); node != 0; node = OF_peer(node)) {
133 		if (ofw_bus_node_is_compatible(node, "simple-framebuffer"))
134 			break;
135 	}
136 	if (node == 0)
137 		return (false);
138 
139 	if (nodep != NULL)
140 		*nodep = node;
141 
142 	return (true);
143 }
144 
145 static int
146 vt_simplefb_probe(struct vt_device *vd)
147 {
148 	int disabled;
149 
150 	disabled = 0;
151 	TUNABLE_INT_FETCH("hw.syscons.disable", &disabled);
152 	if (disabled != 0)
153 		return (CN_DEAD);
154 
155 	if (!vt_simplefb_node(NULL))
156 		return (CN_DEAD);
157 
158 	return (CN_INTERNAL);
159 }
160 
161 static int
162 vt_simplefb_init(struct vt_device *vd)
163 {
164 	char format[16];
165 	pcell_t height, width, stride;
166 	struct fb_info *sc;
167 	phandle_t node;
168 	bus_size_t size;
169 	int error;
170 
171 	/* Initialize softc */
172 	vd->vd_softc = sc = &local_info;
173 
174 	if (!vt_simplefb_node(&node))
175 		return (CN_DEAD);
176 
177 	if (OF_getencprop(node, "height", &height, sizeof(height)) == -1 ||
178 	    OF_getencprop(node, "width", &width, sizeof(width)) == -1 ||
179 	    OF_getencprop(node, "stride", &stride, sizeof(stride)) == -1 ||
180 	    OF_getprop(node, "format", format, sizeof(format)) == -1) {
181 		return (CN_DEAD);
182 	}
183 
184 	sc->fb_height = height;
185 	sc->fb_width = width;
186 	sc->fb_stride = stride;
187 	sc->fb_cmsize = NCOLORS;
188 
189 	error = 1;
190 	for (int i = 0; i < nitems(simplefb_formats); i++) {
191 		if (strcmp(format, simplefb_formats[i].name) == 0) {
192 			vt_config_cons_colors(sc,
193 			    simplefb_formats[i].format,
194 			    (1 << simplefb_formats[i].rbits) - 1,
195 			    simplefb_formats[i].rshift,
196 			    (1 << simplefb_formats[i].gbits) - 1,
197 			    simplefb_formats[i].gshift,
198 			    (1 << simplefb_formats[i].bbits) - 1,
199 			    simplefb_formats[i].bshift);
200 			sc->fb_depth = sc->fb_bpp = simplefb_formats[i].depth;
201 			error = 0;
202 			break;
203 		}
204 	}
205 	if (error != 0)
206 		return (CN_DEAD);
207 
208 	ofw_reg_to_paddr(node, 0, &sc->fb_pbase, &size, NULL);
209 	sc->fb_vbase = (intptr_t)pmap_mapdev_attr(sc->fb_pbase,
210 	    size, VM_MEMATTR_WRITE_COMBINING);
211 	sc->fb_size = size;
212 
213 	vt_fb_init(vd);
214 
215 	return (CN_INTERNAL);
216 }
217 
218 static void
219 vt_simplefb_fini(struct vt_device *vd, void *softc)
220 {
221 	struct fb_info *sc;
222 
223 	sc = softc;
224 	vt_fb_fini(vd, softc);
225 	pmap_unmapdev((void *)sc->fb_vbase, sc->fb_size);
226 }
227