1 /* $NetBSD: arm_simplefb.c,v 1.13 2022/10/14 22:10:15 jmcneill 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@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.13 2022/10/14 22:10:15 jmcneill 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
arm_simplefb_find_node(void)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
arm_simplefb_init_screen(void * cookie,struct vcons_screen * scr,int existing,long * defattr)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
arm_simplefb_ioctl(void * v,void * vs,u_long cmd,void * data,int flag,lwp_t * l)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
arm_simplefb_mmap(void * v,void * vs,off_t offset,int prot)167 arm_simplefb_mmap(void *v, void *vs, off_t offset, int prot)
168 {
169 return -1;
170 }
171
172 static void
arm_simplefb_pollc(void * v,int on)173 arm_simplefb_pollc(void *v, int on)
174 {
175 }
176
177 uint64_t
arm_simplefb_physaddr(void)178 arm_simplefb_physaddr(void)
179 {
180 return arm_simplefb_addr;
181 }
182
183 void
arm_simplefb_preattach(void)184 arm_simplefb_preattach(void)
185 {
186 struct arm_simplefb_softc * const sc = &arm_simplefb_softc;
187 struct rasops_info *ri = &arm_simplefb_screen.scr_ri;
188 bus_space_tag_t bst = &arm_generic_bs_tag;
189 bus_space_handle_t bsh;
190 uint32_t width, height, stride;
191 const char *format;
192 bus_addr_t addr;
193 bus_size_t size;
194 uint16_t depth;
195 long defattr;
196 bool swapped = false;
197 bool is_10bit = false;
198
199 const int phandle = arm_simplefb_find_node();
200 if (phandle == -1)
201 return;
202
203 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0 || size == 0)
204 return;
205
206 if (of_getprop_uint32(phandle, "width", &width) != 0 ||
207 of_getprop_uint32(phandle, "height", &height) != 0 ||
208 of_getprop_uint32(phandle, "stride", &stride) != 0 ||
209 (format = fdtbus_get_string(phandle, "format")) == NULL)
210 return;
211
212 if (width == 0 || height == 0)
213 return;
214
215 if (strcmp(format, "a8b8g8r8") == 0 ||
216 strcmp(format, "x8r8g8b8") == 0) {
217 depth = 32;
218 } else if (strcmp(format, "r8g8b8a8") == 0 ||
219 strcmp(format, "b8g8r8x8") == 0) {
220 depth = 32;
221 swapped = true;
222 } else if (strcmp(format, "x2r10g10b10") == 0) {
223 depth = 32;
224 is_10bit = true;
225 } else if (strcmp(format, "r5g6b5") == 0) {
226 depth = 16;
227 } else {
228 return;
229 }
230
231 /*
232 * Make sure that the size of the linear FB mapping is big enough
233 * to fit the requested screen dimensions.
234 */
235 if (size < stride * height) {
236 return;
237 }
238
239 if (bus_space_map(bst, addr, size,
240 BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE, &bsh) != 0)
241 return;
242
243 arm_simplefb_addr = addr;
244 arm_simplefb_size = size;
245 arm_simplefb_bsh = bsh;
246
247 sc->sc_width = width;
248 sc->sc_height = height;
249 sc->sc_depth = depth;
250 sc->sc_stride = stride;
251 sc->sc_bits = bus_space_vaddr(bst, bsh);
252 sc->sc_swapped = swapped;
253 sc->sc_10bit = is_10bit;
254
255 wsfont_init();
256
257 arm_simplefb_accessops.ioctl = arm_simplefb_ioctl;
258 arm_simplefb_accessops.mmap = arm_simplefb_mmap;
259 arm_simplefb_accessops.pollc = arm_simplefb_pollc;
260
261 vcons_earlyinit(&arm_simplefb_vcons_data, sc, &arm_simplefb_stdscreen,
262 &arm_simplefb_accessops);
263 arm_simplefb_vcons_data.init_screen = arm_simplefb_init_screen;
264 vcons_init_screen(&arm_simplefb_vcons_data, &arm_simplefb_screen, 1,
265 &defattr);
266 arm_simplefb_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
267
268 if (ri->ri_rows < 1 || ri->ri_cols < 1)
269 return;
270
271 arm_simplefb_stdscreen.nrows = ri->ri_rows;
272 arm_simplefb_stdscreen.ncols = ri->ri_cols;
273 arm_simplefb_stdscreen.textops = &ri->ri_ops;
274 arm_simplefb_stdscreen.capabilities = ri->ri_caps;
275
276 wsdisplay_preattach(&arm_simplefb_stdscreen, ri, 0, 0, defattr);
277
278 vcons_replay_msgbuf(&arm_simplefb_screen);
279 }
280