xref: /netbsd-src/sys/arch/arm/fdt/arm_simplefb.c (revision 9fd8799cb5ceb66c69f2eb1a6d26a1d587ba1f1e)
1 /* $NetBSD: arm_simplefb.c,v 1.4 2020/10/21 11:06:13 rin 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 
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: arm_simplefb.c,v 1.4 2020/10/21 11:06:13 rin Exp $");
37 
38 #include <sys/param.h>
39 #include <sys/bus.h>
40 #include <sys/cpu.h>
41 #include <sys/device.h>
42 
43 #include <dev/fdt/fdtvar.h>
44 #include <arm/fdt/arm_fdtvar.h>
45 
46 #include <dev/wscons/wsconsio.h>
47 #include <dev/wscons/wsdisplayvar.h>
48 #include <dev/rasops/rasops.h>
49 #include <dev/wsfont/wsfont.h>
50 #include <dev/wscons/wsdisplay_vconsvar.h>
51 
52 #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
53 #include <dev/pci/pcireg.h>
54 #include <dev/pci/pcivar.h>
55 #include <dev/pci/pciconf.h>
56 #endif
57 
58 #include <arm/fdt/arm_simplefb.h>
59 
60 #include <libfdt.h>
61 
62 extern struct bus_space arm_generic_bs_tag;
63 
64 static struct arm_simplefb_softc {
65 	uint32_t	sc_width;
66 	uint32_t	sc_height;
67 	uint32_t	sc_stride;
68 	uint16_t	sc_depth;
69 	bool		sc_swapped;
70 	void		*sc_bits;
71 } arm_simplefb_softc;
72 
73 static struct wsscreen_descr arm_simplefb_stdscreen = {
74 	.name = "std",
75 	.ncols = 0,
76 	.nrows = 0,
77 	.textops = NULL,
78 	.fontwidth = 0,
79 	.fontheight = 0,
80 	.capabilities = 0,
81 	.modecookie = NULL
82 };
83 
84 static struct wsdisplay_accessops arm_simplefb_accessops;
85 static struct vcons_data arm_simplefb_vcons_data;
86 static struct vcons_screen arm_simplefb_screen;
87 
88 static bus_addr_t arm_simplefb_addr;
89 static bus_size_t arm_simplefb_size;
90 static bus_space_handle_t arm_simplefb_bsh;
91 
92 static int
93 arm_simplefb_find_node(void)
94 {
95 	static const char * simplefb_compatible[] = { "simple-framebuffer", NULL };
96 	int chosen_phandle, child;
97 
98 	chosen_phandle = OF_finddevice("/chosen");
99 	if (chosen_phandle == -1)
100 		return -1;
101 
102 	for (child = OF_child(chosen_phandle); child; child = OF_peer(child)) {
103 		if (!fdtbus_status_okay(child))
104 			continue;
105 		if (!of_match_compatible(child, simplefb_compatible))
106 			continue;
107 
108 		return child;
109 	}
110 
111 	return -1;
112 }
113 
114 static void
115 arm_simplefb_init_screen(void *cookie, struct vcons_screen *scr,
116     int existing, long *defattr)
117 {
118 	struct arm_simplefb_softc * const sc = cookie;
119 	struct rasops_info *ri = &scr->scr_ri;
120 
121 	ri->ri_width = sc->sc_width;
122 	ri->ri_height = sc->sc_height;
123 	ri->ri_depth = sc->sc_depth;
124 	ri->ri_stride = sc->sc_stride;
125 	ri->ri_bits = sc->sc_bits;
126 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR | RI_CLEAR;
127 
128 	if (sc->sc_swapped) {
129 		KASSERT(ri->ri_depth == 32);
130 		ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 8;
131 		ri->ri_rpos =  8;
132 		ri->ri_gpos = 16;
133 		ri->ri_bpos = 24;
134 	}
135 
136 	scr->scr_flags |= VCONS_LOADFONT;
137 	scr->scr_flags |= VCONS_DONT_READ;
138 
139 	rasops_init(ri, ri->ri_height / 8, ri->ri_width / 8);
140 	ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE;
141 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
142 	    sc->sc_width / ri->ri_font->fontwidth);
143 
144 	ri->ri_hw = scr;
145 }
146 
147 static int
148 arm_simplefb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, lwp_t *l)
149 {
150 	return EPASSTHROUGH;
151 }
152 
153 static paddr_t
154 arm_simplefb_mmap(void *v, void *vs, off_t offset, int prot)
155 {
156 	return -1;
157 }
158 
159 static void
160 arm_simplefb_pollc(void *v, int on)
161 {
162 }
163 
164 #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
165 static void
166 arm_simplefb_reconfig(void *arg, uint64_t new_addr)
167 {
168 	struct arm_simplefb_softc * const sc = &arm_simplefb_softc;
169 	struct rasops_info *ri = &arm_simplefb_screen.scr_ri;
170 	bus_space_tag_t bst = &arm_generic_bs_tag;
171 
172 	bus_space_unmap(bst, arm_simplefb_bsh, arm_simplefb_size);
173 	bus_space_map(bst, new_addr, arm_simplefb_size,
174 	    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE, &arm_simplefb_bsh);
175 
176 	sc->sc_bits = bus_space_vaddr(bst, arm_simplefb_bsh);
177 	ri->ri_bits = sc->sc_bits;
178 
179 	arm_simplefb_addr = (bus_addr_t)new_addr;
180 }
181 #endif
182 
183 uint64_t
184 arm_simplefb_physaddr(void)
185 {
186 	return arm_simplefb_addr;
187 }
188 
189 void
190 arm_simplefb_preattach(void)
191 {
192 	struct arm_simplefb_softc * const sc = &arm_simplefb_softc;
193 	struct rasops_info *ri = &arm_simplefb_screen.scr_ri;
194 	bus_space_tag_t bst = &arm_generic_bs_tag;
195 	bus_space_handle_t bsh;
196 	uint32_t width, height, stride;
197 	const char *format;
198 	bus_addr_t addr;
199 	bus_size_t size;
200 	uint16_t depth;
201 	long defattr;
202 	bool swapped = false;
203 
204 	const int phandle = arm_simplefb_find_node();
205 	if (phandle == -1)
206 		return;
207 
208 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0 || size == 0)
209 		return;
210 
211 	if (of_getprop_uint32(phandle, "width", &width) != 0 ||
212 	    of_getprop_uint32(phandle, "height", &height) != 0 ||
213 	    of_getprop_uint32(phandle, "stride", &stride) != 0 ||
214 	    (format = fdtbus_get_string(phandle, "format")) == NULL)
215 		return;
216 
217 	if (width == 0 || height == 0)
218 		return;
219 
220 	if (strcmp(format, "a8b8g8r8") == 0 ||
221 	    strcmp(format, "x8r8g8b8") == 0) {
222 		depth = 32;
223 	} else if (strcmp(format, "r8g8b8a8") == 0 ||
224 		   strcmp(format, "b8g8r8x8") == 0) {
225 		depth = 32;
226 		swapped = true;
227 	} else if (strcmp(format, "r5g6b5") == 0) {
228 		depth = 16;
229 	} else {
230 		return;
231 	}
232 
233 	if (bus_space_map(bst, addr, size,
234 	    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE, &bsh) != 0)
235 		return;
236 
237 	arm_simplefb_addr = addr;
238 	arm_simplefb_size = size;
239 	arm_simplefb_bsh = bsh;
240 
241 	sc->sc_width = width;
242 	sc->sc_height = height;
243 	sc->sc_depth = depth;
244 	sc->sc_stride = stride;
245 	sc->sc_bits = bus_space_vaddr(bst, bsh);
246 	sc->sc_swapped = swapped;
247 
248 	wsfont_init();
249 
250 	arm_simplefb_accessops.ioctl = arm_simplefb_ioctl;
251 	arm_simplefb_accessops.mmap = arm_simplefb_mmap;
252 	arm_simplefb_accessops.pollc = arm_simplefb_pollc;
253 
254 	vcons_init(&arm_simplefb_vcons_data, sc, &arm_simplefb_stdscreen,
255 		&arm_simplefb_accessops);
256 	arm_simplefb_vcons_data.init_screen = arm_simplefb_init_screen;
257 	arm_simplefb_vcons_data.use_intr = 0;
258 	vcons_init_screen(&arm_simplefb_vcons_data, &arm_simplefb_screen, 1, &defattr);
259 	arm_simplefb_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
260 
261 	if (ri->ri_rows < 1 || ri->ri_cols < 1)
262 		return;
263 
264 	arm_simplefb_stdscreen.nrows = ri->ri_rows;
265 	arm_simplefb_stdscreen.ncols = ri->ri_cols;
266 	arm_simplefb_stdscreen.textops = &ri->ri_ops;
267 	arm_simplefb_stdscreen.capabilities = ri->ri_caps;
268 
269 	wsdisplay_preattach(&arm_simplefb_stdscreen, ri, 0, 0, defattr);
270 
271 	vcons_replay_msgbuf(&arm_simplefb_screen);
272 
273 #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
274 	/*
275 	 * Let the PCI resource allocator know about our framebuffer. This
276 	 * lets us know if the FB base address changes so we can remap the
277 	 * framebuffer if necessary.
278 	 */
279 	pciconf_resource_reserve(PCI_CONF_MAP_MEM, addr, size,
280 	    arm_simplefb_reconfig, NULL);
281 #endif
282 }
283