voyagerfb.c revision 1.11 1 /* $NetBSD: voyagerfb.c,v 1.11 2011/12/22 07:32:33 macallan Exp $ */
2
3 /*
4 * Copyright (c) 2009, 2011 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 AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 /*
29 * A console driver for Silicon Motion SM502 / Voyager GX graphics controllers
30 * tested on GDIUM only so far
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: voyagerfb.c,v 1.11 2011/12/22 07:32:33 macallan Exp $");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/device.h>
40 #include <sys/malloc.h>
41 #include <sys/lwp.h>
42 #include <sys/kauth.h>
43
44 #include <dev/videomode/videomode.h>
45
46 #include <dev/pci/pcivar.h>
47 #include <dev/pci/pcireg.h>
48 #include <dev/pci/pcidevs.h>
49 #include <dev/pci/pciio.h>
50 #include <dev/ic/sm502reg.h>
51
52 #include <dev/wscons/wsdisplayvar.h>
53 #include <dev/wscons/wsconsio.h>
54 #include <dev/wsfont/wsfont.h>
55 #include <dev/rasops/rasops.h>
56 #include <dev/wscons/wsdisplay_vconsvar.h>
57 #include <dev/pci/wsdisplay_pci.h>
58
59 #include <dev/i2c/i2cvar.h>
60 #include <dev/pci/voyagervar.h>
61
62 #include "opt_voyagerfb.h"
63
64 #ifdef VOYAGERFB_DEBUG
65 #define DPRINTF aprint_error
66 #else
67 #define DPRINTF while (0) printf
68 #endif
69
70 /* there are probably gdium-specific */
71 #define GPIO_BACKLIGHT 0x20000000
72
73 struct voyagerfb_softc {
74 device_t sc_dev;
75
76 pci_chipset_tag_t sc_pc;
77 pcitag_t sc_pcitag;
78 bus_space_tag_t sc_memt;
79
80 bus_space_handle_t sc_fbh;
81 bus_space_handle_t sc_regh;
82 bus_addr_t sc_fb, sc_reg;
83 bus_size_t sc_fbsize, sc_regsize;
84
85 int sc_width, sc_height, sc_depth, sc_stride;
86 int sc_locked;
87 void *sc_fbaddr;
88 struct vcons_screen sc_console_screen;
89 struct wsscreen_descr sc_defaultscreen_descr;
90 const struct wsscreen_descr *sc_screens[1];
91 struct wsscreen_list sc_screenlist;
92 struct vcons_data vd;
93 uint8_t *sc_dataport;
94 int sc_mode;
95 int sc_bl_on, sc_bl_level;
96 void *sc_gpio_cookie;
97
98 /* cursor stuff */
99 int sc_cur_x;
100 int sc_cur_y;
101 int sc_hot_x;
102 int sc_hot_y;
103 uint32_t sc_cursor_addr;
104 uint32_t *sc_cursor;
105
106 /* colour map */
107 u_char sc_cmap_red[256];
108 u_char sc_cmap_green[256];
109 u_char sc_cmap_blue[256];
110 };
111
112 static int voyagerfb_match(device_t, cfdata_t, void *);
113 static void voyagerfb_attach(device_t, device_t, void *);
114
115 CFATTACH_DECL_NEW(voyagerfb, sizeof(struct voyagerfb_softc),
116 voyagerfb_match, voyagerfb_attach, NULL, NULL);
117
118 extern const u_char rasops_cmap[768];
119
120 static int voyagerfb_ioctl(void *, void *, u_long, void *, int,
121 struct lwp *);
122 static paddr_t voyagerfb_mmap(void *, void *, off_t, int);
123 static void voyagerfb_init_screen(void *, struct vcons_screen *, int,
124 long *);
125
126 static int voyagerfb_putcmap(struct voyagerfb_softc *,
127 struct wsdisplay_cmap *);
128 static int voyagerfb_getcmap(struct voyagerfb_softc *,
129 struct wsdisplay_cmap *);
130 static void voyagerfb_restore_palette(struct voyagerfb_softc *);
131 static int voyagerfb_putpalreg(struct voyagerfb_softc *, int, uint8_t,
132 uint8_t, uint8_t);
133
134 static void voyagerfb_init(struct voyagerfb_softc *);
135
136 static void voyagerfb_rectfill(struct voyagerfb_softc *, int, int, int, int,
137 uint32_t);
138 static void voyagerfb_bitblt(struct voyagerfb_softc *, int, int, int, int,
139 int, int, int);
140
141 static void voyagerfb_cursor(void *, int, int, int);
142 static void voyagerfb_putchar(void *, int, int, u_int, long);
143 static void voyagerfb_copycols(void *, int, int, int, int);
144 static void voyagerfb_erasecols(void *, int, int, int, long);
145 static void voyagerfb_copyrows(void *, int, int, int);
146 static void voyagerfb_eraserows(void *, int, int, long);
147
148 static int voyagerfb_set_curpos(struct voyagerfb_softc *, int, int);
149 static int voyagerfb_gcursor(struct voyagerfb_softc *, struct wsdisplay_cursor *);
150 static int voyagerfb_scursor(struct voyagerfb_softc *, struct wsdisplay_cursor *);
151
152 struct wsdisplay_accessops voyagerfb_accessops = {
153 voyagerfb_ioctl,
154 voyagerfb_mmap,
155 NULL, /* alloc_screen */
156 NULL, /* free_screen */
157 NULL, /* show_screen */
158 NULL, /* load_font */
159 NULL, /* pollc */
160 NULL /* scroll */
161 };
162
163 static void voyagerfb_setup_backlight(struct voyagerfb_softc *);
164 static void voyagerfb_brightness_up(device_t);
165 static void voyagerfb_brightness_down(device_t);
166 /* set backlight level */
167 static void voyagerfb_set_backlight(struct voyagerfb_softc *, int);
168 /* turn backlight on and off without messing with the level */
169 static void voyagerfb_switch_backlight(struct voyagerfb_softc *, int);
170
171 /* wait for FIFO empty so we can feed it another command */
172 static inline void
173 voyagerfb_ready(struct voyagerfb_softc *sc)
174 {
175 do {} while ((bus_space_read_4(sc->sc_memt, sc->sc_regh,
176 SM502_SYSTEM_CTRL) & SM502_SYSCTL_FIFO_EMPTY) == 0);
177 }
178
179 /* wait for the drawing engine to be idle */
180 static inline void
181 voyagerfb_wait(struct voyagerfb_softc *sc)
182 {
183 do {} while ((bus_space_read_4(sc->sc_memt, sc->sc_regh,
184 SM502_SYSTEM_CTRL) & SM502_SYSCTL_ENGINE_BUSY) != 0);
185 }
186
187 static int
188 voyagerfb_match(device_t parent, cfdata_t match, void *aux)
189 {
190 struct voyager_attach_args *vaa = (struct voyager_attach_args *)aux;
191
192 if (strcmp(vaa->vaa_name, "voyagerfb") == 0) return 100;
193 return 0;
194 }
195
196 static void
197 voyagerfb_attach(device_t parent, device_t self, void *aux)
198 {
199 struct voyagerfb_softc *sc = device_private(self);
200 struct voyager_attach_args *vaa = aux;
201 struct rasops_info *ri;
202 struct wsemuldisplaydev_attach_args aa;
203 prop_dictionary_t dict;
204 unsigned long defattr;
205 uint32_t reg;
206 bool is_console;
207 int i, j;
208
209 sc->sc_pc = vaa->vaa_pc;
210 sc->sc_pcitag = vaa->vaa_pcitag;
211 sc->sc_memt = vaa->vaa_tag;
212 sc->sc_dev = self;
213
214 aprint_normal("\n");
215
216 dict = device_properties(self);
217 prop_dictionary_get_bool(dict, "is_console", &is_console);
218
219 sc->sc_fb = vaa->vaa_mem_pa;
220 sc->sc_fbh = vaa->vaa_memh;
221 sc->sc_fbsize = 16 * 1024 * 1024;
222 sc->sc_fbaddr = bus_space_vaddr(sc->sc_memt, sc->sc_fbh);
223
224 sc->sc_reg = vaa->vaa_reg_pa;
225 sc->sc_regh = vaa->vaa_regh;
226 sc->sc_regsize = 2 * 1024 * 1024;
227 sc->sc_dataport = bus_space_vaddr(sc->sc_memt, sc->sc_regh);
228 sc->sc_dataport += SM502_DATAPORT;
229
230 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_DISP_CTRL);
231 switch (reg & SM502_PDC_DEPTH_MASK) {
232 case SM502_PDC_8BIT:
233 sc->sc_depth = 8;
234 break;
235 case SM502_PDC_16BIT:
236 sc->sc_depth = 16;
237 break;
238 case SM502_PDC_32BIT:
239 sc->sc_depth = 24;
240 break;
241 default:
242 panic("%s: unsupported depth", device_xname(self));
243 }
244 sc->sc_stride = (bus_space_read_4(sc->sc_memt, sc->sc_regh,
245 SM502_PANEL_FB_OFFSET) & SM502_FBA_WIN_STRIDE_MASK) >> 16;
246 sc->sc_width = (bus_space_read_4(sc->sc_memt, sc->sc_regh,
247 SM502_PANEL_FB_WIDTH) & SM502_FBW_WIN_WIDTH_MASK) >> 16;
248 sc->sc_height = (bus_space_read_4(sc->sc_memt, sc->sc_regh,
249 SM502_PANEL_FB_HEIGHT) & SM502_FBH_WIN_HEIGHT_MASK) >> 16;
250
251 printf("%s: %d x %d, %d bit, stride %d\n", device_xname(self),
252 sc->sc_width, sc->sc_height, sc->sc_depth, sc->sc_stride);
253 /*
254 * XXX yeah, casting the fb address to uint32_t is formally wrong
255 * but as far as I know there are no SM502 with 64bit BARs
256 */
257 aprint_normal("%s: %d MB aperture at 0x%08x\n", device_xname(self),
258 (int)(sc->sc_fbsize >> 20), (uint32_t)sc->sc_fb);
259
260 sc->sc_defaultscreen_descr = (struct wsscreen_descr){
261 "default",
262 0, 0,
263 NULL,
264 8, 16,
265 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
266 NULL
267 };
268 sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
269 sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
270 sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
271 sc->sc_locked = 0;
272
273 vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
274 &voyagerfb_accessops);
275 sc->vd.init_screen = voyagerfb_init_screen;
276
277 /* backlight control */
278 sc->sc_gpio_cookie = device_private(parent);
279 voyagerfb_setup_backlight(sc);
280
281 #ifdef VOYAGERFB_ANTIALIAS
282 sc->sc_depth = 32;
283 #else
284 sc->sc_depth = 8;
285 #endif
286
287 /* init engine here */
288 voyagerfb_init(sc);
289
290 ri = &sc->sc_console_screen.scr_ri;
291
292 if (is_console) {
293 vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
294 &defattr);
295 sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
296
297 sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
298 sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
299 sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
300 sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
301 wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
302 defattr);
303 } else {
304 /*
305 * since we're not the console we can postpone the rest
306 * until someone actually allocates a screen for us
307 */
308 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
309 }
310
311 j = 0;
312 if (sc->sc_depth <= 8) {
313 for (i = 0; i < (1 << sc->sc_depth); i++) {
314
315 sc->sc_cmap_red[i] = rasops_cmap[j];
316 sc->sc_cmap_green[i] = rasops_cmap[j + 1];
317 sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
318 voyagerfb_putpalreg(sc, i, rasops_cmap[j],
319 rasops_cmap[j + 1], rasops_cmap[j + 2]);
320 j += 3;
321 }
322 }
323
324 voyagerfb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
325 ri->ri_devcmap[(defattr >> 16) & 0xff]);
326
327 if (is_console)
328 vcons_replay_msgbuf(&sc->sc_console_screen);
329
330 aa.console = is_console;
331 aa.scrdata = &sc->sc_screenlist;
332 aa.accessops = &voyagerfb_accessops;
333 aa.accesscookie = &sc->vd;
334
335 config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
336 }
337
338 static int
339 voyagerfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
340 struct lwp *l)
341 {
342 struct vcons_data *vd = v;
343 struct voyagerfb_softc *sc = vd->cookie;
344 struct wsdisplay_fbinfo *wdf;
345 struct vcons_screen *ms = vd->active;
346 struct wsdisplay_param *param;
347
348 switch (cmd) {
349 case WSDISPLAYIO_GTYPE:
350 *(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
351 return 0;
352
353 /* PCI config read/write passthrough. */
354 case PCI_IOC_CFGREAD:
355 case PCI_IOC_CFGWRITE:
356 return pci_devioctl(sc->sc_pc, sc->sc_pcitag,
357 cmd, data, flag, l);
358
359 case WSDISPLAYIO_GET_BUSID:
360 return wsdisplayio_busid_pci(device_parent(sc->sc_dev),
361 sc->sc_pc, sc->sc_pcitag, data);
362
363 case WSDISPLAYIO_GINFO:
364 if (ms == NULL)
365 return ENODEV;
366 wdf = (void *)data;
367 wdf->height = ms->scr_ri.ri_height;
368 wdf->width = ms->scr_ri.ri_width;
369 wdf->depth = 32;
370 wdf->cmsize = 256;
371 return 0;
372
373 case WSDISPLAYIO_GETCMAP:
374 return voyagerfb_getcmap(sc,
375 (struct wsdisplay_cmap *)data);
376
377 case WSDISPLAYIO_PUTCMAP:
378 return voyagerfb_putcmap(sc,
379 (struct wsdisplay_cmap *)data);
380
381 case WSDISPLAYIO_LINEBYTES:
382 *(u_int *)data = sc->sc_stride;
383 return 0;
384
385 case WSDISPLAYIO_SMODE: {
386 int new_mode = *(int*)data;
387 if (new_mode != sc->sc_mode) {
388 sc->sc_mode = new_mode;
389 if(new_mode == WSDISPLAYIO_MODE_EMUL) {
390 #ifdef VOYAGERFB_ANTIALIAS
391 sc->sc_depth = 32;
392 #else
393 sc->sc_depth = 8;
394 #endif
395 voyagerfb_init(sc);
396 voyagerfb_restore_palette(sc);
397 vcons_redraw_screen(ms);
398 } else {
399 sc->sc_depth = 32;
400 voyagerfb_init(sc);
401 }
402 }
403 }
404 return 0;
405
406 case WSDISPLAYIO_GVIDEO:
407 *(int *)data = sc->sc_bl_on ? WSDISPLAYIO_VIDEO_ON :
408 WSDISPLAYIO_VIDEO_OFF;
409 return 0;
410
411 case WSDISPLAYIO_SVIDEO: {
412 int new_bl = *(int *)data;
413
414 voyagerfb_switch_backlight(sc, new_bl);
415 }
416 return 0;
417
418 case WSDISPLAYIO_GETPARAM:
419 param = (struct wsdisplay_param *)data;
420 switch (param->param) {
421 case WSDISPLAYIO_PARAM_BRIGHTNESS:
422 param->min = 0;
423 param->max = 255;
424 param->curval = sc->sc_bl_level;
425 return 0;
426 case WSDISPLAYIO_PARAM_BACKLIGHT:
427 param->min = 0;
428 param->max = 1;
429 param->curval = sc->sc_bl_on;
430 return 0;
431 }
432 return EPASSTHROUGH;
433
434 case WSDISPLAYIO_SETPARAM:
435 param = (struct wsdisplay_param *)data;
436 switch (param->param) {
437 case WSDISPLAYIO_PARAM_BRIGHTNESS:
438 voyagerfb_set_backlight(sc, param->curval);
439 return 0;
440 case WSDISPLAYIO_PARAM_BACKLIGHT:
441 voyagerfb_switch_backlight(sc, param->curval);
442 return 0;
443 }
444 return EPASSTHROUGH;
445
446 case WSDISPLAYIO_GCURPOS:
447 {
448 struct wsdisplay_curpos *pos;
449
450 pos = (struct wsdisplay_curpos *)data;
451 pos->x = sc->sc_cur_x;
452 pos->y = sc->sc_cur_y;
453 }
454 return 0;
455
456 case WSDISPLAYIO_SCURPOS:
457 {
458 struct wsdisplay_curpos *pos;
459
460 pos = (struct wsdisplay_curpos *)data;
461 voyagerfb_set_curpos(sc, pos->x, pos->y);
462 }
463 return 0;
464
465 case WSDISPLAYIO_GCURMAX:
466 {
467 struct wsdisplay_curpos *pos;
468
469 pos = (struct wsdisplay_curpos *)data;
470 pos->x = 64;
471 pos->y = 64;
472 }
473 return 0;
474
475 case WSDISPLAYIO_GCURSOR:
476 {
477 struct wsdisplay_cursor *cu;
478
479 cu = (struct wsdisplay_cursor *)data;
480 return voyagerfb_gcursor(sc, cu);
481 }
482
483 case WSDISPLAYIO_SCURSOR:
484 {
485 struct wsdisplay_cursor *cu;
486
487 cu = (struct wsdisplay_cursor *)data;
488 return voyagerfb_scursor(sc, cu);
489 }
490 }
491 return EPASSTHROUGH;
492 }
493
494 static paddr_t
495 voyagerfb_mmap(void *v, void *vs, off_t offset, int prot)
496 {
497 struct vcons_data *vd = v;
498 struct voyagerfb_softc *sc = vd->cookie;
499 paddr_t pa;
500
501 /* 'regular' framebuffer mmap()ing */
502 if (offset < sc->sc_fbsize) {
503 pa = bus_space_mmap(sc->sc_memt, sc->sc_fb + offset, 0, prot,
504 BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
505 return pa;
506 }
507
508 /*
509 * restrict all other mappings to processes with superuser privileges
510 * or the kernel itself
511 */
512 if (kauth_authorize_generic(kauth_cred_get(), KAUTH_GENERIC_ISSUSER,
513 NULL) != 0) {
514 aprint_normal("%s: mmap() rejected.\n",
515 device_xname(sc->sc_dev));
516 return -1;
517 }
518
519 if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
520 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
521 BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
522 return pa;
523 }
524
525 if ((offset >= sc->sc_reg) &&
526 (offset < (sc->sc_reg + sc->sc_regsize))) {
527 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot, 0);
528 return pa;
529 }
530
531 return -1;
532 }
533
534 static void
535 voyagerfb_init_screen(void *cookie, struct vcons_screen *scr,
536 int existing, long *defattr)
537 {
538 struct voyagerfb_softc *sc = cookie;
539 struct rasops_info *ri = &scr->scr_ri;
540
541 ri->ri_depth = sc->sc_depth;
542 ri->ri_width = sc->sc_width;
543 ri->ri_height = sc->sc_height;
544 ri->ri_stride = sc->sc_stride;
545 ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
546
547 ri->ri_bits = (char *)sc->sc_fbaddr;
548
549 if (existing) {
550 ri->ri_flg |= RI_CLEAR;
551 }
552 #ifdef VOYAGERFB_ANTIALIAS
553 ri->ri_flg |= RI_ENABLE_ALPHA;
554 #endif
555
556 rasops_init(ri, sc->sc_height / 8, sc->sc_width / 8);
557 ri->ri_caps = WSSCREEN_WSCOLORS;
558
559 rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
560 sc->sc_width / ri->ri_font->fontwidth);
561
562 ri->ri_hw = scr;
563 ri->ri_ops.copyrows = voyagerfb_copyrows;
564 ri->ri_ops.copycols = voyagerfb_copycols;
565 ri->ri_ops.eraserows = voyagerfb_eraserows;
566 ri->ri_ops.erasecols = voyagerfb_erasecols;
567 ri->ri_ops.cursor = voyagerfb_cursor;
568 ri->ri_ops.putchar = voyagerfb_putchar;
569 }
570
571 static int
572 voyagerfb_putcmap(struct voyagerfb_softc *sc, struct wsdisplay_cmap *cm)
573 {
574 u_char *r, *g, *b;
575 u_int index = cm->index;
576 u_int count = cm->count;
577 int i, error;
578 u_char rbuf[256], gbuf[256], bbuf[256];
579
580 #ifdef VOYAGERFB_DEBUG
581 aprint_debug("putcmap: %d %d\n",index, count);
582 #endif
583 if (cm->index >= 256 || cm->count > 256 ||
584 (cm->index + cm->count) > 256)
585 return EINVAL;
586 error = copyin(cm->red, &rbuf[index], count);
587 if (error)
588 return error;
589 error = copyin(cm->green, &gbuf[index], count);
590 if (error)
591 return error;
592 error = copyin(cm->blue, &bbuf[index], count);
593 if (error)
594 return error;
595
596 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
597 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
598 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
599
600 r = &sc->sc_cmap_red[index];
601 g = &sc->sc_cmap_green[index];
602 b = &sc->sc_cmap_blue[index];
603
604 for (i = 0; i < count; i++) {
605 voyagerfb_putpalreg(sc, index, *r, *g, *b);
606 index++;
607 r++, g++, b++;
608 }
609 return 0;
610 }
611
612 static int
613 voyagerfb_getcmap(struct voyagerfb_softc *sc, struct wsdisplay_cmap *cm)
614 {
615 u_int index = cm->index;
616 u_int count = cm->count;
617 int error;
618
619 if (index >= 255 || count > 256 || index + count > 256)
620 return EINVAL;
621
622 error = copyout(&sc->sc_cmap_red[index], cm->red, count);
623 if (error)
624 return error;
625 error = copyout(&sc->sc_cmap_green[index], cm->green, count);
626 if (error)
627 return error;
628 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count);
629 if (error)
630 return error;
631
632 return 0;
633 }
634
635 static void
636 voyagerfb_restore_palette(struct voyagerfb_softc *sc)
637 {
638 int i;
639
640 for (i = 0; i < (1 << sc->sc_depth); i++) {
641 voyagerfb_putpalreg(sc, i, sc->sc_cmap_red[i],
642 sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
643 }
644 }
645
646 static int
647 voyagerfb_putpalreg(struct voyagerfb_softc *sc, int idx, uint8_t r,
648 uint8_t g, uint8_t b)
649 {
650 uint32_t reg;
651
652 reg = (r << 16) | (g << 8) | b;
653 /* XXX we should probably write the CRT palette too */
654 bus_space_write_4(sc->sc_memt, sc->sc_regh,
655 SM502_PALETTE_PANEL + (idx << 2), reg);
656 return 0;
657 }
658
659 static void
660 voyagerfb_init(struct voyagerfb_softc *sc)
661 {
662 int reg;
663
664 voyagerfb_wait(sc);
665 /* disable colour compare */
666 bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_COLOR_COMP_MASK, 0);
667 /* allow writes to all planes */
668 bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PLANEMASK,
669 0xffffffff);
670 /* disable clipping */
671 bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_CLIP_TOP_LEFT, 0);
672 /* source and destination in local memory, no offset */
673 bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_SRC_BASE, 0);
674 bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DST_BASE, 0);
675 /* pitch is screen stride */
676 bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PITCH,
677 sc->sc_width | (sc->sc_width << 16));
678 /* window is screen width */
679 bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_WINDOW_WIDTH,
680 sc->sc_width | (sc->sc_width << 16));
681 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_DISP_CTRL);
682 reg &= ~SM502_PDC_DEPTH_MASK;
683
684 switch (sc->sc_depth) {
685 case 8:
686 bus_space_write_4(sc->sc_memt, sc->sc_regh,
687 SM502_STRETCH, SM502_STRETCH_8BIT);
688 sc->sc_stride = sc->sc_width;
689 reg |= SM502_PDC_8BIT;
690 break;
691 case 16:
692 bus_space_write_4(sc->sc_memt, sc->sc_regh,
693 SM502_STRETCH, SM502_STRETCH_16BIT);
694 sc->sc_stride = sc->sc_width << 1;
695 reg |= SM502_PDC_16BIT;
696 break;
697 case 24:
698 case 32:
699 bus_space_write_4(sc->sc_memt, sc->sc_regh,
700 SM502_STRETCH, SM502_STRETCH_32BIT);
701 sc->sc_stride = sc->sc_width << 2;
702 reg |= SM502_PDC_32BIT;
703 break;
704 }
705 bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_FB_OFFSET,
706 (sc->sc_stride << 16) | sc->sc_stride);
707 bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_DISP_CTRL,
708 reg);
709
710 /* put the cursor at the end of video memory */
711 sc->sc_cursor_addr = 16 * 1024 * 1024 - 16 * 64; /* XXX */
712 DPRINTF("%s: %08x\n", __func__, sc->sc_cursor_addr);
713 sc->sc_cursor = (uint32_t *)((uint8_t *)bus_space_vaddr(sc->sc_memt, sc->sc_fbh)
714 + sc->sc_cursor_addr);
715 #ifdef VOYAGERFB_DEBUG
716 bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_CRSR_XY, 0x00100010);
717 bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_CRSR_COL12, 0x0000ffff);
718 bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_CRSR_COL3, 0x0000f800);
719 bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_CRSR_ADDR,
720 SM502_CRSR_ENABLE | sc->sc_cursor_addr);
721 sc->sc_cursor[0] = 0x00000000;
722 sc->sc_cursor[1] = 0x00000000;
723 sc->sc_cursor[2] = 0xffffffff;
724 sc->sc_cursor[3] = 0xffffffff;
725 sc->sc_cursor[4] = 0xaaaaaaaa;
726 sc->sc_cursor[5] = 0xaaaaaaaa;
727 sc->sc_cursor[6] = 0xffffffff;
728 sc->sc_cursor[7] = 0x00000000;
729 #else
730 bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_CRSR_ADDR,
731 sc->sc_cursor_addr);
732 #endif
733 }
734
735 static void
736 voyagerfb_rectfill(struct voyagerfb_softc *sc, int x, int y, int wi, int he,
737 uint32_t colour)
738 {
739
740 voyagerfb_ready(sc);
741 bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_CONTROL,
742 ROP_COPY |
743 SM502_CTRL_USE_ROP2 |
744 SM502_CTRL_CMD_RECTFILL |
745 SM502_CTRL_QUICKSTART_E);
746 bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_FOREGROUND,
747 colour);
748 bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DST,
749 (x << 16) | y);
750 bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DIMENSION,
751 (wi << 16) | he);
752 }
753
754 static void
755 voyagerfb_bitblt(struct voyagerfb_softc *sc, int xs, int ys, int xd, int yd,
756 int wi, int he, int rop)
757 {
758 uint32_t cmd;
759
760 cmd = (rop & 0xf) | SM502_CTRL_USE_ROP2 | SM502_CTRL_CMD_BITBLT |
761 SM502_CTRL_QUICKSTART_E;
762
763 voyagerfb_ready(sc);
764
765 if (xd <= xs) {
766 /* left to right */
767 } else {
768 /*
769 * According to the manual this flag reverses only the blitter's
770 * X direction. At least on my Gdium it also reverses the Y
771 * direction
772 */
773 cmd |= SM502_CTRL_R_TO_L;
774 xs += wi - 1;
775 xd += wi - 1;
776 ys += he - 1;
777 yd += he - 1;
778 }
779
780 bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_CONTROL, cmd);
781 bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_SRC,
782 (xs << 16) | ys);
783 bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DST,
784 (xd << 16) | yd);
785 bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DIMENSION,
786 (wi << 16) | he);
787 }
788
789 static void
790 voyagerfb_cursor(void *cookie, int on, int row, int col)
791 {
792 struct rasops_info *ri = cookie;
793 struct vcons_screen *scr = ri->ri_hw;
794 struct voyagerfb_softc *sc = scr->scr_cookie;
795 int x, y, wi, he;
796
797 wi = ri->ri_font->fontwidth;
798 he = ri->ri_font->fontheight;
799
800 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
801 x = ri->ri_ccol * wi + ri->ri_xorigin;
802 y = ri->ri_crow * he + ri->ri_yorigin;
803 if (ri->ri_flg & RI_CURSOR) {
804 voyagerfb_bitblt(sc, x, y, x, y, wi, he, ROP_INVERT);
805 ri->ri_flg &= ~RI_CURSOR;
806 }
807 ri->ri_crow = row;
808 ri->ri_ccol = col;
809 if (on) {
810 x = ri->ri_ccol * wi + ri->ri_xorigin;
811 y = ri->ri_crow * he + ri->ri_yorigin;
812 voyagerfb_bitblt(sc, x, y, x, y, wi, he, ROP_INVERT);
813 ri->ri_flg |= RI_CURSOR;
814 }
815 } else {
816 scr->scr_ri.ri_crow = row;
817 scr->scr_ri.ri_ccol = col;
818 scr->scr_ri.ri_flg &= ~RI_CURSOR;
819 }
820
821 }
822
823 static inline void
824 voyagerfb_feed8(struct voyagerfb_softc *sc, uint8_t *data, int len)
825 {
826 uint32_t *port = (uint32_t *)sc->sc_dataport;
827 int i;
828
829 for (i = 0; i < ((len + 3) & 0xfffc); i++) {
830 *port = *data;
831 data++;
832 }
833 }
834
835 static inline void
836 voyagerfb_feed16(struct voyagerfb_softc *sc, uint16_t *data, int len)
837 {
838 uint32_t *port = (uint32_t *)sc->sc_dataport;
839 int i;
840
841 len = len << 1;
842 for (i = 0; i < ((len + 1) & 0xfffe); i++) {
843 *port = *data;
844 data++;
845 }
846 }
847
848
849 static void
850 voyagerfb_putchar(void *cookie, int row, int col, u_int c, long attr)
851 {
852 struct rasops_info *ri = cookie;
853 struct wsdisplay_font *font = PICK_FONT(ri, c);
854 struct vcons_screen *scr = ri->ri_hw;
855 struct voyagerfb_softc *sc = scr->scr_cookie;
856 uint32_t cmd;
857
858 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
859 int fg, bg, uc;
860 uint8_t *data;
861 int x, y, wi, he;
862 wi = font->fontwidth;
863 he = font->fontheight;
864
865 if (!CHAR_IN_FONT(c, font))
866 return;
867 bg = ri->ri_devcmap[(attr >> 16) & 0x0f];
868 fg = ri->ri_devcmap[(attr >> 24) & 0x0f];
869 x = ri->ri_xorigin + col * wi;
870 y = ri->ri_yorigin + row * he;
871 if (c == 0x20) {
872 voyagerfb_rectfill(sc, x, y, wi, he, bg);
873 return;
874 }
875 uc = c - font->firstchar;
876 data = (uint8_t *)font->data + uc * ri->ri_fontscale;
877 if (font->stride < font->fontwidth) {
878 /* this is a mono font */
879 cmd = ROP_COPY |
880 SM502_CTRL_USE_ROP2 |
881 SM502_CTRL_CMD_HOSTWRT |
882 SM502_CTRL_HOSTBLT_MONO |
883 SM502_CTRL_QUICKSTART_E |
884 SM502_CTRL_MONO_PACK_32BIT;
885 voyagerfb_ready(sc);
886 bus_space_write_4(sc->sc_memt, sc->sc_regh,
887 SM502_CONTROL, cmd);
888 bus_space_write_4(sc->sc_memt, sc->sc_regh,
889 SM502_FOREGROUND, fg);
890 bus_space_write_4(sc->sc_memt, sc->sc_regh,
891 SM502_BACKGROUND, bg);
892 bus_space_write_4(sc->sc_memt, sc->sc_regh,
893 SM502_SRC, 0);
894 bus_space_write_4(sc->sc_memt, sc->sc_regh,
895 SM502_DST, (x << 16) | y);
896 bus_space_write_4(sc->sc_memt, sc->sc_regh,
897 SM502_DIMENSION, (wi << 16) | he);
898 /* now feed the data, padded to 32bit */
899 switch (ri->ri_font->stride) {
900 case 1:
901 voyagerfb_feed8(sc, data, ri->ri_fontscale);
902 break;
903 case 2:
904 voyagerfb_feed16(sc, (uint16_t *)data,
905 ri->ri_fontscale);
906 break;
907
908 }
909 } else {
910 /*
911 * alpha font
912 * we can't accelerate the actual alpha blending but
913 * we can at least use a host blit to go through the
914 * pipeline instead of having to sync the engine
915 */
916 int i, r, g, b, alpha;
917 int rf, gf, bf, rb, gb, bb;
918 uint32_t pixel;
919
920 cmd = ROP_COPY |
921 SM502_CTRL_USE_ROP2 |
922 SM502_CTRL_CMD_HOSTWRT |
923 SM502_CTRL_QUICKSTART_E;
924 voyagerfb_ready(sc);
925 bus_space_write_4(sc->sc_memt, sc->sc_regh,
926 SM502_CONTROL, cmd);
927 bus_space_write_4(sc->sc_memt, sc->sc_regh,
928 SM502_SRC, 0);
929 bus_space_write_4(sc->sc_memt, sc->sc_regh,
930 SM502_DST, (x << 16) | y);
931 bus_space_write_4(sc->sc_memt, sc->sc_regh,
932 SM502_DIMENSION, (wi << 16) | he);
933 rf = (fg >> 16) & 0xff;
934 rb = (bg >> 16) & 0xff;
935 gf = (fg >> 8) & 0xff;
936 gb = (bg >> 8) & 0xff;
937 bf = fg & 0xff;
938 bb = bg & 0xff;
939 for (i = 0; i < wi * he; i++) {
940 alpha = *data;
941 data++;
942 r = alpha * rf + (255 - alpha) * rb;
943 g = alpha * gf + (255 - alpha) * gb;
944 b = alpha * bf + (255 - alpha) * bb;
945 pixel = (r & 0xff00) << 8 |
946 (g & 0xff00) |
947 (b & 0xff00) >> 8;
948 bus_space_write_4(sc->sc_memt, sc->sc_regh,
949 SM502_DATAPORT, pixel);
950 }
951 }
952 }
953 }
954
955 static void
956 voyagerfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
957 {
958 struct rasops_info *ri = cookie;
959 struct vcons_screen *scr = ri->ri_hw;
960 struct voyagerfb_softc *sc = scr->scr_cookie;
961 int32_t xs, xd, y, width, height;
962
963 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
964 xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
965 xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
966 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
967 width = ri->ri_font->fontwidth * ncols;
968 height = ri->ri_font->fontheight;
969 voyagerfb_bitblt(sc, xs, y, xd, y, width, height, ROP_COPY);
970 }
971 }
972
973 static void
974 voyagerfb_erasecols(void *cookie, int row, int startcol, int ncols,
975 long fillattr)
976 {
977 struct rasops_info *ri = cookie;
978 struct vcons_screen *scr = ri->ri_hw;
979 struct voyagerfb_softc *sc = scr->scr_cookie;
980 int32_t x, y, width, height, fg, bg, ul;
981
982 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
983 x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
984 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
985 width = ri->ri_font->fontwidth * ncols;
986 height = ri->ri_font->fontheight;
987 rasops_unpack_attr(fillattr, &fg, &bg, &ul);
988
989 voyagerfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
990 }
991 }
992
993 static void
994 voyagerfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
995 {
996 struct rasops_info *ri = cookie;
997 struct vcons_screen *scr = ri->ri_hw;
998 struct voyagerfb_softc *sc = scr->scr_cookie;
999 int32_t x, ys, yd, width, height;
1000 int i;
1001
1002 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1003 x = ri->ri_xorigin;
1004 ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
1005 yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
1006 width = ri->ri_emuwidth;
1007 height = ri->ri_font->fontheight * nrows;
1008 if ((nrows > 1) && (dstrow > srcrow)) {
1009 /*
1010 * the blitter can't do bottom-up copies so we have
1011 * to copy line by line here
1012 * should probably use a command sequence
1013 */
1014 ys += (height - ri->ri_font->fontheight);
1015 yd += (height - ri->ri_font->fontheight);
1016 for (i = 0; i < nrows; i++) {
1017 voyagerfb_bitblt(sc, x, ys, x, yd, width,
1018 ri->ri_font->fontheight, ROP_COPY);
1019 ys -= ri->ri_font->fontheight;
1020 yd -= ri->ri_font->fontheight;
1021 }
1022 } else
1023 voyagerfb_bitblt(sc, x, ys, x, yd, width, height,
1024 ROP_COPY);
1025 }
1026 }
1027
1028 static void
1029 voyagerfb_eraserows(void *cookie, int row, int nrows, long fillattr)
1030 {
1031 struct rasops_info *ri = cookie;
1032 struct vcons_screen *scr = ri->ri_hw;
1033 struct voyagerfb_softc *sc = scr->scr_cookie;
1034 int32_t x, y, width, height, fg, bg, ul;
1035
1036 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1037 x = ri->ri_xorigin;
1038 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1039 width = ri->ri_emuwidth;
1040 height = ri->ri_font->fontheight * nrows;
1041 rasops_unpack_attr(fillattr, &fg, &bg, &ul);
1042
1043 voyagerfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
1044 }
1045 }
1046
1047 /* backlight control */
1048 static void
1049 voyagerfb_setup_backlight(struct voyagerfb_softc *sc)
1050 {
1051 /* switch the pin to gpio mode if it isn't already */
1052 voyager_control_gpio(sc->sc_gpio_cookie, ~GPIO_BACKLIGHT, 0);
1053 /* turn it on */
1054 voyager_write_gpio(sc->sc_gpio_cookie, 0xffffffff, GPIO_BACKLIGHT);
1055 sc->sc_bl_on = 1;
1056 sc->sc_bl_level = 255;
1057 pmf_event_register(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_UP,
1058 voyagerfb_brightness_up, TRUE);
1059 pmf_event_register(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_DOWN,
1060 voyagerfb_brightness_down, TRUE);
1061 }
1062
1063 static void
1064 voyagerfb_set_backlight(struct voyagerfb_softc *sc, int level)
1065 {
1066
1067 /*
1068 * should we do nothing when backlight is off, should we just store the
1069 * level and use it when turning back on or should we just flip sc_bl_on
1070 * and turn the backlight on?
1071 * For now turn it on so a crashed screensaver can't get the user stuck
1072 * with a dark screen as long as hotkeys work
1073 */
1074 if (level > 255) level = 255;
1075 if (level < 0) level = 0;
1076 if (level == sc->sc_bl_level)
1077 return;
1078 sc->sc_bl_level = level;
1079 if (sc->sc_bl_on == 0)
1080 sc->sc_bl_on = 1;
1081 /* and here we would actually muck with the hardware */
1082 if ((level == 0) || (level == 255)) {
1083 /* in these cases bypass the PWM and use the gpio */
1084 voyager_control_gpio(sc->sc_gpio_cookie, ~GPIO_BACKLIGHT, 0);
1085 if (level == 0) {
1086 voyager_write_gpio(sc->sc_gpio_cookie, ~GPIO_BACKLIGHT, 0);
1087 } else {
1088 voyager_write_gpio(sc->sc_gpio_cookie, 0xffffffff, GPIO_BACKLIGHT);
1089 }
1090 } else {
1091 uint32_t pwm;
1092
1093 pwm = voyager_set_pwm(20000, level * 1000 / 256);
1094 pwm |= SM502_PWM_ENABLE;
1095 bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PWM0, pwm);
1096
1097 /* let the PWM take over */
1098 voyager_control_gpio(sc->sc_gpio_cookie, 0xffffffff, GPIO_BACKLIGHT);
1099 }
1100 }
1101
1102 static void
1103 voyagerfb_switch_backlight(struct voyagerfb_softc *sc, int on)
1104 {
1105
1106 if (on == sc->sc_bl_on)
1107 return;
1108 sc->sc_bl_on = on;
1109 if (on) {
1110 int level = sc->sc_bl_level;
1111
1112 sc->sc_bl_level = -1;
1113 voyagerfb_set_backlight(sc, level);
1114 } else {
1115 voyager_control_gpio(sc->sc_gpio_cookie, ~GPIO_BACKLIGHT, 0);
1116 voyager_write_gpio(sc->sc_gpio_cookie, ~GPIO_BACKLIGHT, 0);
1117 }
1118 }
1119
1120
1121 static void
1122 voyagerfb_brightness_up(device_t dev)
1123 {
1124 struct voyagerfb_softc *sc = device_private(dev);
1125
1126 voyagerfb_set_backlight(sc, sc->sc_bl_level + 8);
1127 }
1128
1129 static void
1130 voyagerfb_brightness_down(device_t dev)
1131 {
1132 struct voyagerfb_softc *sc = device_private(dev);
1133
1134 voyagerfb_set_backlight(sc, sc->sc_bl_level - 8);
1135 }
1136
1137 static int
1138 voyagerfb_set_curpos(struct voyagerfb_softc *sc, int x, int y)
1139 {
1140 uint32_t val;
1141 int xx, yy;
1142
1143 sc->sc_cur_x = x;
1144 sc->sc_cur_y = y;
1145
1146 xx = x - sc->sc_hot_x;
1147 yy = y - sc->sc_hot_y;
1148
1149 if (xx < 0) xx = abs(xx) | 0x800;
1150 if (yy < 0) yy = abs(yy) | 0x800;
1151
1152 val = (xx & 0xffff) | (yy << 16);
1153 bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_CRSR_XY, val);
1154
1155 return 0;
1156 }
1157
1158 static int
1159 voyagerfb_gcursor(struct voyagerfb_softc *sc, struct wsdisplay_cursor *cur)
1160 {
1161 /* do nothing for now */
1162 return 0;
1163 }
1164
1165 static int
1166 voyagerfb_scursor(struct voyagerfb_softc *sc, struct wsdisplay_cursor *cur)
1167 {
1168 if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
1169
1170 bus_space_write_4(sc->sc_memt, sc->sc_regh,
1171 SM502_PANEL_CRSR_ADDR,
1172 sc->sc_cursor_addr | (cur->enable ? SM502_CRSR_ENABLE : 0));
1173 DPRINTF("%s: %08x\n", __func__, sc->sc_cursor_addr);
1174 }
1175 if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
1176
1177 sc->sc_hot_x = cur->hot.x;
1178 sc->sc_hot_y = cur->hot.y;
1179 }
1180 if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
1181
1182 voyagerfb_set_curpos(sc, cur->pos.x, cur->pos.y);
1183 }
1184 if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
1185 int i, idx;
1186 uint32_t val;
1187
1188 for (i = 0; i < cur->cmap.count; i++) {
1189 val = ((cur->cmap.red[i] & 0xf8) << 8) |
1190 ((cur->cmap.green[i] & 0xfc) << 3) |
1191 ((cur->cmap.blue[i] & 0xf8) >> 3);
1192 idx = i + cur->cmap.index;
1193 bus_space_write_2(sc->sc_memt, sc->sc_regh,
1194 SM502_PANEL_CRSR_COL12 + (idx << 1),
1195 val);
1196 /*
1197 * if userland doesn't try to set the 3rd colour we
1198 * assume it expects an X11-style 2 colour cursor
1199 * X should be our main user anyway
1200 */
1201 if ((idx == 1) &&
1202 ((cur->cmap.count + cur->cmap.index) < 3)) {
1203 bus_space_write_2(sc->sc_memt, sc->sc_regh,
1204 SM502_PANEL_CRSR_COL3,
1205 val);
1206 }
1207 DPRINTF("%s: %d %04x\n", __func__, i + cur->cmap.index, val);
1208 }
1209 }
1210 if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
1211
1212 int i, j, cnt = 0;
1213 uint32_t latch = 0, omask;
1214 uint8_t imask;
1215 DPRINTF("%s: %d %d\n", __func__, cur->size.x, cur->size.y);
1216 for (i = 0; i < 256; i++) {
1217 omask = 0x00000001;
1218 imask = 0x01;
1219 cur->image[cnt] &= cur->mask[cnt];
1220 for (j = 0; j < 8; j++) {
1221 if (cur->mask[cnt] & imask)
1222 latch |= omask;
1223 omask <<= 1;
1224 if (cur->image[cnt] & imask)
1225 latch |= omask;
1226 omask <<= 1;
1227 imask <<= 1;
1228 }
1229 cnt++;
1230 imask = 0x01;
1231 cur->image[cnt] &= cur->mask[cnt];
1232 for (j = 0; j < 8; j++) {
1233 if (cur->mask[cnt] & imask)
1234 latch |= omask;
1235 omask <<= 1;
1236 if (cur->image[cnt] & imask)
1237 latch |= omask;
1238 omask <<= 1;
1239 imask <<= 1;
1240 }
1241 cnt++;
1242 sc->sc_cursor[i] = latch;
1243 latch = 0;
1244 }
1245 }
1246 return 0;
1247 }
1248