xref: /freebsd/sys/dev/vt/hw/simplefb/simplefb.c (revision 51e235148a4becba94e824a44bd69687644a7f56)
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 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/fbio.h>
37 
38 #include <vm/vm.h>
39 #include <vm/pmap.h>
40 
41 #include <dev/vt/vt.h>
42 #include <dev/vt/hw/fb/vt_fb.h>
43 #include <dev/vt/colors/vt_termcolors.h>
44 
45 #include <dev/ofw/openfirm.h>
46 #include <dev/ofw/ofw_bus.h>
47 #include <dev/ofw/ofw_subr.h>
48 #include <dev/ofw/ofw_bus_subr.h>
49 
50 static vd_init_t vt_simplefb_init;
51 static vd_fini_t vt_simplefb_fini;
52 static vd_probe_t vt_simplefb_probe;
53 
54 static struct vt_driver vt_simplefb_driver = {
55 	.vd_name = "simplefb",
56 	.vd_probe = vt_simplefb_probe,
57 	.vd_init = vt_simplefb_init,
58 	.vd_fini = vt_simplefb_fini,
59 	.vd_blank = vt_fb_blank,
60 	.vd_bitblt_text = vt_fb_bitblt_text,
61 	.vd_invalidate_text = vt_fb_invalidate_text,
62 	.vd_bitblt_bmp = vt_fb_bitblt_bitmap,
63 	.vd_drawrect = vt_fb_drawrect,
64 	.vd_setpixel = vt_fb_setpixel,
65 	.vd_fb_ioctl = vt_fb_ioctl,
66 	.vd_fb_mmap = vt_fb_mmap,
67 	.vd_suspend = vt_suspend,
68 	.vd_resume = vt_resume,
69 	/* Better than efifb, but still generic driver. */
70 	.vd_priority = VD_PRIORITY_GENERIC + 2,
71 };
72 
73 struct {
74 	const char *name;
75 	int rbits, rshift;
76 	int gbits, gshift;
77 	int bbits, bshift;
78 	int depth;
79 	enum vt_color_format format;
80 } simplefb_formats[] = {
81 	{
82 		.name = "r5g6b5",
83 		.rbits = 5, .rshift = 11,
84 		.gbits = 6, .gshift = 5,
85 		.bbits = 5, .bshift = 0,
86 		.depth = 16, .format = COLOR_FORMAT_RGB,
87 	},
88 	{
89 		.name = "r8g8b8",
90 		.rbits = 8, .rshift = 16,
91 		.gbits = 8, .gshift = 8,
92 		.bbits = 8, .bshift = 0,
93 		.depth = 24, .format = COLOR_FORMAT_RGB,
94 	},
95 	{
96 		.name = "a8r8g8b8",
97 		.rbits = 8, .rshift = 16,
98 		.gbits = 8, .gshift = 8,
99 		.bbits = 8, .bshift = 0,
100 		.depth = 32, .format = COLOR_FORMAT_RGB,
101 	},
102 	{
103 		.name = "x8r8g8b8",
104 		.rbits = 8, .rshift = 16,
105 		.gbits = 8, .gshift = 8,
106 		.bbits = 8, .bshift = 0,
107 		.depth = 32, .format = COLOR_FORMAT_RGB,
108 	},
109 	{
110 		.name = "x2r10g10b10",
111 		.rbits = 10, .rshift = 20,
112 		.gbits = 10, .gshift = 10,
113 		.bbits = 10, .bshift = 0,
114 		.depth = 32, .format = COLOR_FORMAT_RGB,
115 	},
116 };
117 
118 static struct fb_info local_info;
119 VT_DRIVER_DECLARE(vt_simplefb, vt_simplefb_driver);
120 
121 static bool
122 vt_simplefb_node(phandle_t *nodep)
123 {
124 	phandle_t chosen, node;
125 
126 	chosen = OF_finddevice("/chosen");
127 	if (chosen == -1)
128 		return (false);
129 
130 	for (node = OF_child(chosen); node != 0; node = OF_peer(node)) {
131 		if (ofw_bus_node_is_compatible(node, "simple-framebuffer"))
132 			break;
133 	}
134 	if (node == 0)
135 		return (false);
136 
137 	if (nodep != NULL)
138 		*nodep = node;
139 
140 	return (true);
141 }
142 
143 static int
144 vt_simplefb_probe(struct vt_device *vd)
145 {
146 	int disabled;
147 
148 	disabled = 0;
149 	TUNABLE_INT_FETCH("hw.syscons.disable", &disabled);
150 	if (disabled != 0)
151 		return (CN_DEAD);
152 
153 	if (!vt_simplefb_node(NULL))
154 		return (CN_DEAD);
155 
156 	return (CN_INTERNAL);
157 }
158 
159 static int
160 vt_simplefb_init(struct vt_device *vd)
161 {
162 	char format[16];
163 	pcell_t height, width, stride;
164 	struct fb_info *sc;
165 	phandle_t node;
166 	bus_size_t size;
167 	int error;
168 
169 	/* Initialize softc */
170 	vd->vd_softc = sc = &local_info;
171 
172 	if (!vt_simplefb_node(&node))
173 		return (CN_DEAD);
174 
175 	if (OF_getencprop(node, "height", &height, sizeof(height)) == -1 ||
176 	    OF_getencprop(node, "width", &width, sizeof(width)) == -1 ||
177 	    OF_getencprop(node, "stride", &stride, sizeof(stride)) == -1 ||
178 	    OF_getprop(node, "format", format, sizeof(format)) == -1) {
179 		return (CN_DEAD);
180 	}
181 
182 	sc->fb_height = height;
183 	sc->fb_width = width;
184 	sc->fb_stride = stride;
185 	sc->fb_cmsize = NCOLORS;
186 
187 	error = 1;
188 	for (int i = 0; i < nitems(simplefb_formats); i++) {
189 		if (strcmp(format, simplefb_formats[i].name) == 0) {
190 			vt_config_cons_colors(sc,
191 			    simplefb_formats[i].format,
192 			    (1 << simplefb_formats[i].rbits) - 1,
193 			    simplefb_formats[i].rshift,
194 			    (1 << simplefb_formats[i].gbits) - 1,
195 			    simplefb_formats[i].gshift,
196 			    (1 << simplefb_formats[i].bbits) - 1,
197 			    simplefb_formats[i].bshift);
198 			sc->fb_depth = sc->fb_bpp = simplefb_formats[i].depth;
199 			error = 0;
200 			break;
201 		}
202 	}
203 	if (error != 0)
204 		return (CN_DEAD);
205 
206 	ofw_reg_to_paddr(node, 0, &sc->fb_pbase, &size, NULL);
207 	sc->fb_vbase = (intptr_t)pmap_mapdev_attr(sc->fb_pbase,
208 	    size, VM_MEMATTR_WRITE_COMBINING);
209 	sc->fb_size = size;
210 
211 	vt_fb_init(vd);
212 
213 	return (CN_INTERNAL);
214 }
215 
216 static void
217 vt_simplefb_fini(struct vt_device *vd, void *softc)
218 {
219 	struct fb_info *sc;
220 
221 	sc = softc;
222 	vt_fb_fini(vd, softc);
223 	pmap_unmapdev((void *)sc->fb_vbase, sc->fb_size);
224 }
225