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