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