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