genfb.c revision 1.20 1 /* $NetBSD: genfb.c,v 1.20 2009/02/16 22:24:40 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.20 2009/02/16 22:24:40 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 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 long defattr;
148 int i, j;
149 bool console;
150
151 dict = device_properties(&sc->sc_dev);
152 prop_dictionary_get_bool(dict, "is_console", &console);
153
154 /* do not attach when we're not console */
155 if (!console) {
156 aprint_normal_dev(&sc->sc_dev, "no console, unable to continue\n");
157 return -1;
158 }
159
160 aprint_verbose_dev(&sc->sc_dev, "framebuffer at %p, size %dx%d, depth %d, "
161 "stride %d\n", sc->sc_fbaddr,
162 sc->sc_width, sc->sc_height, sc->sc_depth, sc->sc_stride);
163
164 sc->sc_defaultscreen_descr = (struct wsscreen_descr){
165 "default",
166 0, 0,
167 NULL,
168 8, 16,
169 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
170 NULL
171 };
172 sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
173 sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
174 memcpy(&sc->sc_ops, ops, sizeof(struct genfb_ops));
175 sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
176
177 sc->sc_shadowfb = malloc(sc->sc_fbsize, M_DEVBUF, M_WAITOK);
178
179 vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
180 &genfb_accessops);
181 sc->vd.init_screen = genfb_init_screen;
182
183 /* Do not print anything between this point and the screen
184 * clear operation below. Otherwise it will be lost. */
185
186 ri = &sc->sc_console_screen.scr_ri;
187
188 vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
189 &defattr);
190 sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
191
192 sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
193 sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
194 sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
195 sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
196 wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
197 defattr);
198
199 /* Clear the whole screen to bring it to a known state. */
200 (*ri->ri_ops.eraserows)(ri, 0, ri->ri_rows, defattr);
201
202 j = 0;
203 for (i = 0; i < (1 << sc->sc_depth); i++) {
204
205 sc->sc_cmap_red[i] = rasops_cmap[j];
206 sc->sc_cmap_green[i] = rasops_cmap[j + 1];
207 sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
208 genfb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1],
209 rasops_cmap[j + 2]);
210 j += 3;
211 }
212
213 if (genfb_softc == NULL)
214 genfb_softc = sc;
215
216 aa.console = console;
217 aa.scrdata = &sc->sc_screenlist;
218 aa.accessops = &genfb_accessops;
219 aa.accesscookie = &sc->vd;
220
221 config_found(&sc->sc_dev, &aa, wsemuldisplaydevprint);
222
223 return 0;
224 }
225
226 static int
227 genfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
228 struct lwp *l)
229 {
230 struct vcons_data *vd = v;
231 struct genfb_softc *sc = vd->cookie;
232 struct wsdisplay_fbinfo *wdf;
233 struct vcons_screen *ms = vd->active;
234
235 switch (cmd) {
236
237 case WSDISPLAYIO_GINFO:
238 if (ms == NULL)
239 return ENODEV;
240 wdf = (void *)data;
241 wdf->height = ms->scr_ri.ri_height;
242 wdf->width = ms->scr_ri.ri_width;
243 wdf->depth = ms->scr_ri.ri_depth;
244 wdf->cmsize = 256;
245 return 0;
246
247 case WSDISPLAYIO_GETCMAP:
248 return genfb_getcmap(sc,
249 (struct wsdisplay_cmap *)data);
250
251 case WSDISPLAYIO_PUTCMAP:
252 return genfb_putcmap(sc,
253 (struct wsdisplay_cmap *)data);
254
255 case WSDISPLAYIO_LINEBYTES:
256 *(u_int *)data = sc->sc_stride;
257 return 0;
258
259 case WSDISPLAYIO_SMODE:
260 {
261 int new_mode = *(int*)data;
262
263 /* notify the bus backend */
264 if (sc->sc_ops.genfb_ioctl)
265 return sc->sc_ops.genfb_ioctl(sc, vs,
266 cmd, data, flag, l);
267
268 if (new_mode != sc->sc_mode) {
269 sc->sc_mode = new_mode;
270 if(new_mode == WSDISPLAYIO_MODE_EMUL) {
271 genfb_restore_palette(sc);
272 vcons_redraw_screen(ms);
273 }
274 }
275 }
276 return 0;
277 default:
278 if (sc->sc_ops.genfb_ioctl)
279 return sc->sc_ops.genfb_ioctl(sc, vs, cmd,
280 data, flag, l);
281 }
282 return EPASSTHROUGH;
283 }
284
285 static paddr_t
286 genfb_mmap(void *v, void *vs, off_t offset, int prot)
287 {
288 struct vcons_data *vd = v;
289 struct genfb_softc *sc = vd->cookie;
290
291 if (sc->sc_ops.genfb_mmap)
292 return sc->sc_ops.genfb_mmap(sc, vs, offset, prot);
293
294 return -1;
295 }
296
297 static void
298 genfb_init_screen(void *cookie, struct vcons_screen *scr,
299 int existing, long *defattr)
300 {
301 struct genfb_softc *sc = cookie;
302 struct rasops_info *ri = &scr->scr_ri;
303
304 ri->ri_depth = sc->sc_depth;
305 ri->ri_width = sc->sc_width;
306 ri->ri_height = sc->sc_height;
307 ri->ri_stride = sc->sc_stride;
308 ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
309
310 if (sc->sc_shadowfb != NULL) {
311
312 ri->ri_hwbits = (char *)sc->sc_fbaddr;
313 ri->ri_bits = (char *)sc->sc_shadowfb;
314 } else
315 ri->ri_bits = (char *)sc->sc_fbaddr;
316
317 if (existing) {
318 ri->ri_flg |= RI_CLEAR;
319 }
320
321 rasops_init(ri, sc->sc_height / 8, sc->sc_width / 8);
322 ri->ri_caps = WSSCREEN_WSCOLORS;
323
324 rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
325 sc->sc_width / ri->ri_font->fontwidth);
326
327 ri->ri_hw = scr;
328 }
329
330 static int
331 genfb_putcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
332 {
333 u_char *r, *g, *b;
334 u_int index = cm->index;
335 u_int count = cm->count;
336 int i, error;
337 u_char rbuf[256], gbuf[256], bbuf[256];
338
339 #ifdef GENFB_DEBUG
340 aprint_debug("putcmap: %d %d\n",index, count);
341 #endif
342 if (cm->index >= 256 || cm->count > 256 ||
343 (cm->index + cm->count) > 256)
344 return EINVAL;
345 error = copyin(cm->red, &rbuf[index], count);
346 if (error)
347 return error;
348 error = copyin(cm->green, &gbuf[index], count);
349 if (error)
350 return error;
351 error = copyin(cm->blue, &bbuf[index], count);
352 if (error)
353 return error;
354
355 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
356 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
357 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
358
359 r = &sc->sc_cmap_red[index];
360 g = &sc->sc_cmap_green[index];
361 b = &sc->sc_cmap_blue[index];
362
363 for (i = 0; i < count; i++) {
364 genfb_putpalreg(sc, index, *r, *g, *b);
365 index++;
366 r++, g++, b++;
367 }
368 return 0;
369 }
370
371 static int
372 genfb_getcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
373 {
374 u_int index = cm->index;
375 u_int count = cm->count;
376 int error;
377
378 if (index >= 255 || count > 256 || index + count > 256)
379 return EINVAL;
380
381 error = copyout(&sc->sc_cmap_red[index], cm->red, count);
382 if (error)
383 return error;
384 error = copyout(&sc->sc_cmap_green[index], cm->green, count);
385 if (error)
386 return error;
387 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count);
388 if (error)
389 return error;
390
391 return 0;
392 }
393
394 static void
395 genfb_restore_palette(struct genfb_softc *sc)
396 {
397 int i;
398
399 for (i = 0; i < (1 << sc->sc_depth); i++) {
400 genfb_putpalreg(sc, i, sc->sc_cmap_red[i],
401 sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
402 }
403 }
404
405 static int
406 genfb_putpalreg(struct genfb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
407 uint8_t b)
408 {
409
410 if (sc->sc_cmcb) {
411
412 sc->sc_cmcb->gcc_set_mapreg(sc->sc_cmcb->gcc_cookie,
413 idx, r, g, b);
414 }
415 return 0;
416 }
417
418 void
419 genfb_cnattach(void)
420 {
421 genfb_cnattach_called = 1;
422 }
423
424 void
425 genfb_disable(void)
426 {
427 genfb_enabled = 0;
428 }
429
430 int
431 genfb_is_console(void)
432 {
433 return genfb_cnattach_called;
434 }
435
436 int
437 genfb_is_enabled(void)
438 {
439 return genfb_enabled;
440 }
441
442 int
443 genfb_borrow(bus_addr_t addr, bus_space_handle_t *hdlp)
444 {
445 struct genfb_softc *sc = genfb_softc;
446
447 if (sc && sc->sc_ops.genfb_borrow)
448 return sc->sc_ops.genfb_borrow(sc, addr, hdlp);
449 return 0;
450 }
451