arm_simplefb.c revision 1.12 1 /* $NetBSD: arm_simplefb.c,v 1.12 2022/07/17 20:23:17 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2019 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jared McNeill <jmcneill (at) invisible.ca>.
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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "pci.h"
33 #include "opt_pci.h"
34 #include "opt_vcons.h"
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: arm_simplefb.c,v 1.12 2022/07/17 20:23:17 riastradh Exp $");
38
39 #include <sys/param.h>
40 #include <sys/bus.h>
41 #include <sys/cpu.h>
42 #include <sys/device.h>
43
44 #include <dev/fdt/fdtvar.h>
45 #include <arm/fdt/arm_fdtvar.h>
46
47 #include <dev/wscons/wsconsio.h>
48 #include <dev/wscons/wsdisplayvar.h>
49 #include <dev/rasops/rasops.h>
50 #include <dev/wsfont/wsfont.h>
51 #include <dev/wscons/wsdisplay_vconsvar.h>
52
53 #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
54 #include <dev/pci/pcireg.h>
55 #include <dev/pci/pcivar.h>
56 #include <dev/pci/pciconf.h>
57 #endif
58
59 #include <arm/fdt/arm_simplefb.h>
60
61 #include <libfdt.h>
62
63 extern struct bus_space arm_generic_bs_tag;
64
65 static struct arm_simplefb_softc {
66 uint32_t sc_width;
67 uint32_t sc_height;
68 uint32_t sc_stride;
69 uint16_t sc_depth;
70 bool sc_swapped;
71 bool sc_10bit;
72 void *sc_bits;
73 } arm_simplefb_softc;
74
75 static struct wsscreen_descr arm_simplefb_stdscreen = {
76 .name = "std",
77 .ncols = 0,
78 .nrows = 0,
79 .textops = NULL,
80 .fontwidth = 0,
81 .fontheight = 0,
82 .capabilities = 0,
83 .modecookie = NULL
84 };
85
86 static struct wsdisplay_accessops arm_simplefb_accessops;
87 static struct vcons_data arm_simplefb_vcons_data;
88 static struct vcons_screen arm_simplefb_screen;
89
90 static bus_addr_t arm_simplefb_addr;
91 static bus_size_t arm_simplefb_size;
92 static bus_space_handle_t arm_simplefb_bsh;
93
94 static const struct device_compatible_entry compat_data[] = {
95 { .compat = "simple-framebuffer" },
96 DEVICE_COMPAT_EOL
97 };
98
99 static int
100 arm_simplefb_find_node(void)
101 {
102 int chosen_phandle, child;
103
104 chosen_phandle = OF_finddevice("/chosen");
105 if (chosen_phandle == -1)
106 return -1;
107
108 for (child = OF_child(chosen_phandle); child; child = OF_peer(child)) {
109 if (!fdtbus_status_okay(child))
110 continue;
111 if (!of_compatible_match(child, compat_data))
112 continue;
113
114 return child;
115 }
116
117 return -1;
118 }
119
120 static void
121 arm_simplefb_init_screen(void *cookie, struct vcons_screen *scr,
122 int existing, long *defattr)
123 {
124 struct arm_simplefb_softc * const sc = cookie;
125 struct rasops_info *ri = &scr->scr_ri;
126
127 ri->ri_width = sc->sc_width;
128 ri->ri_height = sc->sc_height;
129 ri->ri_depth = sc->sc_depth;
130 ri->ri_stride = sc->sc_stride;
131 ri->ri_bits = sc->sc_bits;
132 ri->ri_flg = RI_CENTER | RI_FULLCLEAR | RI_CLEAR;
133
134 if (sc->sc_swapped) {
135 KASSERT(ri->ri_depth == 32);
136 ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 8;
137 ri->ri_rpos = 8;
138 ri->ri_gpos = 16;
139 ri->ri_bpos = 24;
140 } else if (sc->sc_10bit) {
141 KASSERT(ri->ri_depth == 32);
142 ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 10;
143 ri->ri_rpos = 20;
144 ri->ri_gpos = 10;
145 ri->ri_bpos = 0;
146 }
147
148 scr->scr_flags |= VCONS_LOADFONT;
149 scr->scr_flags |= VCONS_DONT_READ;
150
151 rasops_init(ri, ri->ri_height / 8, ri->ri_width / 8);
152 ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE;
153 rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
154 sc->sc_width / ri->ri_font->fontwidth);
155
156 ri->ri_hw = scr;
157 }
158
159 static int
160 arm_simplefb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
161 lwp_t *l)
162 {
163 return EPASSTHROUGH;
164 }
165
166 static paddr_t
167 arm_simplefb_mmap(void *v, void *vs, off_t offset, int prot)
168 {
169 return -1;
170 }
171
172 static void
173 arm_simplefb_pollc(void *v, int on)
174 {
175 }
176
177 #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
178 static void
179 arm_simplefb_reconfig(void *arg, uint64_t new_addr)
180 {
181 struct arm_simplefb_softc * const sc = &arm_simplefb_softc;
182 struct rasops_info *ri = &arm_simplefb_screen.scr_ri;
183 bus_space_tag_t bst = &arm_generic_bs_tag;
184
185 bus_space_unmap(bst, arm_simplefb_bsh, arm_simplefb_size);
186 bus_space_map(bst, new_addr, arm_simplefb_size,
187 BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE,
188 &arm_simplefb_bsh);
189
190 sc->sc_bits = bus_space_vaddr(bst, arm_simplefb_bsh);
191 ri->ri_bits = sc->sc_bits;
192
193 arm_simplefb_addr = (bus_addr_t)new_addr;
194 }
195 #endif
196
197 uint64_t
198 arm_simplefb_physaddr(void)
199 {
200 return arm_simplefb_addr;
201 }
202
203 void
204 arm_simplefb_preattach(void)
205 {
206 struct arm_simplefb_softc * const sc = &arm_simplefb_softc;
207 struct rasops_info *ri = &arm_simplefb_screen.scr_ri;
208 bus_space_tag_t bst = &arm_generic_bs_tag;
209 bus_space_handle_t bsh;
210 uint32_t width, height, stride;
211 const char *format;
212 bus_addr_t addr;
213 bus_size_t size;
214 uint16_t depth;
215 long defattr;
216 bool swapped = false;
217 bool is_10bit = false;
218
219 const int phandle = arm_simplefb_find_node();
220 if (phandle == -1)
221 return;
222
223 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0 || size == 0)
224 return;
225
226 if (of_getprop_uint32(phandle, "width", &width) != 0 ||
227 of_getprop_uint32(phandle, "height", &height) != 0 ||
228 of_getprop_uint32(phandle, "stride", &stride) != 0 ||
229 (format = fdtbus_get_string(phandle, "format")) == NULL)
230 return;
231
232 if (width == 0 || height == 0)
233 return;
234
235 if (strcmp(format, "a8b8g8r8") == 0 ||
236 strcmp(format, "x8r8g8b8") == 0) {
237 depth = 32;
238 } else if (strcmp(format, "r8g8b8a8") == 0 ||
239 strcmp(format, "b8g8r8x8") == 0) {
240 depth = 32;
241 swapped = true;
242 } else if (strcmp(format, "x2r10g10b10") == 0) {
243 depth = 32;
244 is_10bit = true;
245 } else if (strcmp(format, "r5g6b5") == 0) {
246 depth = 16;
247 } else {
248 return;
249 }
250
251 /*
252 * Make sure that the size of the linear FB mapping is big enough
253 * to fit the requested screen dimensions.
254 */
255 if (size < stride * height) {
256 return;
257 }
258
259 if (bus_space_map(bst, addr, size,
260 BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE, &bsh) != 0)
261 return;
262
263 arm_simplefb_addr = addr;
264 arm_simplefb_size = size;
265 arm_simplefb_bsh = bsh;
266
267 sc->sc_width = width;
268 sc->sc_height = height;
269 sc->sc_depth = depth;
270 sc->sc_stride = stride;
271 sc->sc_bits = bus_space_vaddr(bst, bsh);
272 sc->sc_swapped = swapped;
273 sc->sc_10bit = is_10bit;
274
275 wsfont_init();
276
277 arm_simplefb_accessops.ioctl = arm_simplefb_ioctl;
278 arm_simplefb_accessops.mmap = arm_simplefb_mmap;
279 arm_simplefb_accessops.pollc = arm_simplefb_pollc;
280
281 vcons_earlyinit(&arm_simplefb_vcons_data, sc, &arm_simplefb_stdscreen,
282 &arm_simplefb_accessops);
283 arm_simplefb_vcons_data.init_screen = arm_simplefb_init_screen;
284 vcons_init_screen(&arm_simplefb_vcons_data, &arm_simplefb_screen, 1,
285 &defattr);
286 arm_simplefb_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
287
288 if (ri->ri_rows < 1 || ri->ri_cols < 1)
289 return;
290
291 arm_simplefb_stdscreen.nrows = ri->ri_rows;
292 arm_simplefb_stdscreen.ncols = ri->ri_cols;
293 arm_simplefb_stdscreen.textops = &ri->ri_ops;
294 arm_simplefb_stdscreen.capabilities = ri->ri_caps;
295
296 wsdisplay_preattach(&arm_simplefb_stdscreen, ri, 0, 0, defattr);
297
298 vcons_replay_msgbuf(&arm_simplefb_screen);
299
300 #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
301 /*
302 * Let the PCI resource allocator know about our framebuffer. This
303 * lets us know if the FB base address changes so we can remap the
304 * framebuffer if necessary.
305 */
306 pciconf_resource_reserve(PCI_CONF_MAP_MEM, addr, size,
307 arm_simplefb_reconfig, NULL);
308 #endif
309 }
310