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