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