xref: /netbsd-src/sys/dev/wsfb/genfb.c (revision 10ad5ffa714ce1a679dcc9dd8159648df2d67b5a)
1 /*	$NetBSD: genfb.c,v 1.26 2009/02/21 17:24:47 christos Exp $ */
2 
3 /*-
4  * Copyright (c) 2007 Michael Lorenz
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: genfb.c,v 1.26 2009/02/21 17:24:47 christos Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/device.h>
36 #include <sys/proc.h>
37 #include <sys/mutex.h>
38 #include <sys/ioctl.h>
39 #include <sys/kernel.h>
40 #include <sys/systm.h>
41 #include <sys/kmem.h>
42 
43 #include <dev/wscons/wsconsio.h>
44 #include <dev/wscons/wsdisplayvar.h>
45 #include <dev/rasops/rasops.h>
46 #include <dev/wsfont/wsfont.h>
47 
48 #include <dev/wscons/wsdisplay_vconsvar.h>
49 
50 #include <dev/wsfb/genfbvar.h>
51 
52 #include "opt_genfb.h"
53 #include "opt_wsfb.h"
54 
55 #ifdef GENFB_DEBUG
56 #define GPRINTF panic
57 #else
58 #define GPRINTF aprint_verbose
59 #endif
60 
61 static int	genfb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
62 static paddr_t	genfb_mmap(void *, void *, off_t, int);
63 static void	genfb_init_screen(void *, struct vcons_screen *, int, long *);
64 
65 static int	genfb_putcmap(struct genfb_softc *, struct wsdisplay_cmap *);
66 static int 	genfb_getcmap(struct genfb_softc *, struct wsdisplay_cmap *);
67 static void	genfb_restore_palette(struct genfb_softc *);
68 static int 	genfb_putpalreg(struct genfb_softc *, uint8_t, uint8_t,
69 			    uint8_t, uint8_t);
70 
71 extern const u_char rasops_cmap[768];
72 
73 static int genfb_cnattach_called = 0;
74 static int genfb_enabled = 1;
75 
76 struct wsdisplay_accessops genfb_accessops = {
77 	genfb_ioctl,
78 	genfb_mmap,
79 	NULL,	/* alloc_screen */
80 	NULL,	/* free_screen */
81 	NULL,	/* show_screen */
82 	NULL, 	/* load_font */
83 	NULL,	/* pollc */
84 	NULL	/* scroll */
85 };
86 
87 static struct genfb_softc *genfb_softc = NULL;
88 
89 void
90 genfb_init(struct genfb_softc *sc)
91 {
92 	prop_dictionary_t dict;
93 	uint64_t cmap_cb;
94 	uint32_t fboffset;
95 
96 	dict = device_properties(&sc->sc_dev);
97 #ifdef GENFB_DEBUG
98 	printf(prop_dictionary_externalize(dict));
99 #endif
100 	if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) {
101 		GPRINTF("no width property\n");
102 		return;
103 	}
104 	if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) {
105 		GPRINTF("no height property\n");
106 		return;
107 	}
108 	if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) {
109 		GPRINTF("no depth property\n");
110 		return;
111 	}
112 
113 	/* XXX should be a 64bit value */
114 	if (!prop_dictionary_get_uint32(dict, "address", &fboffset)) {
115 		GPRINTF("no address property\n");
116 		return;
117 	}
118 
119 	sc->sc_fboffset = fboffset;
120 
121 	if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->sc_stride))
122 		sc->sc_stride = (sc->sc_width * sc->sc_depth) >> 3;
123 
124 	/*
125 	 * deal with a bug in the Raptor firmware which always sets
126 	 * stride = width even when depth != 8
127 	 */
128 	if (sc->sc_stride < sc->sc_width * (sc->sc_depth >> 3))
129 		sc->sc_stride = sc->sc_width * (sc->sc_depth >> 3);
130 
131 	sc->sc_fbsize = sc->sc_height * sc->sc_stride;
132 
133 	/* optional colour map callback */
134 	sc->sc_cmcb = NULL;
135 	if (prop_dictionary_get_uint64(dict, "cmap_callback", &cmap_cb)) {
136 		if (cmap_cb != 0)
137 			sc->sc_cmcb = (void *)(vaddr_t)cmap_cb;
138 	}
139 }
140 
141 int
142 genfb_attach(struct genfb_softc *sc, struct genfb_ops *ops)
143 {
144 	struct wsemuldisplaydev_attach_args aa;
145 	prop_dictionary_t dict;
146 	struct rasops_info *ri;
147 	uint16_t crow;
148 	long defattr;
149 	int i, j;
150 	bool console;
151 
152 	dict = device_properties(&sc->sc_dev);
153 	prop_dictionary_get_bool(dict, "is_console", &console);
154 
155 	if (prop_dictionary_get_uint16(dict, "cursor-row", &crow) == false)
156 		crow = 0;
157 	if (prop_dictionary_get_bool(dict, "clear-screen", &sc->sc_want_clear)
158 	    == false)
159 		sc->sc_want_clear = true;
160 
161 	/* do not attach when we're not console */
162 	if (!console) {
163 		aprint_normal_dev(&sc->sc_dev, "no console, unable to continue\n");
164 		return -1;
165 	}
166 
167 	aprint_verbose_dev(&sc->sc_dev, "framebuffer at %p, size %dx%d, depth %d, "
168 	    "stride %d\n",
169 	    sc->sc_fboffset ? (void *)(intptr_t)sc->sc_fboffset : sc->sc_fbaddr,
170 	    sc->sc_width, sc->sc_height, sc->sc_depth, sc->sc_stride);
171 
172 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
173 		"default",
174 		0, 0,
175 		NULL,
176 		8, 16,
177 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
178 		NULL
179 	};
180 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
181 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
182 	memcpy(&sc->sc_ops, ops, sizeof(struct genfb_ops));
183 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
184 
185 	sc->sc_shadowfb = kmem_alloc(sc->sc_fbsize, KM_SLEEP);
186 	if (sc->sc_want_clear == false && sc->sc_shadowfb != NULL)
187 		memcpy(sc->sc_shadowfb, sc->sc_fbaddr, sc->sc_fbsize);
188 
189 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
190 	    &genfb_accessops);
191 	sc->vd.init_screen = genfb_init_screen;
192 
193 	/* Do not print anything between this point and the screen
194 	 * clear operation below.  Otherwise it will be lost. */
195 
196 	ri = &sc->sc_console_screen.scr_ri;
197 
198 	vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
199 	    &defattr);
200 	sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
201 
202 	sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
203 	sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
204 	sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
205 	sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
206 	wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, crow,
207 	    defattr);
208 
209 	/* Clear the whole screen to bring it to a known state. */
210 	if (sc->sc_want_clear)
211 		(*ri->ri_ops.eraserows)(ri, 0, ri->ri_rows, defattr);
212 
213 	j = 0;
214 	for (i = 0; i < (1 << sc->sc_depth); i++) {
215 
216 		sc->sc_cmap_red[i] = rasops_cmap[j];
217 		sc->sc_cmap_green[i] = rasops_cmap[j + 1];
218 		sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
219 		genfb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1],
220 		    rasops_cmap[j + 2]);
221 		j += 3;
222 	}
223 
224 	if (genfb_softc == NULL)
225 		genfb_softc = sc;
226 
227 	aa.console = console;
228 	aa.scrdata = &sc->sc_screenlist;
229 	aa.accessops = &genfb_accessops;
230 	aa.accesscookie = &sc->vd;
231 
232 	config_found(&sc->sc_dev, &aa, wsemuldisplaydevprint);
233 
234 	return 0;
235 }
236 
237 static int
238 genfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
239 	struct lwp *l)
240 {
241 	struct vcons_data *vd = v;
242 	struct genfb_softc *sc = vd->cookie;
243 	struct wsdisplay_fbinfo *wdf;
244 	struct vcons_screen *ms = vd->active;
245 	int new_mode, error;
246 
247 	switch (cmd) {
248 		case WSDISPLAYIO_GINFO:
249 			if (ms == NULL)
250 				return ENODEV;
251 			wdf = (void *)data;
252 			wdf->height = ms->scr_ri.ri_height;
253 			wdf->width = ms->scr_ri.ri_width;
254 			wdf->depth = ms->scr_ri.ri_depth;
255 			wdf->cmsize = 256;
256 			return 0;
257 
258 		case WSDISPLAYIO_GETCMAP:
259 			return genfb_getcmap(sc,
260 			    (struct wsdisplay_cmap *)data);
261 
262 		case WSDISPLAYIO_PUTCMAP:
263 			return genfb_putcmap(sc,
264 			    (struct wsdisplay_cmap *)data);
265 
266 		case WSDISPLAYIO_LINEBYTES:
267 			*(u_int *)data = sc->sc_stride;
268 			return 0;
269 
270 		case WSDISPLAYIO_SMODE:
271 			new_mode = *(int *)data;
272 
273 			/* notify the bus backend */
274 			error = 0;
275 			if (sc->sc_ops.genfb_ioctl)
276 				error = sc->sc_ops.genfb_ioctl(sc, vs,
277 					    cmd, data, flag, l);
278 			if (error)
279 				return error;
280 
281 			if (new_mode != sc->sc_mode) {
282 				sc->sc_mode = new_mode;
283 				if (new_mode == WSDISPLAYIO_MODE_EMUL) {
284 					genfb_restore_palette(sc);
285 					vcons_redraw_screen(ms);
286 				}
287 			}
288 			return 0;
289 		default:
290 			if (sc->sc_ops.genfb_ioctl)
291 				return sc->sc_ops.genfb_ioctl(sc, vs, cmd,
292 				    data, flag, l);
293 	}
294 	return EPASSTHROUGH;
295 }
296 
297 static paddr_t
298 genfb_mmap(void *v, void *vs, off_t offset, int prot)
299 {
300 	struct vcons_data *vd = v;
301 	struct genfb_softc *sc = vd->cookie;
302 
303 	if (sc->sc_ops.genfb_mmap)
304 		return sc->sc_ops.genfb_mmap(sc, vs, offset, prot);
305 
306 	return -1;
307 }
308 
309 static void
310 genfb_init_screen(void *cookie, struct vcons_screen *scr,
311     int existing, long *defattr)
312 {
313 	struct genfb_softc *sc = cookie;
314 	struct rasops_info *ri = &scr->scr_ri;
315 
316 	ri->ri_depth = sc->sc_depth;
317 	ri->ri_width = sc->sc_width;
318 	ri->ri_height = sc->sc_height;
319 	ri->ri_stride = sc->sc_stride;
320 	ri->ri_flg = RI_CENTER;
321 	if (sc->sc_want_clear)
322 		ri->ri_flg |= RI_FULLCLEAR;
323 
324 	if (sc->sc_shadowfb != NULL) {
325 
326 		ri->ri_hwbits = (char *)sc->sc_fbaddr;
327 		ri->ri_bits = (char *)sc->sc_shadowfb;
328 	} else
329 		ri->ri_bits = (char *)sc->sc_fbaddr;
330 
331 	if (existing && sc->sc_want_clear) {
332 		ri->ri_flg |= RI_CLEAR;
333 	}
334 
335 	rasops_init(ri, sc->sc_height / 8, sc->sc_width / 8);
336 	ri->ri_caps = WSSCREEN_WSCOLORS;
337 
338 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
339 		    sc->sc_width / ri->ri_font->fontwidth);
340 
341 	ri->ri_hw = scr;
342 }
343 
344 static int
345 genfb_putcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
346 {
347 	u_char *r, *g, *b;
348 	u_int index = cm->index;
349 	u_int count = cm->count;
350 	int i, error;
351 	u_char rbuf[256], gbuf[256], bbuf[256];
352 
353 #ifdef GENFB_DEBUG
354 	aprint_debug("putcmap: %d %d\n",index, count);
355 #endif
356 	if (cm->index >= 256 || cm->count > 256 ||
357 	    (cm->index + cm->count) > 256)
358 		return EINVAL;
359 	error = copyin(cm->red, &rbuf[index], count);
360 	if (error)
361 		return error;
362 	error = copyin(cm->green, &gbuf[index], count);
363 	if (error)
364 		return error;
365 	error = copyin(cm->blue, &bbuf[index], count);
366 	if (error)
367 		return error;
368 
369 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
370 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
371 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
372 
373 	r = &sc->sc_cmap_red[index];
374 	g = &sc->sc_cmap_green[index];
375 	b = &sc->sc_cmap_blue[index];
376 
377 	for (i = 0; i < count; i++) {
378 		genfb_putpalreg(sc, index, *r, *g, *b);
379 		index++;
380 		r++, g++, b++;
381 	}
382 	return 0;
383 }
384 
385 static int
386 genfb_getcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
387 {
388 	u_int index = cm->index;
389 	u_int count = cm->count;
390 	int error;
391 
392 	if (index >= 255 || count > 256 || index + count > 256)
393 		return EINVAL;
394 
395 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
396 	if (error)
397 		return error;
398 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
399 	if (error)
400 		return error;
401 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
402 	if (error)
403 		return error;
404 
405 	return 0;
406 }
407 
408 static void
409 genfb_restore_palette(struct genfb_softc *sc)
410 {
411 	int i;
412 
413 	for (i = 0; i < (1 << sc->sc_depth); i++) {
414 		genfb_putpalreg(sc, i, sc->sc_cmap_red[i],
415 		    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
416 	}
417 }
418 
419 static int
420 genfb_putpalreg(struct genfb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
421     uint8_t b)
422 {
423 
424 	if (sc->sc_cmcb) {
425 
426 		sc->sc_cmcb->gcc_set_mapreg(sc->sc_cmcb->gcc_cookie,
427 		    idx, r, g, b);
428 	}
429 	return 0;
430 }
431 
432 void
433 genfb_cnattach(void)
434 {
435 	genfb_cnattach_called = 1;
436 }
437 
438 void
439 genfb_disable(void)
440 {
441 	genfb_enabled = 0;
442 }
443 
444 int
445 genfb_is_console(void)
446 {
447 	return genfb_cnattach_called;
448 }
449 
450 int
451 genfb_is_enabled(void)
452 {
453 	return genfb_enabled;
454 }
455 
456 int
457 genfb_borrow(bus_addr_t addr, bus_space_handle_t *hdlp)
458 {
459 	struct genfb_softc *sc = genfb_softc;
460 
461 	if (sc && sc->sc_ops.genfb_borrow)
462 		return sc->sc_ops.genfb_borrow(sc, addr, hdlp);
463 	return 0;
464 }
465