arm_simplefb.c revision 1.2
1/* $NetBSD: arm_simplefb.c,v 1.2 2020/10/19 01:12:14 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.2 2020/10/19 01:12:14 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
62extern struct bus_space arm_generic_bs_tag;
63
64static 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
73static 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
84static struct wsdisplay_accessops arm_simplefb_accessops;
85static struct vcons_data arm_simplefb_vcons_data;
86static struct vcons_screen arm_simplefb_screen;
87
88static int
89arm_simplefb_find_node(void)
90{
91	static const char * simplefb_compatible[] = { "simple-framebuffer", NULL };
92	int chosen_phandle, child;
93
94	chosen_phandle = OF_finddevice("/chosen");
95	if (chosen_phandle == -1)
96		return -1;
97
98	for (child = OF_child(chosen_phandle); child; child = OF_peer(child)) {
99		if (!fdtbus_status_okay(child))
100			continue;
101		if (!of_match_compatible(child, simplefb_compatible))
102			continue;
103
104		return child;
105	}
106
107	return -1;
108}
109
110static void
111arm_simplefb_init_screen(void *cookie, struct vcons_screen *scr,
112    int existing, long *defattr)
113{
114	struct arm_simplefb_softc * const sc = cookie;
115	struct rasops_info *ri = &scr->scr_ri;
116
117	ri->ri_width = sc->sc_width;
118	ri->ri_height = sc->sc_height;
119	ri->ri_depth = sc->sc_depth;
120	ri->ri_stride = sc->sc_stride;
121	ri->ri_bits = sc->sc_bits;
122	ri->ri_flg = RI_CENTER | RI_FULLCLEAR | RI_CLEAR;
123
124	if (sc->sc_swapped) {
125		KASSERT(ri->ri_depth == 32);
126		ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 8;
127		ri->ri_rpos =  8;
128		ri->ri_gpos = 16;
129		ri->ri_bpos = 24;
130	}
131
132	scr->scr_flags |= VCONS_LOADFONT;
133	scr->scr_flags |= VCONS_DONT_READ;
134
135	rasops_init(ri, ri->ri_height / 8, ri->ri_width / 8);
136	ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE;
137	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
138	    sc->sc_width / ri->ri_font->fontwidth);
139
140	ri->ri_hw = scr;
141}
142
143static int
144arm_simplefb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, lwp_t *l)
145{
146	return EPASSTHROUGH;
147}
148
149static paddr_t
150arm_simplefb_mmap(void *v, void *vs, off_t offset, int prot)
151{
152	return -1;
153}
154
155static void
156arm_simplefb_pollc(void *v, int on)
157{
158}
159
160void
161arm_simplefb_preattach(void)
162{
163	struct arm_simplefb_softc * const sc = &arm_simplefb_softc;
164	struct rasops_info *ri = &arm_simplefb_screen.scr_ri;
165	bus_space_tag_t bst = &arm_generic_bs_tag;
166	bus_space_handle_t bsh;
167	uint32_t width, height, stride;
168	const char *format;
169	bus_addr_t addr;
170	bus_size_t size;
171	uint16_t depth;
172	long defattr;
173	bool swapped = false;
174
175	const int phandle = arm_simplefb_find_node();
176	if (phandle == -1)
177		return;
178
179	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0 || size == 0)
180		return;
181
182	if (of_getprop_uint32(phandle, "width", &width) != 0 ||
183	    of_getprop_uint32(phandle, "height", &height) != 0 ||
184	    of_getprop_uint32(phandle, "stride", &stride) != 0 ||
185	    (format = fdtbus_get_string(phandle, "format")) == NULL)
186		return;
187
188	if (width == 0 || height == 0)
189		return;
190
191	if (strcmp(format, "a8b8g8r8") == 0 ||
192	    strcmp(format, "x8r8g8b8") == 0) {
193		depth = 32;
194	} else if (strcmp(format, "r8g8b8a8") == 0 ||
195		   strcmp(format, "b8g8r8x8") == 0) {
196		depth = 32;
197		swapped = true;
198	} else if (strcmp(format, "r5g6b5") == 0) {
199		depth = 16;
200	} else {
201		return;
202	}
203
204	if (bus_space_map(bst, addr, size,
205	    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE, &bsh) != 0)
206		return;
207
208	sc->sc_width = width;
209	sc->sc_height = height;
210	sc->sc_depth = depth;
211	sc->sc_stride = stride;
212	sc->sc_bits = bus_space_vaddr(bst, bsh);
213	sc->sc_swapped = swapped;
214
215	wsfont_init();
216
217	arm_simplefb_accessops.ioctl = arm_simplefb_ioctl;
218	arm_simplefb_accessops.mmap = arm_simplefb_mmap;
219	arm_simplefb_accessops.pollc = arm_simplefb_pollc;
220
221	vcons_init(&arm_simplefb_vcons_data, sc, &arm_simplefb_stdscreen,
222		&arm_simplefb_accessops);
223	arm_simplefb_vcons_data.init_screen = arm_simplefb_init_screen;
224	arm_simplefb_vcons_data.use_intr = 0;
225	vcons_init_screen(&arm_simplefb_vcons_data, &arm_simplefb_screen, 1, &defattr);
226	arm_simplefb_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
227
228	if (ri->ri_rows < 1 || ri->ri_cols < 1)
229		return;
230
231	arm_simplefb_stdscreen.nrows = ri->ri_rows;
232	arm_simplefb_stdscreen.ncols = ri->ri_cols;
233	arm_simplefb_stdscreen.textops = &ri->ri_ops;
234	arm_simplefb_stdscreen.capabilities = ri->ri_caps;
235
236	wsdisplay_preattach(&arm_simplefb_stdscreen, ri, 0, 0, defattr);
237
238	vcons_replay_msgbuf(&arm_simplefb_screen);
239
240#if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
241	/*
242	 * Let the PCI resource allocator know about our framebuffer. This
243	 * protects the VGA device BARs from being reprogrammed when we the
244	 * framebuffer is located in VRAM.
245	 */
246	pciconf_resource_reserve(PCI_CONF_MAP_MEM, addr, size);
247#endif
248}
249