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