xref: /openbsd/sys/dev/fdt/simplefb.c (revision 09467b48)
1 /*	$OpenBSD: simplefb.c,v 1.11 2020/05/28 20:24:26 fcambus Exp $	*/
2 /*
3  * Copyright (c) 2016 Mark Kettenis
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <sys/param.h>
19 #include <sys/systm.h>
20 #include <sys/device.h>
21 
22 #include <uvm/uvm_extern.h>
23 
24 #include <machine/bus.h>
25 #include <machine/fdt.h>
26 
27 #include <dev/ofw/openfirm.h>
28 #include <dev/ofw/fdt.h>
29 
30 #include <dev/wscons/wsconsio.h>
31 #include <dev/wscons/wsdisplayvar.h>
32 #include <dev/rasops/rasops.h>
33 
34 #define SIMPLEFB_WIDTH	160
35 #define SIMPLEFB_HEIGHT	50
36 
37 struct simplefb_format {
38 	const char *format;
39 	int depth;
40 	int rpos, rnum;
41 	int gpos, gnum;
42 	int bpos, bnum;
43 };
44 
45 /*
46  * Supported pixel formats.  Layout ommitted when it matches the
47  * rasops defaults.
48  */
49 struct simplefb_format simplefb_formats[] = {
50 	{ "r5g6b5", 16 },
51 	{ "x1r5g5b5", 15 },
52 	{ "a1r5g5b5", 15 },
53 	{ "r8g8b8", 24 },
54 	{ "x8r8g8b8", 32, 16, 8, 8, 8, 0, 8 },
55 	{ "a8r8g8b8", 32, 16, 8, 8, 8, 0, 8 },
56 	{ "x8b8g8r8", 32 },
57 	{ "a8b8g8r8", 32 }
58 };
59 
60 struct simplefb_softc {
61 	struct device		sc_dev;
62 	bus_space_tag_t		sc_iot;
63 	bus_space_handle_t	sc_ioh;
64 
65 	struct rasops_info	sc_ri;
66 	struct wsscreen_descr	sc_wsd;
67 	struct wsscreen_list	sc_wsl;
68 	struct wsscreen_descr	*sc_scrlist[1];
69 
70 	struct simplefb_format	*sc_format;
71 	paddr_t			sc_paddr;
72 	psize_t			sc_psize;
73 };
74 
75 struct rasops_info simplefb_ri;
76 struct wsscreen_descr simplefb_wsd = { "std" };
77 struct wsdisplay_charcell simplefb_bs[SIMPLEFB_WIDTH * SIMPLEFB_HEIGHT];
78 
79 int	simplefb_match(struct device *, void *, void *);
80 void	simplefb_attach(struct device *, struct device *, void *);
81 
82 struct cfattach simplefb_ca = {
83 	sizeof(struct simplefb_softc), simplefb_match, simplefb_attach
84 };
85 
86 struct cfdriver simplefb_cd = {
87 	NULL, "simplefb", DV_DULL
88 };
89 
90 const char *simplefb_init(int, struct rasops_info *);
91 
92 int	simplefb_wsioctl(void *, u_long, caddr_t, int, struct proc *);
93 paddr_t	simplefb_wsmmap(void *, off_t, int);
94 int	simplefb_alloc_screen(void *, const struct wsscreen_descr *,
95 	    void **, int *, int *, uint32_t *);
96 
97 struct wsdisplay_accessops simplefb_accessops = {
98 	.ioctl = simplefb_wsioctl,
99 	.mmap = simplefb_wsmmap,
100 	.alloc_screen = simplefb_alloc_screen,
101 	.free_screen = rasops_free_screen,
102 	.show_screen = rasops_show_screen,
103 	.getchar = rasops_getchar,
104 	.load_font = rasops_load_font,
105 	.list_font = rasops_list_font,
106 	.scrollback = rasops_scrollback
107 };
108 
109 int
110 simplefb_match(struct device *parent, void *match, void *aux)
111 {
112 	struct fdt_attach_args *faa = aux;
113 
114 	/* Don't attach if it has no address space. */
115 	if (faa->fa_nreg < 1 || faa->fa_reg[0].size == 0)
116 		return 0;
117 
118 	/* Don't attach if another driver already claimed our framebuffer. */
119 	if (rasops_check_framebuffer(faa->fa_reg[0].addr))
120 		return 0;
121 
122 	return OF_is_compatible(faa->fa_node, "simple-framebuffer");
123 }
124 
125 void
126 simplefb_attach(struct device *parent, struct device *self, void *aux)
127 {
128 	struct simplefb_softc *sc = (struct simplefb_softc *)self;
129 	struct fdt_attach_args *faa = aux;
130 	struct rasops_info *ri = &sc->sc_ri;
131 	struct wsemuldisplaydev_attach_args waa;
132 	const char *format;
133 	int console = 0;
134 	uint32_t defattr;
135 
136 	format = simplefb_init(faa->fa_node, ri);
137 	if (format) {
138 		printf(": unsupported format \"%s\"\n", format);
139 		return;
140 	}
141 
142 	if (faa->fa_node == stdout_node)
143 		console = 1;
144 
145 	sc->sc_iot = faa->fa_iot;
146 	sc->sc_paddr = faa->fa_reg[0].addr;
147 	sc->sc_psize = faa->fa_reg[0].size;
148 	if (bus_space_map(sc->sc_iot, sc->sc_paddr, sc->sc_psize,
149 	    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE, &sc->sc_ioh)) {
150 		printf(": can't map framebuffer\n");
151 		return;
152 	}
153 
154 	ri->ri_bits = bus_space_vaddr(sc->sc_iot, sc->sc_ioh);
155 	ri->ri_hw = sc;
156 
157 	if (console) {
158 		/* Preserve contents. */
159 		ri->ri_bs = simplefb_bs;
160 		ri->ri_flg &= ~RI_CLEAR;
161 	}
162 
163 	printf(": %dx%d, %dbpp\n", ri->ri_width, ri->ri_height, ri->ri_depth);
164 
165 	ri->ri_flg |= RI_VCONS;
166 	rasops_init(ri, SIMPLEFB_HEIGHT, SIMPLEFB_WIDTH);
167 
168 	strlcpy(sc->sc_wsd.name, "std", sizeof(sc->sc_wsd.name));
169 	sc->sc_wsd.capabilities = ri->ri_caps;
170 	sc->sc_wsd.nrows = ri->ri_rows;
171 	sc->sc_wsd.ncols = ri->ri_cols;
172 	sc->sc_wsd.textops = &ri->ri_ops;
173 	sc->sc_wsd.fontwidth = ri->ri_font->fontwidth;
174 	sc->sc_wsd.fontheight = ri->ri_font->fontheight;
175 
176 	sc->sc_scrlist[0] = &sc->sc_wsd;
177 	sc->sc_wsl.nscreens = 1;
178 	sc->sc_wsl.screens = (const struct wsscreen_descr **)sc->sc_scrlist;
179 
180 	if (console) {
181 		ri->ri_ops.pack_attr(ri->ri_active, 0, 0, 0, &defattr);
182 		wsdisplay_cnattach(&sc->sc_wsd, ri->ri_active,
183 		    simplefb_ri.ri_ccol, simplefb_ri.ri_crow, defattr);
184 	}
185 
186 	memset(&waa, 0, sizeof(waa));
187 	waa.scrdata = &sc->sc_wsl;
188 	waa.accessops = &simplefb_accessops;
189 	waa.accesscookie = ri;
190 	waa.console = console;
191 
192 	config_found_sm(self, &waa, wsemuldisplaydevprint,
193 	    wsemuldisplaydevsubmatch);
194 }
195 
196 const char *
197 simplefb_init(int node, struct rasops_info *ri)
198 {
199 	struct simplefb_format *fmt = NULL;
200 	static char format[16];
201 	int i;
202 
203 	format[0] = 0;
204 	OF_getprop(node, "format", format, sizeof(format));
205 	format[sizeof(format) - 1] = 0;
206 
207 	for (i = 0; i < nitems(simplefb_formats); i++) {
208 		if (strcmp(format, simplefb_formats[i].format) == 0) {
209 			fmt = &simplefb_formats[i];
210 			break;
211 		}
212 	}
213 	if (fmt == NULL)
214 		return format;
215 
216 	ri->ri_width = OF_getpropint(node, "width", 0);
217 	ri->ri_height = OF_getpropint(node, "height", 0);
218 	ri->ri_stride = OF_getpropint(node, "stride", 0);
219 	ri->ri_depth = fmt->depth;
220 	ri->ri_rpos = fmt->rpos;
221 	ri->ri_rnum = fmt->rnum;
222 	ri->ri_gpos = fmt->gpos;
223 	ri->ri_gnum = fmt->gnum;
224 	ri->ri_bpos = fmt->bpos;
225 	ri->ri_bnum = fmt->bnum;
226 	ri->ri_flg = RI_CENTER | RI_CLEAR | RI_FULLCLEAR | RI_WRONLY;
227 
228 	return NULL;
229 }
230 
231 int
232 simplefb_wsioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
233 {
234 	struct rasops_info *ri = v;
235 	struct wsdisplay_param *dp = (struct wsdisplay_param *)data;
236 	struct wsdisplay_fbinfo	*wdf;
237 
238 	switch (cmd) {
239 	case WSDISPLAYIO_GETPARAM:
240 		if (ws_get_param)
241 			return ws_get_param(dp);
242 		return -1;
243 	case WSDISPLAYIO_SETPARAM:
244 		if (ws_set_param)
245 			return ws_set_param(dp);
246 		return -1;
247 	case WSDISPLAYIO_GTYPE:
248 		*(u_int *)data = WSDISPLAY_TYPE_EFIFB;
249 		return 0;
250 	case WSDISPLAYIO_GINFO:
251 		wdf = (struct wsdisplay_fbinfo *)data;
252 		wdf->width = ri->ri_width;
253 		wdf->height = ri->ri_height;
254 		wdf->depth = ri->ri_depth;
255 		wdf->cmsize = 0;	/* color map is unavailable */
256 		break;
257 	case WSDISPLAYIO_LINEBYTES:
258 		*(u_int *)data = ri->ri_stride;
259 		break;
260 	case WSDISPLAYIO_SMODE:
261 		break;
262 	case WSDISPLAYIO_GETSUPPORTEDDEPTH:
263 		switch (ri->ri_depth) {
264 		case 32:
265 			*(u_int *)data = WSDISPLAYIO_DEPTH_24_32;
266 			break;
267 		case 24:
268 			*(u_int *)data = WSDISPLAYIO_DEPTH_24_24;
269 			break;
270 		case 16:
271 			*(u_int *)data = WSDISPLAYIO_DEPTH_16;
272 			break;
273 		case 15:
274 			*(u_int *)data = WSDISPLAYIO_DEPTH_15;
275 			break;
276 		default:
277 			return -1;
278 		}
279 		break;
280 	default:
281 		return -1;
282 	}
283 
284 	return 0;
285 }
286 
287 paddr_t
288 simplefb_wsmmap(void *v, off_t off, int prot)
289 {
290 	struct rasops_info *ri = v;
291 	struct simplefb_softc *sc = ri->ri_hw;
292 
293 	if (off < 0 || off >= sc->sc_psize)
294 		return -1;
295 
296 	return ((sc->sc_paddr + off) | PMAP_NOCACHE);
297 }
298 
299 int
300 simplefb_alloc_screen(void *v, const struct wsscreen_descr *type,
301     void **cookiep, int *curxp, int *curyp, uint32_t *attrp)
302 {
303 	return rasops_alloc_screen(v, cookiep, curxp, curyp, attrp);
304 }
305 
306 #include "ukbd.h"
307 
308 #if NUKBD > 0
309 #include <dev/usb/ukbdvar.h>
310 #endif
311 
312 void
313 simplefb_init_cons(bus_space_tag_t iot)
314 {
315 	struct rasops_info *ri = &simplefb_ri;
316 	bus_space_handle_t ioh;
317 	struct fdt_reg reg;
318 	void *node;
319 	uint32_t defattr = 0;
320 
321 	node = fdt_find_cons("simple-framebuffer");
322 	if (node == NULL)
323 		return;
324 
325 	if (fdt_get_reg(node, 0, &reg))
326 		return;
327 
328 	if (bus_space_map(iot, reg.addr, reg.size,
329 	    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE, &ioh))
330 		return;
331 
332 	ri->ri_bits = bus_space_vaddr(iot, ioh);
333 
334 	if (simplefb_init(stdout_node, ri))
335 		return;
336 
337 	ri->ri_bs = simplefb_bs;
338 	rasops_init(ri, SIMPLEFB_HEIGHT, SIMPLEFB_WIDTH);
339 
340 	simplefb_wsd.capabilities = ri->ri_caps;
341 	simplefb_wsd.ncols = ri->ri_cols;
342 	simplefb_wsd.nrows = ri->ri_rows;
343 	simplefb_wsd.textops = &ri->ri_ops;
344 	simplefb_wsd.fontwidth = ri->ri_font->fontwidth;
345 	simplefb_wsd.fontheight = ri->ri_font->fontheight;
346 
347 	ri->ri_ops.pack_attr(ri, 0, 0, 0, &defattr);
348 	wsdisplay_cnattach(&simplefb_wsd, ri, 0, 0, defattr);
349 
350 #if NUKBD > 0
351 	/* Allow USB keyboards to become the console input device. */
352 	ukbd_cnattach();
353 #endif
354 }
355