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