xref: /netbsd-src/sys/dev/wsfb/genfb.c (revision 8ac07aec990b9d2e483062509d0a9fa5b4f57cf2)
1 /*	$NetBSD: genfb.c,v 1.16 2008/04/05 15:50:49 cegger 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  * 3. Neither the name of The NetBSD Foundation nor the names of its
16  *    contributors may be used to endorse or promote products derived
17  *    from this software without specific prior written permission.
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 <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: genfb.c,v 1.16 2008/04/05 15:50:49 cegger Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/device.h>
39 #include <sys/proc.h>
40 #include <sys/mutex.h>
41 #include <sys/ioctl.h>
42 #include <sys/kernel.h>
43 #include <sys/systm.h>
44 #include <sys/malloc.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 
51 #include <dev/wscons/wsdisplay_vconsvar.h>
52 
53 #include <dev/wsfb/genfbvar.h>
54 
55 #include "opt_genfb.h"
56 #include "opt_wsfb.h"
57 
58 #ifdef GENFB_DEBUG
59 #define GPRINTF panic
60 #else
61 #define GPRINTF aprint_verbose
62 #endif
63 
64 static int	genfb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
65 static paddr_t	genfb_mmap(void *, void *, off_t, int);
66 static void	genfb_init_screen(void *, struct vcons_screen *, int, long *);
67 
68 static int	genfb_putcmap(struct genfb_softc *, struct wsdisplay_cmap *);
69 static int 	genfb_getcmap(struct genfb_softc *, struct wsdisplay_cmap *);
70 static void	genfb_restore_palette(struct genfb_softc *);
71 static int 	genfb_putpalreg(struct genfb_softc *, uint8_t, uint8_t,
72 			    uint8_t, uint8_t);
73 
74 extern const u_char rasops_cmap[768];
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 void
88 genfb_init(struct genfb_softc *sc)
89 {
90 	prop_dictionary_t dict;
91 	uint64_t cmap_cb;
92 	uint32_t fboffset;
93 
94 	dict = device_properties(&sc->sc_dev);
95 #ifdef GENFB_DEBUG
96 	printf(prop_dictionary_externalize(dict));
97 #endif
98 	if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) {
99 		GPRINTF("no width property\n");
100 		return;
101 	}
102 	if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) {
103 		GPRINTF("no height property\n");
104 		return;
105 	}
106 	if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) {
107 		GPRINTF("no depth property\n");
108 		return;
109 	}
110 
111 	/* XXX should be a 64bit value */
112 	if (!prop_dictionary_get_uint32(dict, "address", &fboffset)) {
113 		GPRINTF("no address property\n");
114 		return;
115 	}
116 
117 	sc->sc_fboffset = fboffset;
118 
119 	if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->sc_stride))
120 		sc->sc_stride = (sc->sc_width * sc->sc_depth) >> 3;
121 
122 	/*
123 	 * deal with a bug in the Raptor firmware which always sets
124 	 * stride = width even when depth != 8
125 	 */
126 	if (sc->sc_stride < sc->sc_width * (sc->sc_depth >> 3))
127 		sc->sc_stride = sc->sc_width * (sc->sc_depth >> 3);
128 
129 	sc->sc_fbsize = sc->sc_height * sc->sc_stride;
130 
131 	/* optional colour map callback */
132 	sc->sc_cmcb = NULL;
133 	if (prop_dictionary_get_uint64(dict, "cmap_callback", &cmap_cb)) {
134 		if (cmap_cb != 0)
135 			sc->sc_cmcb = (void *)(vaddr_t)cmap_cb;
136 	}
137 }
138 
139 int
140 genfb_attach(struct genfb_softc *sc, struct genfb_ops *ops)
141 {
142 	struct wsemuldisplaydev_attach_args aa;
143 	prop_dictionary_t dict;
144 	struct rasops_info *ri;
145 	long defattr;
146 	int i, j;
147 	bool console;
148 
149 	dict = device_properties(&sc->sc_dev);
150 	prop_dictionary_get_bool(dict, "is_console", &console);
151 
152 	/* do not attach when we're not console */
153 	if (!console) {
154 		aprint_normal_dev(&sc->sc_dev, "no console, unable to continue\n");
155 		return -1;
156 	}
157 
158 	aprint_verbose_dev(&sc->sc_dev, "framebuffer at %p, size %dx%d, depth %d, "
159 	    "stride %d\n", sc->sc_fbaddr,
160 	    sc->sc_width, sc->sc_height, sc->sc_depth, sc->sc_stride);
161 
162 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
163 		"default",
164 		0, 0,
165 		NULL,
166 		8, 16,
167 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
168 		NULL
169 	};
170 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
171 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
172 	memcpy(&sc->sc_ops, ops, sizeof(struct genfb_ops));
173 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
174 
175 	sc->sc_shadowfb = malloc(sc->sc_fbsize, M_DEVBUF, M_WAITOK);
176 
177 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
178 	    &genfb_accessops);
179 	sc->vd.init_screen = genfb_init_screen;
180 
181 	/* Do not print anything between this point and the screen
182 	 * clear operation below.  Otherwise it will be lost. */
183 
184 	ri = &sc->sc_console_screen.scr_ri;
185 
186 	vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
187 	    &defattr);
188 	sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
189 
190 	sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
191 	sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
192 	sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
193 	sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
194 	wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
195 	    defattr);
196 
197 	/* Clear the whole screen to bring it to a known state. */
198 	(*ri->ri_ops.eraserows)(ri, 0, ri->ri_rows, defattr);
199 
200 	j = 0;
201 	for (i = 0; i < (1 << sc->sc_depth); i++) {
202 
203 		sc->sc_cmap_red[i] = rasops_cmap[j];
204 		sc->sc_cmap_green[i] = rasops_cmap[j + 1];
205 		sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
206 		genfb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1],
207 		    rasops_cmap[j + 2]);
208 		j += 3;
209 	}
210 
211 	aa.console = console;
212 	aa.scrdata = &sc->sc_screenlist;
213 	aa.accessops = &genfb_accessops;
214 	aa.accesscookie = &sc->vd;
215 
216 	config_found(&sc->sc_dev, &aa, wsemuldisplaydevprint);
217 
218 	return 0;
219 }
220 
221 static int
222 genfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
223 	struct lwp *l)
224 {
225 	struct vcons_data *vd = v;
226 	struct genfb_softc *sc = vd->cookie;
227 	struct wsdisplay_fbinfo *wdf;
228 	struct vcons_screen *ms = vd->active;
229 
230 	switch (cmd) {
231 
232 		case WSDISPLAYIO_GINFO:
233 			if (ms == NULL)
234 				return ENODEV;
235 			wdf = (void *)data;
236 			wdf->height = ms->scr_ri.ri_height;
237 			wdf->width = ms->scr_ri.ri_width;
238 			wdf->depth = ms->scr_ri.ri_depth;
239 			wdf->cmsize = 256;
240 			return 0;
241 
242 		case WSDISPLAYIO_GETCMAP:
243 			return genfb_getcmap(sc,
244 			    (struct wsdisplay_cmap *)data);
245 
246 		case WSDISPLAYIO_PUTCMAP:
247 			return genfb_putcmap(sc,
248 			    (struct wsdisplay_cmap *)data);
249 
250 		case WSDISPLAYIO_LINEBYTES:
251 			*(u_int *)data = sc->sc_stride;
252 			return 0;
253 
254 		case WSDISPLAYIO_SMODE:
255 			{
256 				int new_mode = *(int*)data;
257 
258 				/* notify the bus backend */
259 				if (sc->sc_ops.genfb_ioctl)
260 					return sc->sc_ops.genfb_ioctl(sc, vs,
261 					    cmd, data, flag, l);
262 
263 				if (new_mode != sc->sc_mode) {
264 					sc->sc_mode = new_mode;
265 					if(new_mode == WSDISPLAYIO_MODE_EMUL) {
266 						genfb_restore_palette(sc);
267 						vcons_redraw_screen(ms);
268 					}
269 				}
270 			}
271 			return 0;
272 		default:
273 			if (sc->sc_ops.genfb_ioctl)
274 				return sc->sc_ops.genfb_ioctl(sc, vs, cmd,
275 				    data, flag, l);
276 	}
277 	return EPASSTHROUGH;
278 }
279 
280 static paddr_t
281 genfb_mmap(void *v, void *vs, off_t offset, int prot)
282 {
283 	struct vcons_data *vd = v;
284 	struct genfb_softc *sc = vd->cookie;
285 
286 	if (sc->sc_ops.genfb_mmap)
287 		return sc->sc_ops.genfb_mmap(sc, vs, offset, prot);
288 
289 	return -1;
290 }
291 
292 static void
293 genfb_init_screen(void *cookie, struct vcons_screen *scr,
294     int existing, long *defattr)
295 {
296 	struct genfb_softc *sc = cookie;
297 	struct rasops_info *ri = &scr->scr_ri;
298 
299 	ri->ri_depth = sc->sc_depth;
300 	ri->ri_width = sc->sc_width;
301 	ri->ri_height = sc->sc_height;
302 	ri->ri_stride = sc->sc_stride;
303 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
304 
305 	if (sc->sc_shadowfb != NULL) {
306 
307 		ri->ri_hwbits = (char *)sc->sc_fbaddr;
308 		ri->ri_bits = (char *)sc->sc_shadowfb;
309 	} else
310 		ri->ri_bits = (char *)sc->sc_fbaddr;
311 
312 	if (existing) {
313 		ri->ri_flg |= RI_CLEAR;
314 	}
315 
316 	rasops_init(ri, sc->sc_height / 8, sc->sc_width / 8);
317 	ri->ri_caps = WSSCREEN_WSCOLORS;
318 
319 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
320 		    sc->sc_width / ri->ri_font->fontwidth);
321 
322 	ri->ri_hw = scr;
323 }
324 
325 static int
326 genfb_putcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
327 {
328 	u_char *r, *g, *b;
329 	u_int index = cm->index;
330 	u_int count = cm->count;
331 	int i, error;
332 	u_char rbuf[256], gbuf[256], bbuf[256];
333 
334 #ifdef GENFB_DEBUG
335 	aprint_debug("putcmap: %d %d\n",index, count);
336 #endif
337 	if (cm->index >= 256 || cm->count > 256 ||
338 	    (cm->index + cm->count) > 256)
339 		return EINVAL;
340 	error = copyin(cm->red, &rbuf[index], count);
341 	if (error)
342 		return error;
343 	error = copyin(cm->green, &gbuf[index], count);
344 	if (error)
345 		return error;
346 	error = copyin(cm->blue, &bbuf[index], count);
347 	if (error)
348 		return error;
349 
350 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
351 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
352 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
353 
354 	r = &sc->sc_cmap_red[index];
355 	g = &sc->sc_cmap_green[index];
356 	b = &sc->sc_cmap_blue[index];
357 
358 	for (i = 0; i < count; i++) {
359 		genfb_putpalreg(sc, index, *r, *g, *b);
360 		index++;
361 		r++, g++, b++;
362 	}
363 	return 0;
364 }
365 
366 static int
367 genfb_getcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
368 {
369 	u_int index = cm->index;
370 	u_int count = cm->count;
371 	int error;
372 
373 	if (index >= 255 || count > 256 || index + count > 256)
374 		return EINVAL;
375 
376 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
377 	if (error)
378 		return error;
379 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
380 	if (error)
381 		return error;
382 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
383 	if (error)
384 		return error;
385 
386 	return 0;
387 }
388 
389 static void
390 genfb_restore_palette(struct genfb_softc *sc)
391 {
392 	int i;
393 
394 	for (i = 0; i < (1 << sc->sc_depth); i++) {
395 		genfb_putpalreg(sc, i, sc->sc_cmap_red[i],
396 		    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
397 	}
398 }
399 
400 static int
401 genfb_putpalreg(struct genfb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
402     uint8_t b)
403 {
404 
405 	if (sc->sc_cmcb) {
406 
407 		sc->sc_cmcb->gcc_set_mapreg(sc->sc_cmcb->gcc_cookie,
408 		    idx, r, g, b);
409 	}
410 	return 0;
411 }
412 
413