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