ssdfb.c revision 1.9 1 /* $NetBSD: ssdfb.c,v 1.9 2019/11/02 14:18:36 tnn Exp $ */
2
3 /*
4 * Copyright (c) 2019 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Tobias Nygren.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: ssdfb.c,v 1.9 2019/11/02 14:18:36 tnn Exp $");
34
35 #include "opt_ddb.h"
36
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/conf.h>
40 #include <uvm/uvm.h>
41 #include <uvm/uvm_page.h>
42 #include <uvm/uvm_device.h>
43 #include <sys/condvar.h>
44 #include <sys/kmem.h>
45 #include <sys/kthread.h>
46 #include <dev/wscons/wsdisplayvar.h>
47 #include <dev/rasops/rasops.h>
48 #include <dev/ic/ssdfbvar.h>
49
50 #if defined(DDB)
51 #include <machine/db_machdep.h>
52 #include <ddb/db_extern.h>
53 #endif
54
55 /* userland interface */
56 static int ssdfb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
57 static paddr_t ssdfb_mmap(void *, void *, off_t, int);
58
59 /* wscons screen management */
60 static int ssdfb_alloc_screen(void *, const struct wsscreen_descr *,
61 void **, int *, int *, long *);
62 static void ssdfb_free_screen(void *, void *);
63 static int ssdfb_show_screen(void *, void *, int,
64 void (*cb) (void *, int, int), void *);
65
66 /* rasops hooks */
67 static void ssdfb_putchar(void *, int, int, u_int, long);
68 static void ssdfb_copycols(void *, int, int, int, int);
69 static void ssdfb_erasecols(void *, int, int, int, long);
70 static void ssdfb_copyrows(void *, int, int, int);
71 static void ssdfb_eraserows(void *, int, int, long);
72 static void ssdfb_cursor(void *, int, int, int);
73
74 /* hardware interface */
75 static int ssdfb_init_ssd1306(struct ssdfb_softc *);
76 static int ssdfb_init_ssd1322(struct ssdfb_softc *);
77 static int ssdfb_set_contrast(struct ssdfb_softc *, uint8_t, bool);
78 static int ssdfb_set_display_on(struct ssdfb_softc *, bool, bool);
79 static int ssdfb_set_mode(struct ssdfb_softc *, u_int);
80
81 /* frame buffer damage tracking and synchronization */
82 static void ssdfb_udv_attach(struct ssdfb_softc *sc);
83 static bool ssdfb_is_modified(struct ssdfb_softc *sc);
84 static bool ssdfb_clear_modify(struct ssdfb_softc *sc);
85 static void ssdfb_damage(struct ssdfb_softc *);
86 static void ssdfb_thread(void *);
87 static void ssdfb_set_usepoll(struct ssdfb_softc *, bool);
88 static int ssdfb_sync(struct ssdfb_softc *, bool);
89 static int ssdfb_sync_ssd1306(struct ssdfb_softc *, bool);
90 static int ssdfb_sync_ssd1322(struct ssdfb_softc *, bool);
91 static uint64_t ssdfb_transpose_block(uint8_t *, size_t);
92
93 /* misc helpers */
94 static const struct ssdfb_product *
95 ssdfb_lookup_product(ssdfb_product_id_t);
96 static int ssdfb_pick_font(int *, struct wsdisplay_font **);
97 static void ssdfb_clear_screen(struct ssdfb_softc *);
98 #if defined(DDB)
99 static void ssdfb_ddb_trap_callback(int);
100 #endif
101
102 static const char *ssdfb_controller_names[] = {
103 [SSDFB_CONTROLLER_UNKNOWN] = "unknown",
104 [SSDFB_CONTROLLER_SSD1306] = "Solomon Systech SSD1306",
105 [SSDFB_CONTROLLER_SH1106] = "Sino Wealth SH1106",
106 [SSDFB_CONTROLLER_SSD1322] = "Solomon Systech SSD1322"
107 };
108
109 /*
110 * Display module assemblies supported by this driver.
111 */
112 static const struct ssdfb_product ssdfb_products[] = {
113 {
114 .p_product_id = SSDFB_PRODUCT_SSD1306_GENERIC,
115 .p_controller_id = SSDFB_CONTROLLER_SSD1306,
116 .p_name = "generic",
117 .p_width = 128,
118 .p_height = 64,
119 .p_bits_per_pixel = 1,
120 .p_panel_shift = 0,
121 .p_fosc = 0x8,
122 .p_fosc_div = 0,
123 .p_precharge = 0x1,
124 .p_discharge = 0xf,
125 .p_compin_cfg = SSDFB_COM_PINS_A1_MASK
126 | SSDFB_COM_PINS_ALTERNATIVE_MASK,
127 .p_vcomh_deselect_level = SSD1306_VCOMH_DESELECT_LEVEL_0_77_VCC,
128 .p_default_contrast = 0x7f,
129 .p_multiplex_ratio = 0x3f,
130 .p_init = ssdfb_init_ssd1306,
131 .p_sync = ssdfb_sync_ssd1306
132 },
133 {
134 .p_product_id = SSDFB_PRODUCT_SH1106_GENERIC,
135 .p_controller_id = SSDFB_CONTROLLER_SH1106,
136 .p_name = "generic",
137 .p_width = 128,
138 .p_height = 64,
139 .p_bits_per_pixel = 1,
140 .p_panel_shift = 2,
141 .p_fosc = 0x5,
142 .p_fosc_div = 0,
143 .p_precharge = 0x2,
144 .p_discharge = 0x2,
145 .p_compin_cfg = SSDFB_COM_PINS_A1_MASK
146 | SSDFB_COM_PINS_ALTERNATIVE_MASK,
147 .p_vcomh_deselect_level = SH1106_VCOMH_DESELECT_LEVEL_DEFAULT,
148 .p_default_contrast = 0x80,
149 .p_multiplex_ratio = 0x3f,
150 .p_init = ssdfb_init_ssd1306,
151 .p_sync = ssdfb_sync_ssd1306
152 },
153 {
154 .p_product_id = SSDFB_PRODUCT_ADAFRUIT_938,
155 .p_controller_id = SSDFB_CONTROLLER_SSD1306,
156 .p_name = "Adafruit Industries, LLC product 938",
157 .p_width = 128,
158 .p_height = 64,
159 .p_bits_per_pixel = 1,
160 .p_panel_shift = 0,
161 .p_fosc = 0x8,
162 .p_fosc_div = 0,
163 .p_precharge = 0x1,
164 .p_discharge = 0xf,
165 .p_compin_cfg = 0x12,
166 .p_vcomh_deselect_level = 0x40,
167 .p_default_contrast = 0x8f,
168 .p_multiplex_ratio = 0x3f,
169 .p_init = ssdfb_init_ssd1306,
170 .p_sync = ssdfb_sync_ssd1306
171 },
172 {
173 .p_product_id = SSDFB_PRODUCT_ADAFRUIT_931,
174 .p_controller_id = SSDFB_CONTROLLER_SSD1306,
175 .p_name = "Adafruit Industries, LLC product 931",
176 .p_width = 128,
177 .p_height = 32,
178 .p_bits_per_pixel = 1,
179 .p_panel_shift = 0,
180 .p_fosc = 0x8,
181 .p_fosc_div = 0,
182 .p_precharge = 0x1,
183 .p_discharge = 0xf,
184 .p_compin_cfg = 0x2,
185 .p_vcomh_deselect_level = 0x40,
186 .p_default_contrast = 0x8f,
187 .p_multiplex_ratio = 0x1f,
188 .p_init = ssdfb_init_ssd1306,
189 .p_sync = ssdfb_sync_ssd1306
190 },
191 {
192 .p_product_id = SSDFB_PRODUCT_SSD1322_GENERIC,
193 .p_controller_id = SSDFB_CONTROLLER_SSD1322,
194 .p_name = "generic",
195 .p_width = 256,
196 .p_height = 64,
197 .p_bits_per_pixel = 4,
198 .p_panel_shift = 28,
199 .p_vcomh_deselect_level = SSD1322_DEFAULT_VCOMH,
200 .p_fosc = SSD1322_DEFAULT_FREQUENCY,
201 .p_fosc_div = SSD1322_DEFAULT_DIVIDER,
202 .p_default_contrast = SSD1322_DEFAULT_CONTRAST_CURRENT,
203 .p_multiplex_ratio = 0x3f,
204 .p_init = ssdfb_init_ssd1322,
205 .p_sync = ssdfb_sync_ssd1322
206 }
207 };
208
209 static const struct wsdisplay_accessops ssdfb_accessops = {
210 .ioctl = ssdfb_ioctl,
211 .mmap = ssdfb_mmap,
212 .alloc_screen = ssdfb_alloc_screen,
213 .free_screen = ssdfb_free_screen,
214 .show_screen = ssdfb_show_screen
215 };
216
217 #define SSDFB_CMD1(c) do { cmd[0] = (c); error = sc->sc_cmd(sc->sc_cookie, cmd, 1, usepoll); } while(0)
218 #define SSDFB_CMD2(c, a) do { cmd[0] = (c); cmd[1] = (a); error = sc->sc_cmd(sc->sc_cookie, cmd, 2, usepoll); } while(0)
219 #define SSDFB_CMD3(c, a, b) do { cmd[0] = (c); cmd[1] = (a); cmd[2] = (b); error = sc->sc_cmd(sc->sc_cookie, cmd, 3, usepoll); } while(0)
220
221 void
222 ssdfb_attach(struct ssdfb_softc *sc, int flags)
223 {
224 struct wsemuldisplaydev_attach_args aa;
225 struct rasops_info *ri = &sc->sc_ri;
226 int error = 0;
227 long defattr;
228 const struct ssdfb_product *p;
229 int kt_flags;
230
231 p = ssdfb_lookup_product(flags & SSDFB_ATTACH_FLAG_PRODUCT_MASK);
232 if (p == NULL) {
233 aprint_error(": unknown display assembly\n");
234 return;
235 }
236 sc->sc_p = p;
237
238 aprint_naive("\n");
239 aprint_normal(": %s (%s)\n",
240 ssdfb_controller_names[p->p_controller_id],
241 p->p_name);
242
243 sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
244 sc->sc_is_console = flags & SSDFB_ATTACH_FLAG_CONSOLE ? true : false;
245 sc->sc_inverse = flags & SSDFB_ATTACH_FLAG_INVERSE ? true : false;
246 sc->sc_upsidedown = flags & SSDFB_ATTACH_FLAG_UPSIDEDOWN ? true : false;
247 sc->sc_backoff = 1;
248 sc->sc_contrast = sc->sc_p->p_default_contrast;
249 sc->sc_gddram_len = sc->sc_p->p_width * sc->sc_p->p_height
250 * sc->sc_p->p_bits_per_pixel / 8;
251 sc->sc_gddram = kmem_alloc(sc->sc_gddram_len, KM_SLEEP);
252 if (sc->sc_gddram == NULL)
253 goto out;
254
255 aprint_normal_dev(sc->sc_dev, "%dx%d%s\n", sc->sc_p->p_width,
256 sc->sc_p->p_height, sc->sc_is_console ? ", console" : "");
257
258 /*
259 * Initialize rasops. The native depth is 1-bit monochrome and we
260 * support this in text emul mode via rasops1. But modern Xorg
261 * userland has many rendering glitches when running with 1-bit depth
262 * so to better support this use case we instead declare ourselves as
263 * an 8-bit display with a two entry constant color map.
264 */
265 error = ssdfb_pick_font(&sc->sc_fontcookie, &sc->sc_font);
266 if (error) {
267 aprint_error_dev(sc->sc_dev, "no font\n");
268 goto out;
269 }
270 #ifdef SSDFB_USE_NATIVE_DEPTH
271 ri->ri_depth = sc->sc_p->p_bits_per_pixel;
272 #else
273 ri->ri_depth = 8;
274 #endif
275 ri->ri_font = sc->sc_font;
276 ri->ri_width = sc->sc_p->p_width;
277 ri->ri_height = sc->sc_p->p_height;
278 ri->ri_stride = ri->ri_width * ri->ri_depth / 8;
279 ri->ri_hw = sc;
280 ri->ri_flg = RI_FULLCLEAR | RI_FORCEMONO;
281 sc->sc_ri_bits_len = round_page(ri->ri_stride * ri->ri_height);
282 ri->ri_bits = (u_char *)uvm_km_alloc(kernel_map, sc->sc_ri_bits_len,
283 0, UVM_KMF_WIRED);
284 if (ri->ri_bits == NULL)
285 goto out;
286
287 error = rasops_init(ri,
288 sc->sc_p->p_height / sc->sc_font->fontheight,
289 sc->sc_p->p_width / sc->sc_font->fontwidth);
290 if (error)
291 goto out;
292
293 ri->ri_caps &= ~WSSCREEN_WSCOLORS;
294
295 /*
296 * Save original emul ops & insert our damage notification hooks.
297 */
298 sc->sc_orig_riops = ri->ri_ops;
299 ri->ri_ops.putchar = ssdfb_putchar;
300 ri->ri_ops.copycols = ssdfb_copycols;
301 ri->ri_ops.erasecols = ssdfb_erasecols;
302 ri->ri_ops.copyrows = ssdfb_copyrows;
303 ri->ri_ops.eraserows = ssdfb_eraserows;
304 ri->ri_ops.cursor = ssdfb_cursor;
305
306 /*
307 * Set up the screen.
308 */
309 sc->sc_screen_descr = (struct wsscreen_descr){
310 .name = "default",
311 .ncols = ri->ri_cols,
312 .nrows = ri->ri_rows,
313 .textops = &ri->ri_ops,
314 .fontwidth = ri->ri_font->fontwidth,
315 .fontheight = ri->ri_font->fontheight,
316 .capabilities = ri->ri_caps
317 };
318 sc->sc_screens[0] = &sc->sc_screen_descr;
319 sc->sc_screenlist = (struct wsscreen_list){
320 .nscreens = 1,
321 .screens = sc->sc_screens
322 };
323
324 /*
325 * Initialize hardware.
326 */
327 error = p->p_init(sc);
328 if (error)
329 goto out;
330
331 if (sc->sc_is_console)
332 ssdfb_set_usepoll(sc, true);
333
334 mutex_init(&sc->sc_cond_mtx, MUTEX_DEFAULT, IPL_SCHED);
335 cv_init(&sc->sc_cond, "ssdfb");
336 kt_flags = KTHREAD_MUSTJOIN;
337 /* XXX spi(4) is not MPSAFE yet. */
338 if (ISSET(flags, SSDFB_ATTACH_FLAG_MPSAFE))
339 kt_flags |= KTHREAD_MPSAFE;
340 error = kthread_create(PRI_SOFTCLOCK, kt_flags, NULL, ssdfb_thread, sc,
341 &sc->sc_thread, "%s", device_xname(sc->sc_dev));
342 if (error) {
343 cv_destroy(&sc->sc_cond);
344 mutex_destroy(&sc->sc_cond_mtx);
345 goto out;
346 }
347
348 /*
349 * Attach wsdisplay.
350 */
351 if (sc->sc_is_console) {
352 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
353 wsdisplay_cnattach(&sc->sc_screen_descr, ri, 0, 0, defattr);
354 #if defined(DDB)
355 db_trap_callback = ssdfb_ddb_trap_callback;
356 #endif
357 }
358 aa = (struct wsemuldisplaydev_attach_args){
359 .console = sc->sc_is_console,
360 .scrdata = &sc->sc_screenlist,
361 .accessops = &ssdfb_accessops,
362 .accesscookie = sc
363 };
364 sc->sc_wsdisplay =
365 config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
366
367 return;
368 out:
369 aprint_error_dev(sc->sc_dev, "attach failed: %d\n", error);
370 if (sc->sc_gddram != NULL)
371 kmem_free(sc->sc_gddram, sc->sc_gddram_len);
372 if (ri->ri_bits != NULL)
373 uvm_km_free(kernel_map, (vaddr_t)ri->ri_bits, sc->sc_ri_bits_len,
374 UVM_KMF_WIRED);
375 if (sc->sc_fontcookie > 0)
376 (void) wsfont_unlock(sc->sc_fontcookie);
377 }
378
379 int
380 ssdfb_detach(struct ssdfb_softc *sc)
381 {
382 mutex_enter(&sc->sc_cond_mtx);
383 sc->sc_detaching = true;
384 cv_broadcast(&sc->sc_cond);
385 mutex_exit(&sc->sc_cond_mtx);
386 kthread_join(sc->sc_thread);
387
388 if (sc->sc_uobj != NULL) {
389 mutex_enter(sc->sc_uobj->vmobjlock);
390 sc->sc_uobj->uo_refs--;
391 mutex_exit(sc->sc_uobj->vmobjlock);
392 }
393 config_detach(sc->sc_wsdisplay, DETACH_FORCE);
394
395 cv_destroy(&sc->sc_cond);
396 mutex_destroy(&sc->sc_cond_mtx);
397 uvm_km_free(kernel_map, (vaddr_t)sc->sc_ri.ri_bits, sc->sc_ri_bits_len,
398 UVM_KMF_WIRED);
399 kmem_free(sc->sc_gddram, sc->sc_gddram_len);
400 (void) wsfont_unlock(sc->sc_fontcookie);
401 return 0;
402 }
403
404 static int
405 ssdfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
406 {
407 struct ssdfb_softc *sc = v;
408 struct wsdisplay_param *wdp;
409 struct wsdisplay_cmap *wc;
410 u_char cmap[16];
411 int cmaplen = 1 << sc->sc_p->p_bits_per_pixel;
412 int i;
413 struct wsdisplayio_fbinfo *fbi;
414 int error;
415
416 switch (cmd) {
417 case WSDISPLAYIO_GTYPE:
418 *(u_int *)data = WSDISPLAY_TYPE_SSDFB;
419 return 0;
420 case WSDISPLAYIO_GINFO:
421 *(struct wsdisplay_fbinfo *)data = (struct wsdisplay_fbinfo){
422 .width = sc->sc_ri.ri_width,
423 .height = sc->sc_ri.ri_height,
424 .depth = sc->sc_ri.ri_depth,
425 .cmsize = cmaplen
426 };
427 return 0;
428 case WSDISPLAYIO_GET_FBINFO:
429 fbi = (struct wsdisplayio_fbinfo *)data;
430 error = wsdisplayio_get_fbinfo(&sc->sc_ri, fbi);
431 fbi->fbi_subtype.fbi_cmapinfo.cmap_entries = cmaplen;
432 /* fbi->fbi_pixeltype = WSFB_GREYSCALE */;
433 return error;
434 case WSDISPLAYIO_LINEBYTES:
435 *(u_int *)data = sc->sc_ri.ri_stride;
436 return 0;
437 case WSDISPLAYIO_GETPARAM:
438 wdp = (struct wsdisplay_param *)data;
439 if (wdp->param != WSDISPLAYIO_PARAM_CONTRAST)
440 return EINVAL;
441 wdp->min = 0;
442 wdp->max = 0xff;
443 wdp->curval = sc->sc_contrast;
444 return 0;
445 case WSDISPLAYIO_SETPARAM:
446 wdp = (struct wsdisplay_param *)data;
447 if (wdp->param != WSDISPLAYIO_PARAM_CONTRAST)
448 return EINVAL;
449 if (wdp->curval < 0 || wdp->curval > 0xff)
450 return EINVAL;
451 return ssdfb_set_contrast(sc, wdp->curval, sc->sc_usepoll);
452 case WSDISPLAYIO_GMODE:
453 *(u_int *)data = sc->sc_mode;
454 return 0;
455 case WSDISPLAYIO_SMODE:
456 return ssdfb_set_mode(sc, *(u_int *)data);
457 case WSDISPLAYIO_GVIDEO:
458 *(u_int *)data = sc->sc_display_on
459 ? WSDISPLAYIO_VIDEO_ON
460 : WSDISPLAYIO_VIDEO_OFF;
461 return 0;
462 case WSDISPLAYIO_SVIDEO:
463 switch (*(u_int *)data) {
464 case WSDISPLAYIO_VIDEO_ON:
465 case WSDISPLAYIO_VIDEO_OFF:
466 break;
467 default:
468 return EINVAL;
469 }
470 return ssdfb_set_display_on(sc,
471 *(u_int *)data == WSDISPLAYIO_VIDEO_ON ? true : false,
472 sc->sc_usepoll);
473 #if 0 /* don't let userland mess with polling yet */
474 case WSDISPLAYIO_SET_POLLING:
475 switch (*(u_int *)data) {
476 case 0:
477 case 1:
478 break;
479 default:
480 return EINVAL;
481 }
482 mutex_enter(&sc->sc_cond_mtx);
483 ssdfb_set_usepoll(sc, *(u_int *)data ? true : false);
484 cv_broadcast(&sc->sc_cond);
485 mutex_exit(&sc->sc_cond_mtx);
486 return 0;
487 #endif
488 case WSDISPLAYIO_GETCMAP:
489 wc = (struct wsdisplay_cmap *)data;
490 if (wc->index >= cmaplen ||
491 wc->count > cmaplen - wc->index)
492 return EINVAL;
493 for(i = 0; i < cmaplen; i++) {
494 cmap[i] = 255 * i / (cmaplen - 1);
495 }
496 error = copyout(&cmap[wc->index], wc->red, wc->count);
497 if (error)
498 return error;
499 error = copyout(&cmap[wc->index], wc->green, wc->count);
500 if (error)
501 return error;
502 error = copyout(&cmap[wc->index], wc->blue, wc->count);
503 return error;
504 case WSDISPLAYIO_PUTCMAP:
505 return ENODEV;
506 }
507
508 return EPASSTHROUGH;
509 }
510
511 static paddr_t
512 ssdfb_mmap(void *v, void *vs, off_t off, int prot)
513 {
514 struct ssdfb_softc *sc = (struct ssdfb_softc *)v;
515 struct rasops_info *ri = &sc->sc_ri;
516 vaddr_t va_base = (vaddr_t)ri->ri_bits;
517 paddr_t pa;
518
519 if (off < 0 || off >= sc->sc_ri_bits_len || (off & PAGE_MASK) != 0)
520 return -1;
521
522 if (!pmap_extract(pmap_kernel(), va_base + off, &pa))
523 return -1;
524
525 return atop(pa);
526 }
527
528 static int
529 ssdfb_alloc_screen(void *v, const struct wsscreen_descr *descr, void **cookiep,
530 int *curxp, int *curyp, long *attrp)
531 {
532 struct ssdfb_softc *sc = v;
533 struct rasops_info *ri = &sc->sc_ri;
534
535 if (sc->sc_nscreens > 0)
536 return ENOMEM;
537
538 ri->ri_ops.allocattr(ri, 0, 0, 0, attrp);
539 *cookiep = &sc->sc_ri;
540 *curxp = 0;
541 *curyp = 0;
542 sc->sc_nscreens++;
543
544 return 0;
545 }
546
547 static void
548 ssdfb_free_screen(void *v, void *cookie)
549 {
550 struct ssdfb_softc *sc = v;
551
552 if (sc->sc_is_console)
553 panic("ssdfb_free_screen: is console");
554
555 sc->sc_nscreens--;
556 }
557
558 static int
559 ssdfb_show_screen(void *v, void *cookie, int waitok,
560 void (*cb) (void *, int, int), void *cb_arg)
561 {
562 return 0;
563 }
564
565 static void
566 ssdfb_putchar(void *cookie, int row, int col, u_int c, long attr)
567 {
568 struct rasops_info *ri = (struct rasops_info *)cookie;
569 struct ssdfb_softc *sc = ri->ri_hw;
570
571 sc->sc_orig_riops.putchar(cookie, row, col, c, attr);
572 ssdfb_damage(sc);
573 }
574
575 static void
576 ssdfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
577 {
578 struct rasops_info *ri = (struct rasops_info *)cookie;
579 struct ssdfb_softc *sc = ri->ri_hw;
580
581 sc->sc_orig_riops.copycols(cookie, row, srccol, dstcol, ncols);
582 ssdfb_damage(sc);
583 }
584
585 static void
586 ssdfb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
587 {
588 struct rasops_info *ri = (struct rasops_info *)cookie;
589 struct ssdfb_softc *sc = ri->ri_hw;
590
591 sc->sc_orig_riops.erasecols(cookie, row, startcol, ncols, fillattr);
592 ssdfb_damage(sc);
593 }
594
595 static void
596 ssdfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
597 {
598 struct rasops_info *ri = (struct rasops_info *)cookie;
599 struct ssdfb_softc *sc = ri->ri_hw;
600
601 sc->sc_orig_riops.copyrows(cookie, srcrow, dstrow, nrows);
602 ssdfb_damage(sc);
603 }
604
605 static void
606 ssdfb_eraserows(void *cookie, int row, int nrows, long fillattr)
607 {
608 struct rasops_info *ri = (struct rasops_info *)cookie;
609 struct ssdfb_softc *sc = ri->ri_hw;
610
611 sc->sc_orig_riops.eraserows(cookie, row, nrows, fillattr);
612 ssdfb_damage(sc);
613 }
614
615 static void
616 ssdfb_cursor(void *cookie, int on, int row, int col)
617 {
618 struct rasops_info *ri = (struct rasops_info *)cookie;
619 struct ssdfb_softc *sc = ri->ri_hw;
620
621 sc->sc_orig_riops.cursor(cookie, on, row, col);
622 ssdfb_damage(sc);
623 }
624
625 static int
626 ssdfb_init_ssd1306(struct ssdfb_softc *sc)
627 {
628 int error;
629 uint8_t cmd[2];
630 bool usepoll = true;
631
632 /*
633 * Enter sleep.
634 */
635 SSDFB_CMD1(SSDFB_CMD_SET_DISPLAY_OFF);
636 if (error)
637 return error;
638 SSDFB_CMD1(SSDFB_CMD_DEACTIVATE_SCROLL);
639 if (error)
640 return error;
641 SSDFB_CMD1(SSDFB_CMD_ENTIRE_DISPLAY_OFF);
642 if (error)
643 return error;
644
645 /*
646 * Configure physical display panel layout.
647 */
648 SSDFB_CMD2(SSDFB_CMD_SET_MULTIPLEX_RATIO, sc->sc_p->p_multiplex_ratio);
649 if (error)
650 return error;
651 SSDFB_CMD2(SSDFB_CMD_SET_DISPLAY_OFFSET, 0);
652 if (error)
653 return error;
654 SSDFB_CMD1(SSDFB_CMD_SET_DISPLAY_START_LINE_BASE + 0x00);
655 if (error)
656 return error;
657 SSDFB_CMD2(SSDFB_CMD_SET_COM_PINS_HARDWARE_CFG, sc->sc_p->p_compin_cfg);
658 if (error)
659 return error;
660 if (sc->sc_upsidedown) {
661 SSDFB_CMD1(SSDFB_CMD_SET_SEGMENT_REMAP_REVERSE);
662 if (error)
663 return error;
664 SSDFB_CMD1(SSDFB_CMD_SET_COM_OUTPUT_DIRECTION_REMAP);
665 if (error)
666 return error;
667 } else {
668 SSDFB_CMD1(SSDFB_CMD_SET_SEGMENT_REMAP_NORMAL);
669 if (error)
670 return error;
671 SSDFB_CMD1(SSDFB_CMD_SET_COM_OUTPUT_DIRECTION_NORMAL);
672 if (error)
673 return error;
674 }
675 SSDFB_CMD1(SSDFB_CMD_SET_NORMAL_DISPLAY + (uint8_t)sc->sc_inverse);
676 if (error)
677 return error;
678
679 /*
680 * Configure timing characteristics.
681 */
682 SSDFB_CMD2(SSDFB_CMD_SET_DISPLAY_CLOCK_RATIO,
683 __SHIFTIN(sc->sc_p->p_fosc, SSDFB_DISPLAY_CLOCK_OSCILLATOR_MASK) |
684 __SHIFTIN(sc->sc_p->p_fosc_div, SSDFB_DISPLAY_CLOCK_DIVIDER_MASK));
685 if (error)
686 return error;
687 SSDFB_CMD2(SSDFB_CMD_SET_CONTRAST_CONTROL, sc->sc_contrast);
688 if (error)
689 return error;
690 SSDFB_CMD2(SSDFB_CMD_SET_PRECHARGE_PERIOD,
691 __SHIFTIN(sc->sc_p->p_precharge, SSDFB_PRECHARGE_MASK) |
692 __SHIFTIN(sc->sc_p->p_discharge, SSDFB_DISCHARGE_MASK));
693 if (error)
694 return error;
695 SSDFB_CMD2(SSDFB_CMD_SET_VCOMH_DESELECT_LEVEL,
696 sc->sc_p->p_vcomh_deselect_level);
697 if (error)
698 return error;
699
700 /*
701 * Start charge pumps.
702 */
703 if (sc->sc_p->p_controller_id == SSDFB_CONTROLLER_SH1106) {
704 SSDFB_CMD1(SH1106_CMD_SET_CHARGE_PUMP_7V4);
705 if (error)
706 return error;
707 SSDFB_CMD2(SH1106_CMD_SET_DC_DC, SH1106_DC_DC_ON);
708 if (error)
709 return error;
710 } else {
711 SSDFB_CMD2(SSD1306_CMD_SET_CHARGE_PUMP,
712 SSD1306_CHARGE_PUMP_ENABLE);
713 if (error)
714 return error;
715 }
716
717 ssdfb_clear_screen(sc);
718 error = sc->sc_p->p_sync(sc, usepoll);
719 if (error)
720 return error;
721 error = ssdfb_set_display_on(sc, true, usepoll);
722
723 return error;
724 }
725
726 static int
727 ssdfb_init_ssd1322(struct ssdfb_softc *sc)
728 {
729 int error;
730 uint8_t cmd[3];
731 bool usepoll = true;
732 uint8_t remap;
733 uint8_t dualcom;
734
735 /*
736 * Enter sleep.
737 */
738 SSDFB_CMD2(SSD1322_CMD_SET_COMMAND_LOCK, SSD1322_COMMAND_UNLOCK_MAGIC);
739 if (error)
740 return error;
741 SSDFB_CMD1(SSD1322_CMD_SET_SLEEP_MODE_ON);
742 if (error)
743 return error;
744
745 /*
746 * Start charge pumps.
747 */
748 SSDFB_CMD2(SSD1322_CMD_FUNCTION_SELECTION,
749 SSD1322_FUNCTION_SELECTION_INTERNAL_VDD);
750 if (error)
751 return error;
752 SSDFB_CMD2(SSD1322_CMD_SET_VCOMH, sc->sc_p->p_vcomh_deselect_level);
753 if (error)
754 return error;
755 SSDFB_CMD2(SSD1322_CMD_SET_PRE_CHARGE_VOLTAGE_LEVEL,
756 SSD1322_DEFAULT_PRE_CHARGE_VOLTAGE_LEVEL);
757 if (error)
758 return error;
759 SSDFB_CMD2(SSD1322_CMD_SET_GPIO,
760 SSD1322_GPIO0_DISABLED | SSD1322_GPIO1_DISABLED);
761 if (error)
762 return error;
763
764 /*
765 * Configure timing characteristics.
766 */
767 SSDFB_CMD2(SSD1322_CMD_SET_FRONT_CLOCK_DIVIDER,
768 __SHIFTIN(sc->sc_p->p_fosc, SSD1322_FREQUENCY_MASK) |
769 __SHIFTIN(sc->sc_p->p_fosc_div, SSD1322_DIVIDER_MASK));
770 if (error)
771 return error;
772 SSDFB_CMD2(SSD1322_CMD_SET_PHASE_LENGTH,
773 __SHIFTIN(SSD1322_DEFAULT_PHASE_2,
774 SSD1322_PHASE_LENGTH_PHASE_2_MASK) |
775 __SHIFTIN(SSD1322_DEFAULT_PHASE_1,
776 SSD1322_PHASE_LENGTH_PHASE_1_MASK));
777 if (error)
778 return error;
779 SSDFB_CMD2(SSD1322_CMD_SET_SECOND_PRECHARGE_PERIOD,
780 SSD1322_DEFAULT_SECOND_PRECHARGE);
781 if (error)
782 return error;
783
784 /*
785 * Configure physical display panel layout.
786 */
787 SSDFB_CMD2(SSD1322_CMD_SET_MUX_RATIO, sc->sc_p->p_multiplex_ratio);
788 if (error)
789 return error;
790 if (sc->sc_upsidedown)
791 remap = 0x10;
792 else
793 remap = 0x2;
794 dualcom = 0x1;
795 if (sc->sc_p->p_multiplex_ratio <= 63)
796 dualcom |= 0x10;
797 SSDFB_CMD3(SSD1322_CMD_SET_REMAP_AND_DUAL_COM_LINE_MODE, remap, dualcom);
798 if (error)
799 return error;
800
801 /*
802 * Contrast settings.
803 */
804 SSDFB_CMD1(SSD1322_CMD_SET_DEFAULT_GRAY_SCALE_TABLE);
805 if (error)
806 return error;
807 SSDFB_CMD3(SSD1322_CMD_DISPLAY_ENHANCEMENT_A,
808 SSD1322_DISPLAY_ENHANCEMENT_A_MAGIC1,
809 SSD1322_DISPLAY_ENHANCEMENT_A_MAGIC2);
810 if (error)
811 return error;
812 SSDFB_CMD3(SSD1322_CMD_DISPLAY_ENHANCEMENT_B,
813 SSD1322_DISPLAY_ENHANCEMENT_B_MAGIC1,
814 SSD1322_DISPLAY_ENHANCEMENT_B_MAGIC2);
815 if (error)
816 return error;
817 SSDFB_CMD2(SSD1322_CMD_SET_CONTRAST_CURRENT,
818 sc->sc_contrast);
819 if (error)
820 return error;
821 SSDFB_CMD2(SSD1322_CMD_MASTER_CONTRAST_CURRENT_CONTROL,
822 SSD1322_DEFAULT_MASTER_CONTRAST_CURRENT_CONTROL);
823 if (error)
824 return error;
825
826 /*
827 * Reset display engine state.
828 */
829 SSDFB_CMD2(SSD1322_CMD_SET_DISPLAY_OFFSET, 0x00);
830 if (error)
831 return error;
832 SSDFB_CMD2(SSD1322_CMD_SET_DISPLAY_START_LINE, 0x00);
833 if (error)
834 return error;
835 SSDFB_CMD1(SSD1322_CMD_NORMAL_DISPLAY + (uint8_t)sc->sc_inverse);
836 if (error)
837 return error;
838 SSDFB_CMD1(SSD1322_CMD_EXIT_PARTIAL_DISPLAY);
839 if (error)
840 return error;
841
842 ssdfb_clear_screen(sc);
843 error = ssdfb_sync(sc, usepoll);
844 if (error)
845 return error;
846
847 error = ssdfb_set_display_on(sc, true, usepoll);
848
849 return error;
850 }
851
852 static int
853 ssdfb_set_contrast(struct ssdfb_softc *sc, uint8_t value, bool usepoll)
854 {
855 uint8_t cmd[2];
856
857 switch (sc->sc_p->p_controller_id) {
858 case SSDFB_CONTROLLER_SSD1322:
859 cmd[0] = SSD1322_CMD_SET_CONTRAST_CURRENT;
860 break;
861 default:
862 cmd[0] = SSDFB_CMD_SET_CONTRAST_CONTROL;
863 }
864 cmd[1] = sc->sc_contrast = value;
865
866 return sc->sc_cmd(sc->sc_cookie, cmd, sizeof(cmd), usepoll);
867 }
868
869 static int
870 ssdfb_set_display_on(struct ssdfb_softc *sc, bool value, bool usepoll)
871 {
872 uint8_t cmd[1];
873 int error;
874 sc->sc_display_on = value;
875
876 SSDFB_CMD1(value ? SSDFB_CMD_SET_DISPLAY_ON : SSDFB_CMD_SET_DISPLAY_OFF);
877
878 return error;
879 }
880
881 static int
882 ssdfb_set_mode(struct ssdfb_softc *sc, u_int mode)
883 {
884 switch (mode) {
885 case WSDISPLAYIO_MODE_EMUL:
886 case WSDISPLAYIO_MODE_DUMBFB:
887 break;
888 default:
889 return EINVAL;
890 }
891 if (mode == sc->sc_mode)
892 return 0;
893 mutex_enter(&sc->sc_cond_mtx);
894 sc->sc_mode = mode;
895 cv_broadcast(&sc->sc_cond);
896 mutex_exit(&sc->sc_cond_mtx);
897 ssdfb_clear_screen(sc);
898 ssdfb_damage(sc);
899
900 return 0;
901 }
902
903 static void
904 ssdfb_damage(struct ssdfb_softc *sc)
905 {
906 int s;
907
908 if (sc->sc_usepoll) {
909 (void) ssdfb_sync(sc, true);
910 } else {
911 /*
912 * kernel code isn't permitted to call us via kprintf at
913 * splhigh. In case misbehaving code calls us anyway we can't
914 * safely take the mutex so we skip the damage notification.
915 */
916 if (sc->sc_is_console) {
917 s = splhigh();
918 splx(s);
919 if (s == IPL_HIGH)
920 return;
921 }
922 mutex_enter(&sc->sc_cond_mtx);
923 sc->sc_modified = true;
924 cv_broadcast(&sc->sc_cond);
925 mutex_exit(&sc->sc_cond_mtx);
926 }
927 }
928
929 static void
930 ssdfb_udv_attach(struct ssdfb_softc *sc)
931 {
932 extern const struct cdevsw wsdisplay_cdevsw;
933 dev_t dev;
934 #define WSDISPLAYMINOR(unit, screen) (((unit) << 8) | (screen))
935 dev = makedev(cdevsw_lookup_major(&wsdisplay_cdevsw),
936 WSDISPLAYMINOR(device_unit(sc->sc_wsdisplay), 0));
937 sc->sc_uobj = udv_attach(dev, VM_PROT_READ|VM_PROT_WRITE, 0,
938 sc->sc_ri_bits_len);
939 }
940
941 static bool
942 ssdfb_is_modified(struct ssdfb_softc *sc)
943 {
944 vaddr_t va, va_end;
945
946 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)
947 return sc->sc_modified;
948
949 if (sc->sc_uobj == NULL)
950 return false;
951
952 va = (vaddr_t)sc->sc_ri.ri_bits;
953 va_end = va + sc->sc_ri_bits_len;
954 while (va < va_end) {
955 if (pmap_is_modified(uvm_pageratop(va)))
956 return true;
957 va += PAGE_SIZE;
958 }
959
960 return false;
961 }
962
963 static bool
964 ssdfb_clear_modify(struct ssdfb_softc *sc)
965 {
966 vaddr_t va, va_end;
967 bool ret;
968
969 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
970 mutex_enter(&sc->sc_cond_mtx);
971 ret = sc->sc_modified;
972 sc->sc_modified = false;
973 mutex_exit(&sc->sc_cond_mtx);
974 return ret;
975 }
976
977 if (sc->sc_uobj == NULL)
978 return false;
979
980 va = (vaddr_t)sc->sc_ri.ri_bits;
981 va_end = va + sc->sc_ri_bits_len;
982 ret = false;
983 while (va < va_end) {
984 if (pmap_clear_modify(uvm_pageratop(va)))
985 ret = true;
986 va += PAGE_SIZE;
987 }
988
989 return ret;
990 }
991
992 static void
993 ssdfb_thread(void *arg)
994 {
995 struct ssdfb_softc *sc = (struct ssdfb_softc *)arg;
996 int error;
997
998 mutex_enter(&sc->sc_cond_mtx);
999
1000 if (sc->sc_usepoll)
1001 ssdfb_set_usepoll(sc, false);
1002
1003 while(!sc->sc_detaching) {
1004 if (sc->sc_mode == WSDISPLAYIO_MODE_DUMBFB &&
1005 sc->sc_uobj == NULL) {
1006 mutex_exit(&sc->sc_cond_mtx);
1007 ssdfb_udv_attach(sc);
1008 mutex_enter(&sc->sc_cond_mtx);
1009 }
1010 if (!ssdfb_is_modified(sc)) {
1011 if (cv_timedwait(&sc->sc_cond, &sc->sc_cond_mtx,
1012 sc->sc_mode == WSDISPLAYIO_MODE_EMUL
1013 ? 0 : sc->sc_backoff) == EWOULDBLOCK
1014 && sc->sc_backoff < mstohz(200)) {
1015 sc->sc_backoff <<= 1;
1016 }
1017 continue;
1018 }
1019 sc->sc_backoff = 1;
1020 mutex_exit(&sc->sc_cond_mtx);
1021 (void) ssdfb_clear_modify(sc);
1022 if (!sc->sc_usepoll) {
1023 error = ssdfb_sync(sc, false);
1024 if (error)
1025 device_printf(sc->sc_dev,
1026 "ssdfb_sync: error %d\n",
1027 error);
1028 }
1029 mutex_enter(&sc->sc_cond_mtx);
1030 }
1031
1032 mutex_exit(&sc->sc_cond_mtx);
1033 kthread_exit(0);
1034 }
1035
1036 static void
1037 ssdfb_set_usepoll(struct ssdfb_softc *sc, bool enable)
1038 {
1039 sc->sc_usepoll = enable;
1040 }
1041
1042 static int
1043 ssdfb_sync(struct ssdfb_softc *sc, bool usepoll)
1044 {
1045 return sc->sc_p->p_sync(sc, usepoll);
1046 }
1047
1048 static int
1049 ssdfb_sync_ssd1306(struct ssdfb_softc *sc, bool usepoll)
1050 {
1051 struct rasops_info *ri = &sc->sc_ri;
1052 int block_size = 8;
1053 int ri_block_stride = ri->ri_stride * block_size;
1054 int height_in_blocks = sc->sc_p->p_height / block_size;
1055 int width_in_blocks = sc->sc_p->p_width / block_size;
1056 int ri_block_step = block_size * ri->ri_depth / 8;
1057 int x, y;
1058 union ssdfb_block *blockp;
1059 uint64_t raw_block;
1060 uint8_t *src;
1061 int x1, x2, y1, y2;
1062
1063 /*
1064 * Transfer rasops bitmap into gddram shadow buffer while keeping track
1065 * of the bounding box of the dirty region we scribbled over.
1066 */
1067 x1 = width_in_blocks;
1068 x2 = -1;
1069 y1 = height_in_blocks;
1070 y2 = -1;
1071 for (y = 0; y < height_in_blocks; y++) {
1072 src = &ri->ri_bits[y * ri_block_stride];
1073 blockp = &sc->sc_gddram[y * width_in_blocks];
1074 for (x = 0; x < width_in_blocks; x++) {
1075 raw_block = ssdfb_transpose_block(src, ri->ri_stride);
1076 if (raw_block != blockp->raw) {
1077 blockp->raw = raw_block;
1078 if (x1 > x)
1079 x1 = x;
1080 if (x2 < x)
1081 x2 = x;
1082 if (y1 > y)
1083 y1 = y;
1084 if (y2 < y)
1085 y2 = y;
1086 }
1087 src += ri_block_step;
1088 blockp++;
1089 }
1090 }
1091 if (x2 != -1)
1092 return sc->sc_transfer_rect(sc->sc_cookie,
1093 x1 * block_size + sc->sc_p->p_panel_shift,
1094 (x2 + 1) * block_size - 1 + sc->sc_p->p_panel_shift,
1095 y1,
1096 y2,
1097 &sc->sc_gddram[y1 * width_in_blocks + x1].col[0],
1098 sc->sc_p->p_width,
1099 usepoll);
1100
1101 return 0;
1102 }
1103
1104 static int
1105 ssdfb_sync_ssd1322(struct ssdfb_softc *sc, bool usepoll)
1106 {
1107 struct rasops_info *ri = &sc->sc_ri;
1108 int block_size_w = 4;
1109 int width = sc->sc_p->p_width;
1110 int height = sc->sc_p->p_height;
1111 int width_in_blocks = width / block_size_w;
1112 int x, y;
1113 uint16_t *blockp;
1114 uint16_t raw_block;
1115 uint16_t *src;
1116 uint32_t *src32;
1117 int x1, x2, y1, y2;
1118
1119 /*
1120 * Transfer rasops bitmap into gddram shadow buffer while keeping track
1121 * of the bounding box of the dirty region we scribbled over.
1122 */
1123 x1 = sc->sc_p->p_width;
1124 x2 = -1;
1125 y1 = sc->sc_p->p_height;
1126 y2 = -1;
1127 blockp = (uint16_t*)sc->sc_gddram;
1128 for (y = 0; y < height; y++) {
1129 src = (uint16_t*)&ri->ri_bits[y * ri->ri_stride];
1130 src32 = (uint32_t*)src;
1131 for (x = 0; x < width_in_blocks; x++) {
1132 #if _BYTE_ORDER == _LITTLE_ENDIAN
1133 # ifdef SSDFB_USE_NATIVE_DEPTH
1134 raw_block =
1135 ((*src << 12) & 0xf000) |
1136 ((*src << 4) & 0x0f00) |
1137 ((*src >> 4) & 0x00f0) |
1138 ((*src >> 12) & 0x000f);
1139 src++;
1140 # else
1141 raw_block =
1142 ((*src32 << 8) & 0x0f00) |
1143 ((*src32 << 4) & 0xf000) |
1144 ((*src32 >> 16) & 0x000f) |
1145 ((*src32 >> 20) & 0x00f0);
1146 # endif
1147 src32++;
1148 #else
1149 # error please add big endian host support here
1150 #endif
1151 if (raw_block != *blockp) {
1152 *blockp = raw_block;
1153 if (x1 > x)
1154 x1 = x;
1155 if (x2 < x)
1156 x2 = x;
1157 if (y1 > y)
1158 y1 = y;
1159 if (y2 < y)
1160 y2 = y;
1161 }
1162 blockp++;
1163 }
1164 }
1165
1166 blockp = (uint16_t*)sc->sc_gddram;
1167 if (x2 != -1)
1168 return sc->sc_transfer_rect(sc->sc_cookie,
1169 x1 + sc->sc_p->p_panel_shift,
1170 x2 + sc->sc_p->p_panel_shift,
1171 y1,
1172 y2,
1173 (uint8_t*)&blockp[y1 * width_in_blocks + x1],
1174 width * sc->sc_p->p_bits_per_pixel / 8,
1175 usepoll);
1176 return 0;
1177 }
1178
1179 static uint64_t
1180 ssdfb_transpose_block(uint8_t *src, size_t src_stride)
1181 {
1182 uint64_t x = 0;
1183 #ifdef SSDFB_USE_NATIVE_DEPTH
1184 uint64_t t;
1185 int i;
1186
1187 /*
1188 * collect the 8x8 block.
1189 */
1190 for (i = 0; i < 8; i++) {
1191 x >>= 8;
1192 x |= (uint64_t)src[i * src_stride] << 56;
1193 }
1194
1195 /*
1196 * Transpose it into gddram layout.
1197 * Post-transpose bswap is the same as pre-transpose bit order reversal.
1198 * We do this to match rasops1 bit order.
1199 */
1200 t = (x ^ (x >> 28)) & 0x00000000F0F0F0F0ULL;
1201 x = x ^ t ^ (t << 28);
1202 t = (x ^ (x >> 14)) & 0x0000CCCC0000CCCCULL;
1203 x = x ^ t ^ (t << 14);
1204 t = (x ^ (x >> 7)) & 0x00AA00AA00AA00AAULL;
1205 x = x ^ t ^ (t << 7);
1206 x = bswap64(x);
1207 #else
1208 int m, n;
1209
1210 for (m = 0; m < 8; m++) {
1211 for (n = 0; n < 8; n++) {
1212 x >>= 1;
1213 x |= src[n * src_stride + m] ? (1ULL << 63) : 0;
1214 }
1215 }
1216 #endif
1217 return htole64(x);
1218 }
1219
1220 static const struct ssdfb_product *
1221 ssdfb_lookup_product(ssdfb_product_id_t id)
1222 {
1223 int i;
1224
1225 for (i = 0; i < __arraycount(ssdfb_products); i++) {
1226 if (ssdfb_products[i].p_product_id == id)
1227 return &ssdfb_products[i];
1228 }
1229
1230 return NULL;
1231 }
1232
1233 static int
1234 ssdfb_pick_font(int *cookiep, struct wsdisplay_font **fontp)
1235 {
1236 int error;
1237 int c;
1238 struct wsdisplay_font *f;
1239 int i;
1240 uint8_t d[4][2] = {{5, 8}, {8, 8}, {8, 10} ,{8, 16}};
1241
1242 /*
1243 * Try to find fonts in order of increasing size.
1244 */
1245 wsfont_init();
1246 for(i = 0; i < __arraycount(d); i++) {
1247 c = wsfont_find(NULL, d[i][0], d[i][1], 0,
1248 WSDISPLAY_FONTORDER_L2R, WSDISPLAY_FONTORDER_L2R,
1249 WSFONT_FIND_BITMAP);
1250 if (c > 0)
1251 break;
1252 }
1253 if (c <= 0)
1254 return ENOENT;
1255 error = wsfont_lock(c, &f);
1256 if (error)
1257 return error;
1258 *cookiep = c;
1259 *fontp = f;
1260
1261 return 0;
1262 }
1263
1264 static void
1265 ssdfb_clear_screen(struct ssdfb_softc *sc)
1266 {
1267 struct rasops_info *ri = &sc->sc_ri;
1268
1269 memset(sc->sc_gddram, 0xff, sc->sc_gddram_len);
1270 memset(ri->ri_bits, 0, sc->sc_ri_bits_len);
1271 }
1272
1273 #if defined(DDB)
1274 static void
1275 ssdfb_ddb_trap_callback(int enable)
1276 {
1277 extern struct cfdriver ssdfb_cd;
1278 struct ssdfb_softc *sc;
1279 int i;
1280
1281 for (i = 0; i < ssdfb_cd.cd_ndevs; i++) {
1282 sc = device_lookup_private(&ssdfb_cd, i);
1283 if (sc != NULL && sc->sc_is_console) {
1284 ssdfb_set_usepoll(sc, (bool)enable);
1285 }
1286 }
1287 }
1288 #endif
1289