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