xref: /openbsd-src/sys/dev/fdt/simplefb.c (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1 /*	$OpenBSD: simplefb.c,v 1.8 2020/01/22 05:25:05 patrick 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 *, long *);
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 };
107 
108 int
109 simplefb_match(struct device *parent, void *match, void *aux)
110 {
111 	struct fdt_attach_args *faa = aux;
112 
113 	/* Don't attach if it has no address space. */
114 	if (faa->fa_nreg < 1 || faa->fa_reg[0].size == 0)
115 		return 0;
116 
117 	/* Don't attach if another driver already claimed our framebuffer. */
118 	if (rasops_check_framebuffer(faa->fa_reg[0].addr))
119 		return 0;
120 
121 	return OF_is_compatible(faa->fa_node, "simple-framebuffer");
122 }
123 
124 void
125 simplefb_attach(struct device *parent, struct device *self, void *aux)
126 {
127 	struct simplefb_softc *sc = (struct simplefb_softc *)self;
128 	struct fdt_attach_args *faa = aux;
129 	struct rasops_info *ri = &sc->sc_ri;
130 	struct wsemuldisplaydev_attach_args waa;
131 	const char *format;
132 	int console = 0;
133 	long defattr;
134 
135 	format = simplefb_init(faa->fa_node, ri);
136 	if (format) {
137 		printf(": unsupported format \"%s\"\n", format);
138 		return;
139 	}
140 
141 	if (faa->fa_node == stdout_node)
142 		console = 1;
143 
144 	sc->sc_iot = faa->fa_iot;
145 	sc->sc_paddr = faa->fa_reg[0].addr;
146 	sc->sc_psize = faa->fa_reg[0].size;
147 	if (bus_space_map(sc->sc_iot, sc->sc_paddr, sc->sc_psize,
148 	    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE, &sc->sc_ioh)) {
149 		printf(": can't map framebuffer\n");
150 		return;
151 	}
152 
153 	ri->ri_bits = bus_space_vaddr(sc->sc_iot, sc->sc_ioh);
154 	ri->ri_hw = sc;
155 
156 	if (console) {
157 		/* Preserve contents. */
158 		ri->ri_bs = simplefb_bs;
159 		ri->ri_flg &= ~RI_CLEAR;
160 	}
161 
162 	printf(": %dx%d, %dbpp\n", ri->ri_width, ri->ri_height, ri->ri_depth);
163 
164 	ri->ri_flg |= RI_VCONS;
165 	rasops_init(ri, SIMPLEFB_HEIGHT, SIMPLEFB_WIDTH);
166 
167 	strlcpy(sc->sc_wsd.name, "std", sizeof(sc->sc_wsd.name));
168 	sc->sc_wsd.capabilities = ri->ri_caps;
169 	sc->sc_wsd.nrows = ri->ri_rows;
170 	sc->sc_wsd.ncols = ri->ri_cols;
171 	sc->sc_wsd.textops = &ri->ri_ops;
172 	sc->sc_wsd.fontwidth = ri->ri_font->fontwidth;
173 	sc->sc_wsd.fontheight = ri->ri_font->fontheight;
174 
175 	sc->sc_scrlist[0] = &sc->sc_wsd;
176 	sc->sc_wsl.nscreens = 1;
177 	sc->sc_wsl.screens = (const struct wsscreen_descr **)sc->sc_scrlist;
178 
179 	if (console) {
180 		ri->ri_ops.alloc_attr(ri->ri_active, 0, 0, 0, &defattr);
181 		wsdisplay_cnattach(&sc->sc_wsd, ri->ri_active,
182 		    simplefb_ri.ri_ccol, simplefb_ri.ri_crow, defattr);
183 	}
184 
185 	memset(&waa, 0, sizeof(waa));
186 	waa.scrdata = &sc->sc_wsl;
187 	waa.accessops = &simplefb_accessops;
188 	waa.accesscookie = ri;
189 	waa.console = console;
190 
191 	config_found_sm(self, &waa, wsemuldisplaydevprint,
192 	    wsemuldisplaydevsubmatch);
193 }
194 
195 const char *
196 simplefb_init(int node, struct rasops_info *ri)
197 {
198 	struct simplefb_format *fmt = NULL;
199 	static char format[16];
200 	int i;
201 
202 	format[0] = 0;
203 	OF_getprop(node, "format", format, sizeof(format));
204 	format[sizeof(format) - 1] = 0;
205 
206 	for (i = 0; i < nitems(simplefb_formats); i++) {
207 		if (strcmp(format, simplefb_formats[i].format) == 0) {
208 			fmt = &simplefb_formats[i];
209 			break;
210 		}
211 	}
212 	if (fmt == NULL)
213 		return format;
214 
215 	ri->ri_width = OF_getpropint(node, "width", 0);
216 	ri->ri_height = OF_getpropint(node, "height", 0);
217 	ri->ri_stride = OF_getpropint(node, "stride", 0);
218 	ri->ri_depth = fmt->depth;
219 	ri->ri_rpos = fmt->rpos;
220 	ri->ri_rnum = fmt->rnum;
221 	ri->ri_gpos = fmt->gpos;
222 	ri->ri_gnum = fmt->gnum;
223 	ri->ri_bpos = fmt->bpos;
224 	ri->ri_bnum = fmt->bnum;
225 	ri->ri_flg = RI_CENTER | RI_CLEAR | RI_FULLCLEAR | RI_WRONLY;
226 
227 	return NULL;
228 }
229 
230 int
231 simplefb_wsioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
232 {
233 	struct rasops_info *ri = v;
234 	struct wsdisplay_param *dp = (struct wsdisplay_param *)data;
235 	struct wsdisplay_fbinfo	*wdf;
236 
237 	switch (cmd) {
238 	case WSDISPLAYIO_GETPARAM:
239 		if (ws_get_param)
240 			return ws_get_param(dp);
241 		return -1;
242 	case WSDISPLAYIO_SETPARAM:
243 		if (ws_set_param)
244 			return ws_set_param(dp);
245 		return -1;
246 	case WSDISPLAYIO_GTYPE:
247 		*(u_int *)data = WSDISPLAY_TYPE_EFIFB;
248 		return 0;
249 	case WSDISPLAYIO_GINFO:
250 		wdf = (struct wsdisplay_fbinfo *)data;
251 		wdf->width = ri->ri_width;
252 		wdf->height = ri->ri_height;
253 		wdf->depth = ri->ri_depth;
254 		wdf->cmsize = 0;	/* color map is unavailable */
255 		break;
256 	case WSDISPLAYIO_LINEBYTES:
257 		*(u_int *)data = ri->ri_stride;
258 		break;
259 	case WSDISPLAYIO_SMODE:
260 		break;
261 	case WSDISPLAYIO_GETSUPPORTEDDEPTH:
262 		switch (ri->ri_depth) {
263 		case 32:
264 			*(u_int *)data = WSDISPLAYIO_DEPTH_24_32;
265 			break;
266 		case 24:
267 			*(u_int *)data = WSDISPLAYIO_DEPTH_24_24;
268 			break;
269 		case 16:
270 			*(u_int *)data = WSDISPLAYIO_DEPTH_16;
271 			break;
272 		case 15:
273 			*(u_int *)data = WSDISPLAYIO_DEPTH_15;
274 			break;
275 		default:
276 			return -1;
277 		}
278 		break;
279 	default:
280 		return -1;
281 	}
282 
283 	return 0;
284 }
285 
286 paddr_t
287 simplefb_wsmmap(void *v, off_t off, int prot)
288 {
289 	struct rasops_info *ri = v;
290 	struct simplefb_softc *sc = ri->ri_hw;
291 
292 	if (off < 0 || off >= sc->sc_psize)
293 		return -1;
294 
295 	return ((sc->sc_paddr + off) | PMAP_NOCACHE);
296 }
297 
298 int
299 simplefb_alloc_screen(void *v, const struct wsscreen_descr *type,
300     void **cookiep, int *curxp, int *curyp, long *attrp)
301 {
302 	return rasops_alloc_screen(v, cookiep, curxp, curyp, attrp);
303 }
304 
305 #include "ukbd.h"
306 
307 #if NUKBD > 0
308 #include <dev/usb/ukbdvar.h>
309 #endif
310 
311 void
312 simplefb_init_cons(bus_space_tag_t iot)
313 {
314 	struct rasops_info *ri = &simplefb_ri;
315 	bus_space_handle_t ioh;
316 	struct fdt_reg reg;
317 	void *node;
318 	long defattr = 0;
319 
320 	node = fdt_find_cons("simple-framebuffer");
321 	if (node == NULL)
322 		return;
323 
324 	if (fdt_get_reg(node, 0, &reg))
325 		return;
326 
327 	if (bus_space_map(iot, reg.addr, reg.size,
328 	    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE, &ioh))
329 		return;
330 
331 	ri->ri_bits = bus_space_vaddr(iot, ioh);
332 
333 	if (simplefb_init(stdout_node, ri))
334 		return;
335 
336 	ri->ri_bs = simplefb_bs;
337 	rasops_init(ri, SIMPLEFB_HEIGHT, SIMPLEFB_WIDTH);
338 
339 	simplefb_wsd.capabilities = ri->ri_caps;
340 	simplefb_wsd.ncols = ri->ri_cols;
341 	simplefb_wsd.nrows = ri->ri_rows;
342 	simplefb_wsd.textops = &ri->ri_ops;
343 	simplefb_wsd.fontwidth = ri->ri_font->fontwidth;
344 	simplefb_wsd.fontheight = ri->ri_font->fontheight;
345 
346 	ri->ri_ops.alloc_attr(ri, 0, 0, 0, &defattr);
347 	wsdisplay_cnattach(&simplefb_wsd, ri, 0, 0, defattr);
348 
349 #if NUKBD > 0
350 	/* Allow USB keyboards to become the console input device. */
351 	ukbd_cnattach();
352 #endif
353 }
354