ssdfb.c revision 1.2 1 /* $NetBSD: ssdfb.c,v 1.2 2019/03/17 01:33:02 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.2 2019/03/17 01:33:02 tnn Exp $");
34
35 #include "opt_ddb.h"
36
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <uvm/uvm_page.h>
40 #include <sys/condvar.h>
41 #include <sys/kmem.h>
42 #include <sys/kthread.h>
43 #include <dev/wscons/wsdisplayvar.h>
44 #include <dev/rasops/rasops.h>
45 #include <dev/ic/ssdfbvar.h>
46
47 #if defined(DDB)
48 #include <machine/db_machdep.h>
49 #include <ddb/db_extern.h>
50 #endif
51
52 /* userland interface */
53 static int ssdfb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
54 static paddr_t ssdfb_mmap(void *, void *, off_t, int);
55
56 /* wscons screen management */
57 static int ssdfb_alloc_screen(void *, const struct wsscreen_descr *,
58 void **, int *, int *, long *);
59 static void ssdfb_free_screen(void *, void *);
60 static int ssdfb_show_screen(void *, void *, int,
61 void (*cb) (void *, int, int), void *);
62
63 /* rasops hooks */
64 static void ssdfb_putchar(void *, int, int, u_int, long);
65 static void ssdfb_copycols(void *, int, int, int, int);
66 static void ssdfb_erasecols(void *, int, int, int, long);
67 static void ssdfb_copyrows(void *, int, int, int);
68 static void ssdfb_eraserows(void *, int, int, long);
69 static void ssdfb_cursor(void *, int, int, int);
70
71 /* hardware interface */
72 static int ssdfb_init(struct ssdfb_softc *);
73 static int ssdfb_set_contrast(struct ssdfb_softc *, uint8_t, bool);
74 static int ssdfb_set_display_on(struct ssdfb_softc *, bool, bool);
75 static int ssdfb_set_mode(struct ssdfb_softc *, u_int);
76
77 /* frame buffer damage tracking and synchronization */
78 static bool ssdfb_is_modified(struct ssdfb_softc *sc);
79 static bool ssdfb_clear_modify(struct ssdfb_softc *sc);
80 static void ssdfb_damage(struct ssdfb_softc *);
81 static void ssdfb_thread(void *);
82 static void ssdfb_set_usepoll(struct ssdfb_softc *, bool);
83 static int ssdfb_sync(struct ssdfb_softc *, bool);
84 static uint64_t ssdfb_transpose_block_1bpp(uint8_t *, size_t);
85 static uint64_t ssdfb_transpose_block_8bpp(uint8_t *, size_t);
86
87 /* misc helpers */
88 static const struct ssdfb_product *
89 ssdfb_lookup_product(ssdfb_product_id_t);
90 static int ssdfb_pick_font(int *, struct wsdisplay_font **);
91 static void ssdfb_clear_screen(struct ssdfb_softc *);
92 #if defined(DDB)
93 static void ssdfb_ddb_trap_callback(int);
94 #endif
95
96 static const char *ssdfb_controller_names[] = {
97 [SSDFB_CONTROLLER_UNKNOWN] = "unknown",
98 [SSDFB_CONTROLLER_SSD1306] = "Solomon Systech SSD1306",
99 [SSDFB_CONTROLLER_SH1106] = "Sino Wealth SH1106"
100 };
101
102 /*
103 * Display module assemblies supported by this driver.
104 */
105 static const struct ssdfb_product ssdfb_products[] = {
106 {
107 .p_product_id = SSDFB_PRODUCT_SSD1306_GENERIC,
108 .p_controller_id = SSDFB_CONTROLLER_SSD1306,
109 .p_name = "generic",
110 .p_width = 128,
111 .p_height = 64,
112 .p_panel_shift = 0,
113 .p_fosc = 0x8,
114 .p_fosc_div = 0,
115 .p_precharge = 0x1,
116 .p_discharge = 0xf,
117 .p_compin_cfg = SSDFB_COM_PINS_A1_MASK
118 | SSDFB_COM_PINS_ALTERNATIVE_MASK,
119 .p_vcomh_deselect_level = SSD1306_VCOMH_DESELECT_LEVEL_0_77_VCC,
120 .p_default_contrast = 0x7f,
121 .p_multiplex_ratio = 0x3f,
122 .p_chargepump_cmd = SSD1306_CMD_SET_CHARGE_PUMP,
123 .p_chargepump_arg = SSD1306_CHARGE_PUMP_ENABLE
124 },
125 {
126 .p_product_id = SSDFB_PRODUCT_SH1106_GENERIC,
127 .p_controller_id = SSDFB_CONTROLLER_SH1106,
128 .p_name = "generic",
129 .p_width = 128,
130 .p_height = 64,
131 .p_panel_shift = 2,
132 .p_fosc = 0x5,
133 .p_fosc_div = 0,
134 .p_precharge = 0x2,
135 .p_discharge = 0x2,
136 .p_compin_cfg = SSDFB_COM_PINS_A1_MASK
137 | SSDFB_COM_PINS_ALTERNATIVE_MASK,
138 .p_vcomh_deselect_level = SH1106_VCOMH_DESELECT_LEVEL_DEFAULT,
139 .p_default_contrast = 0x80,
140 .p_multiplex_ratio = 0x3f,
141 .p_chargepump_cmd = SH1106_CMD_SET_CHARGE_PUMP_7V4,
142 .p_chargepump_arg = SSDFB_CMD_NOP
143 },
144 {
145 .p_product_id = SSDFB_PRODUCT_ADAFRUIT_938,
146 .p_controller_id = SSDFB_CONTROLLER_SSD1306,
147 .p_name = "Adafruit Industries, LLC product 938",
148 .p_width = 128,
149 .p_height = 64,
150 .p_panel_shift = 0,
151 .p_fosc = 0x8,
152 .p_fosc_div = 0,
153 .p_precharge = 0x1,
154 .p_discharge = 0xf,
155 .p_compin_cfg = 0x12,
156 .p_vcomh_deselect_level = 0x40,
157 .p_default_contrast = 0x8f,
158 .p_multiplex_ratio = 0x3f,
159 .p_chargepump_cmd = SSD1306_CMD_SET_CHARGE_PUMP,
160 .p_chargepump_arg = SSD1306_CHARGE_PUMP_ENABLE
161 },
162 {
163 .p_product_id = SSDFB_PRODUCT_ADAFRUIT_931,
164 .p_controller_id = SSDFB_CONTROLLER_SSD1306,
165 .p_name = "Adafruit Industries, LLC product 931",
166 .p_width = 128,
167 .p_height = 32,
168 .p_panel_shift = 0,
169 .p_fosc = 0x8,
170 .p_fosc_div = 0,
171 .p_precharge = 0x1,
172 .p_discharge = 0xf,
173 .p_compin_cfg = 0x2,
174 .p_vcomh_deselect_level = 0x40,
175 .p_default_contrast = 0x8f,
176 .p_multiplex_ratio = 0x1f,
177 .p_chargepump_cmd = SSD1306_CMD_SET_CHARGE_PUMP,
178 .p_chargepump_arg = SSD1306_CHARGE_PUMP_ENABLE
179 }
180 };
181
182 static const struct wsdisplay_accessops ssdfb_accessops = {
183 .ioctl = ssdfb_ioctl,
184 .mmap = ssdfb_mmap,
185 .alloc_screen = ssdfb_alloc_screen,
186 .free_screen = ssdfb_free_screen,
187 .show_screen = ssdfb_show_screen
188 };
189
190 #define SSDFB_CMD1(c) do { cmd[0] = (c); error = sc->sc_cmd(sc->sc_cookie, cmd, 1, usepoll); } while(0)
191 #define SSDFB_CMD2(c, a) do { cmd[0] = (c); cmd[1] = (a); error = sc->sc_cmd(sc->sc_cookie, cmd, 2, usepoll); } while(0)
192 #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)
193
194 void
195 ssdfb_attach(struct ssdfb_softc *sc, int flags)
196 {
197 struct wsemuldisplaydev_attach_args aa;
198 struct rasops_info *ri = &sc->sc_ri;
199 int error = 0;
200 long defattr;
201 const struct ssdfb_product *p;
202
203 p = ssdfb_lookup_product(flags & SSDFB_ATTACH_FLAG_PRODUCT_MASK);
204 if (p == NULL) {
205 aprint_error(": unknown display assembly\n");
206 return;
207 }
208 sc->sc_p = p;
209
210 aprint_naive("\n");
211 aprint_normal(": %s (%s)\n",
212 ssdfb_controller_names[p->p_controller_id],
213 p->p_name);
214
215 sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
216 sc->sc_is_console = flags & SSDFB_ATTACH_FLAG_CONSOLE ? true : false;
217 sc->sc_inverse = flags & SSDFB_ATTACH_FLAG_INVERSE ? true : false;
218 sc->sc_upsidedown = flags & SSDFB_ATTACH_FLAG_UPSIDEDOWN ? true : false;
219 sc->sc_backoff = 1;
220 sc->sc_contrast = sc->sc_p->p_default_contrast;
221 sc->sc_gddram_len = sc->sc_p->p_width * sc->sc_p->p_height / 8;
222 sc->sc_gddram = kmem_alloc(sc->sc_gddram_len, KM_SLEEP);
223 if (sc->sc_gddram == NULL)
224 goto out;
225
226 aprint_normal_dev(sc->sc_dev, "%dx%d%s\n", sc->sc_p->p_width,
227 sc->sc_p->p_height, sc->sc_is_console ? ", console" : "");
228
229 /*
230 * Initialize rasops. The native depth is 1-bit monochrome and we
231 * support this in text emul mode via rasops1. But modern Xorg
232 * userland has many rendering glitches when running with 1-bit depth
233 * so to better support this use case we instead declare ourselves as
234 * an 8-bit display with a two entry constant color map.
235 */
236 error = ssdfb_pick_font(&sc->sc_fontcookie, &sc->sc_font);
237 if (error) {
238 aprint_error_dev(sc->sc_dev, "no font\n");
239 goto out;
240 }
241 ri->ri_depth = 8;
242 ri->ri_font = sc->sc_font;
243 ri->ri_width = sc->sc_p->p_width;
244 ri->ri_height = sc->sc_p->p_height;
245 ri->ri_stride = ri->ri_width * ri->ri_depth / 8;
246 ri->ri_hw = sc;
247 ri->ri_flg = RI_FULLCLEAR;
248 sc->sc_ri_bits_len = round_page(ri->ri_stride * ri->ri_height);
249 ri->ri_bits = (u_char *)uvm_km_alloc(kernel_map, sc->sc_ri_bits_len,
250 0, UVM_KMF_WIRED);
251 if (ri->ri_bits == NULL)
252 goto out;
253
254 error = rasops_init(ri,
255 sc->sc_p->p_height / sc->sc_font->fontheight,
256 sc->sc_p->p_width / sc->sc_font->fontwidth);
257 if (error)
258 goto out;
259
260 ri->ri_caps &= ~WSSCREEN_WSCOLORS;
261
262 /*
263 * Save original emul ops & insert our damage notification hooks.
264 */
265 sc->sc_orig_riops = ri->ri_ops;
266 ri->ri_ops.putchar = ssdfb_putchar;
267 ri->ri_ops.copycols = ssdfb_copycols;
268 ri->ri_ops.erasecols = ssdfb_erasecols;
269 ri->ri_ops.copyrows = ssdfb_copyrows;
270 ri->ri_ops.eraserows = ssdfb_eraserows;
271 ri->ri_ops.cursor = ssdfb_cursor;
272
273 /*
274 * Set up the screen.
275 */
276 sc->sc_screen_descr = (struct wsscreen_descr){
277 .name = "default",
278 .ncols = ri->ri_cols,
279 .nrows = ri->ri_rows,
280 .textops = &ri->ri_ops,
281 .fontwidth = ri->ri_font->fontwidth,
282 .fontheight = ri->ri_font->fontheight,
283 .capabilities = ri->ri_caps
284 };
285 sc->sc_screens[0] = &sc->sc_screen_descr;
286 sc->sc_screenlist = (struct wsscreen_list){
287 .nscreens = 1,
288 .screens = sc->sc_screens
289 };
290
291 /*
292 * Initialize hardware.
293 */
294 error = ssdfb_init(sc);
295 if (error)
296 goto out;
297
298 if (sc->sc_is_console)
299 ssdfb_set_usepoll(sc, true);
300
301 mutex_init(&sc->sc_cond_mtx, MUTEX_DEFAULT, IPL_SCHED);
302 cv_init(&sc->sc_cond, "ssdfb");
303 error = kthread_create(PRI_SOFTCLOCK, KTHREAD_MPSAFE | KTHREAD_MUSTJOIN,
304 NULL, ssdfb_thread, sc, &sc->sc_thread, "%s",
305 device_xname(sc->sc_dev));
306 if (error) {
307 cv_destroy(&sc->sc_cond);
308 mutex_destroy(&sc->sc_cond_mtx);
309 goto out;
310 }
311
312 /*
313 * Attach wsdisplay.
314 */
315 if (sc->sc_is_console) {
316 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
317 wsdisplay_cnattach(&sc->sc_screen_descr, ri, 0, 0, defattr);
318 #if defined(DDB)
319 db_trap_callback = ssdfb_ddb_trap_callback;
320 #endif
321 }
322 aa = (struct wsemuldisplaydev_attach_args){
323 .console = sc->sc_is_console,
324 .scrdata = &sc->sc_screenlist,
325 .accessops = &ssdfb_accessops,
326 .accesscookie = sc
327 };
328 sc->sc_wsdisplay =
329 config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
330
331 return;
332 out:
333 aprint_error_dev(sc->sc_dev, "attach failed: %d\n", error);
334 if (sc->sc_gddram != NULL)
335 kmem_free(sc->sc_gddram, sc->sc_gddram_len);
336 if (ri->ri_bits != NULL)
337 uvm_km_free(kernel_map, (vaddr_t)ri->ri_bits, sc->sc_ri_bits_len,
338 UVM_KMF_WIRED);
339 if (sc->sc_fontcookie > 0)
340 (void) wsfont_unlock(sc->sc_fontcookie);
341 }
342
343 int
344 ssdfb_detach(struct ssdfb_softc *sc)
345 {
346 mutex_enter(&sc->sc_cond_mtx);
347 sc->sc_detaching = true;
348 cv_broadcast(&sc->sc_cond);
349 mutex_exit(&sc->sc_cond_mtx);
350 kthread_join(sc->sc_thread);
351
352 config_detach(sc->sc_wsdisplay, DETACH_FORCE);
353
354 cv_destroy(&sc->sc_cond);
355 mutex_destroy(&sc->sc_cond_mtx);
356 uvm_km_free(kernel_map, (vaddr_t)sc->sc_ri.ri_bits, sc->sc_ri_bits_len,
357 UVM_KMF_WIRED);
358 kmem_free(sc->sc_gddram, sc->sc_gddram_len);
359 (void) wsfont_unlock(sc->sc_fontcookie);
360 return 0;
361 }
362
363 static int
364 ssdfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
365 {
366 struct ssdfb_softc *sc = v;
367 struct wsdisplay_param *wdp;
368 struct wsdisplay_cmap *wc;
369 u_char cmap[] = {0, 0xff};
370 int error;
371
372 switch (cmd) {
373 case WSDISPLAYIO_GTYPE:
374 *(u_int *)data = WSDISPLAY_TYPE_SSDFB;
375 return 0;
376 case WSDISPLAYIO_GINFO:
377 *(struct wsdisplay_fbinfo *)data = (struct wsdisplay_fbinfo){
378 .width = sc->sc_ri.ri_width,
379 .height = sc->sc_ri.ri_height,
380 .depth = sc->sc_ri.ri_depth,
381 .cmsize = 2
382 };
383 return 0;
384 case WSDISPLAYIO_GET_FBINFO:
385 return wsdisplayio_get_fbinfo(&sc->sc_ri,
386 (struct wsdisplayio_fbinfo *)data);
387 case WSDISPLAYIO_LINEBYTES:
388 *(u_int *)data = sc->sc_ri.ri_stride;
389 return 0;
390 case WSDISPLAYIO_GETPARAM:
391 wdp = (struct wsdisplay_param *)data;
392 if (wdp->param != WSDISPLAYIO_PARAM_CONTRAST)
393 return EINVAL;
394 wdp->min = 0;
395 wdp->max = 0xff;
396 wdp->curval = sc->sc_contrast;
397 return 0;
398 case WSDISPLAYIO_SETPARAM:
399 wdp = (struct wsdisplay_param *)data;
400 if (wdp->param != WSDISPLAYIO_PARAM_CONTRAST)
401 return EINVAL;
402 if (wdp->curval < 0 || wdp->curval > 0xff)
403 return EINVAL;
404 return ssdfb_set_contrast(sc, wdp->curval, sc->sc_usepoll);
405 case WSDISPLAYIO_GMODE:
406 *(u_int *)data = sc->sc_mode;
407 return 0;
408 case WSDISPLAYIO_SMODE:
409 return ssdfb_set_mode(sc, *(u_int *)data);
410 case WSDISPLAYIO_GVIDEO:
411 *(u_int *)data = sc->sc_display_on
412 ? WSDISPLAYIO_VIDEO_ON
413 : WSDISPLAYIO_VIDEO_OFF;
414 return 0;
415 case WSDISPLAYIO_SVIDEO:
416 switch (*(u_int *)data) {
417 case WSDISPLAYIO_VIDEO_ON:
418 case WSDISPLAYIO_VIDEO_OFF:
419 break;
420 default:
421 return EINVAL;
422 }
423 return ssdfb_set_display_on(sc,
424 *(u_int *)data == WSDISPLAYIO_VIDEO_ON ? true : false,
425 sc->sc_usepoll);
426 #if 0 /* don't let userland mess with polling yet */
427 case WSDISPLAYIO_SET_POLLING:
428 switch (*(u_int *)data) {
429 case 0:
430 case 1:
431 break;
432 default:
433 return EINVAL;
434 }
435 mutex_enter(&sc->sc_cond_mtx);
436 ssdfb_set_usepoll(sc, *(u_int *)data ? true : false);
437 cv_broadcast(&sc->sc_cond);
438 mutex_exit(&sc->sc_cond_mtx);
439 return 0;
440 #endif
441 case WSDISPLAYIO_GETCMAP:
442 wc = (struct wsdisplay_cmap *)data;
443 if (wc->index >= __arraycount(cmap) ||
444 wc->count >= __arraycount(cmap) - wc->index)
445 return EINVAL;
446 error = copyout(&cmap[wc->index], wc->red, wc->count);
447 if (error)
448 return error;
449 error = copyout(&cmap[wc->index], wc->green, wc->count);
450 if (error)
451 return error;
452 error = copyout(&cmap[wc->index], wc->blue, wc->count);
453 return error;
454 case WSDISPLAYIO_PUTCMAP:
455 return ENODEV;
456 }
457
458 return EPASSTHROUGH;
459 }
460
461 static paddr_t
462 ssdfb_mmap(void *v, void *vs, off_t off, int prot)
463 {
464 struct ssdfb_softc *sc = (struct ssdfb_softc *)v;
465 struct rasops_info *ri = &sc->sc_ri;
466 vaddr_t va_base = (vaddr_t)ri->ri_bits;
467 paddr_t pa;
468
469 if (off < 0 || off >= sc->sc_ri_bits_len || (off & PAGE_MASK) != 0)
470 return -1;
471
472 if (!pmap_extract(pmap_kernel(), va_base + off, &pa))
473 return -1;
474
475 return atop(pa);
476 }
477
478 static int
479 ssdfb_alloc_screen(void *v, const struct wsscreen_descr *descr, void **cookiep,
480 int *curxp, int *curyp, long *attrp)
481 {
482 struct ssdfb_softc *sc = v;
483 struct rasops_info *ri = &sc->sc_ri;
484
485 if (sc->sc_nscreens > 0)
486 return ENOMEM;
487
488 ri->ri_ops.allocattr(ri, 0, 0, 0, attrp);
489 *cookiep = &sc->sc_ri;
490 *curxp = 0;
491 *curyp = 0;
492 sc->sc_nscreens++;
493
494 return 0;
495 }
496
497 static void
498 ssdfb_free_screen(void *v, void *cookie)
499 {
500 struct ssdfb_softc *sc = v;
501
502 if (sc->sc_is_console)
503 panic("ssdfb_free_screen: is console");
504
505 sc->sc_nscreens--;
506 }
507
508 static int
509 ssdfb_show_screen(void *v, void *cookie, int waitok,
510 void (*cb) (void *, int, int), void *cb_arg)
511 {
512 return 0;
513 }
514
515 static void
516 ssdfb_putchar(void *cookie, int row, int col, u_int c, long attr)
517 {
518 struct rasops_info *ri = (struct rasops_info *)cookie;
519 struct ssdfb_softc *sc = ri->ri_hw;
520
521 sc->sc_orig_riops.putchar(cookie, row, col, c, attr);
522 ssdfb_damage(sc);
523 }
524
525 static void
526 ssdfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
527 {
528 struct rasops_info *ri = (struct rasops_info *)cookie;
529 struct ssdfb_softc *sc = ri->ri_hw;
530
531 sc->sc_orig_riops.copycols(cookie, row, srccol, dstcol, ncols);
532 ssdfb_damage(sc);
533 }
534
535 static void
536 ssdfb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
537 {
538 struct rasops_info *ri = (struct rasops_info *)cookie;
539 struct ssdfb_softc *sc = ri->ri_hw;
540
541 sc->sc_orig_riops.erasecols(cookie, row, startcol, ncols, fillattr);
542 ssdfb_damage(sc);
543 }
544
545 static void
546 ssdfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
547 {
548 struct rasops_info *ri = (struct rasops_info *)cookie;
549 struct ssdfb_softc *sc = ri->ri_hw;
550
551 sc->sc_orig_riops.copyrows(cookie, srcrow, dstrow, nrows);
552 ssdfb_damage(sc);
553 }
554
555 static void
556 ssdfb_eraserows(void *cookie, int row, int nrows, long fillattr)
557 {
558 struct rasops_info *ri = (struct rasops_info *)cookie;
559 struct ssdfb_softc *sc = ri->ri_hw;
560
561 sc->sc_orig_riops.eraserows(cookie, row, nrows, fillattr);
562 ssdfb_damage(sc);
563 }
564
565 static void
566 ssdfb_cursor(void *cookie, int on, int row, int col)
567 {
568 struct rasops_info *ri = (struct rasops_info *)cookie;
569 struct ssdfb_softc *sc = ri->ri_hw;
570
571 sc->sc_orig_riops.cursor(cookie, on, row, col);
572 ssdfb_damage(sc);
573 }
574
575 static int
576 ssdfb_init(struct ssdfb_softc *sc)
577 {
578 int error;
579 uint8_t cmd[3];
580 bool usepoll = true;
581
582 /*
583 * Enter sleep.
584 */
585 SSDFB_CMD1(SSDFB_CMD_SET_DISPLAY_OFF);
586 if (error)
587 return error;
588 SSDFB_CMD1(SSDFB_CMD_DEACTIVATE_SCROLL);
589 if (error)
590 return error;
591 SSDFB_CMD1(SSDFB_CMD_ENTIRE_DISPLAY_OFF);
592 if (error)
593 return error;
594
595 /*
596 * Configure physical display panel layout.
597 */
598 SSDFB_CMD2(SSDFB_CMD_SET_MULTIPLEX_RATIO, sc->sc_p->p_multiplex_ratio);
599 if (error)
600 return error;
601 SSDFB_CMD2(SSDFB_CMD_SET_DISPLAY_OFFSET, 0);
602 if (error)
603 return error;
604 SSDFB_CMD1(SSDFB_CMD_SET_DISPLAY_START_LINE_BASE + 0x00);
605 if (error)
606 return error;
607 SSDFB_CMD2(SSDFB_CMD_SET_COM_PINS_HARDWARE_CFG, sc->sc_p->p_compin_cfg);
608 if (error)
609 return error;
610 if (sc->sc_upsidedown) {
611 SSDFB_CMD1(SSDFB_CMD_SET_SEGMENT_REMAP_REVERSE);
612 if (error)
613 return error;
614 SSDFB_CMD1(SSDFB_CMD_SET_COM_OUTPUT_DIRECTION_REMAP);
615 if (error)
616 return error;
617 } else {
618 SSDFB_CMD1(SSDFB_CMD_SET_SEGMENT_REMAP_NORMAL);
619 if (error)
620 return error;
621 SSDFB_CMD1(SSDFB_CMD_SET_COM_OUTPUT_DIRECTION_NORMAL);
622 if (error)
623 return error;
624 }
625 SSDFB_CMD1(SSDFB_CMD_SET_NORMAL_DISPLAY + (uint8_t)sc->sc_inverse);
626 if (error)
627 return error;
628
629 /*
630 * Configure timing characteristics.
631 */
632 SSDFB_CMD2(SSDFB_CMD_SET_DISPLAY_CLOCK_RATIO,
633 ((sc->sc_p->p_fosc << SSDFB_DISPLAY_CLOCK_OSCILLATOR_SHIFT) &
634 SSDFB_DISPLAY_CLOCK_OSCILLATOR_MASK) |
635 ((sc->sc_p->p_fosc_div << SSDFB_DISPLAY_CLOCK_DIVIDER_SHIFT) &
636 SSDFB_DISPLAY_CLOCK_DIVIDER_MASK));
637 if (error)
638 return error;
639 SSDFB_CMD2(SSDFB_CMD_SET_CONTRAST_CONTROL, sc->sc_contrast);
640 if (error)
641 return error;
642 SSDFB_CMD2(SSDFB_CMD_SET_PRECHARGE_PERIOD,
643 ((sc->sc_p->p_precharge << SSDFB_PRECHARGE_SHIFT) &
644 SSDFB_PRECHARGE_MASK) |
645 ((sc->sc_p->p_discharge << SSDFB_DISCHARGE_SHIFT) &
646 SSDFB_DISCHARGE_MASK));
647 if (error)
648 return error;
649 SSDFB_CMD2(SSDFB_CMD_SET_VCOMH_DESELECT_LEVEL,
650 sc->sc_p->p_vcomh_deselect_level);
651 if (error)
652 return error;
653
654 /*
655 * Start charge pump.
656 */
657 SSDFB_CMD2(sc->sc_p->p_chargepump_cmd, sc->sc_p->p_chargepump_arg);
658 if (error)
659 return error;
660
661 if (sc->sc_p->p_controller_id == SSDFB_CONTROLLER_SH1106) {
662 SSDFB_CMD2(SH1106_CMD_SET_DC_DC, SH1106_DC_DC_ON);
663 if (error)
664 return error;
665 }
666
667 ssdfb_clear_screen(sc);
668 error = ssdfb_sync(sc, usepoll);
669 if (error)
670 return error;
671 error = ssdfb_set_display_on(sc, true, usepoll);
672
673 return error;
674 }
675
676 static int
677 ssdfb_set_contrast(struct ssdfb_softc *sc, uint8_t value, bool usepoll)
678 {
679 uint8_t cmd[2];
680 int error;
681
682 sc->sc_contrast = value;
683 SSDFB_CMD2(SSDFB_CMD_SET_CONTRAST_CONTROL, value);
684
685 return error;
686 }
687
688 static int
689 ssdfb_set_display_on(struct ssdfb_softc *sc, bool value, bool usepoll)
690 {
691 uint8_t cmd[1];
692 int error;
693 sc->sc_display_on = value;
694
695 SSDFB_CMD1(value ? SSDFB_CMD_SET_DISPLAY_ON : SSDFB_CMD_SET_DISPLAY_OFF);
696
697 return error;
698 }
699
700 static int
701 ssdfb_set_mode(struct ssdfb_softc *sc, u_int mode)
702 {
703 switch (mode) {
704 case WSDISPLAYIO_MODE_EMUL:
705 case WSDISPLAYIO_MODE_DUMBFB:
706 break;
707 default:
708 return EINVAL;
709 }
710 if (mode == sc->sc_mode)
711 return 0;
712 mutex_enter(&sc->sc_cond_mtx);
713 sc->sc_mode = mode;
714 cv_broadcast(&sc->sc_cond);
715 mutex_exit(&sc->sc_cond_mtx);
716 ssdfb_clear_screen(sc);
717 ssdfb_damage(sc);
718
719 return 0;
720 }
721
722 static void
723 ssdfb_damage(struct ssdfb_softc *sc)
724 {
725 int s;
726
727 if (sc->sc_usepoll) {
728 (void) ssdfb_sync(sc, true);
729 } else {
730 /*
731 * kernel code isn't permitted to call us via kprintf at
732 * splhigh. In case misbehaving code calls us anyway we can't
733 * safely take the mutex so we skip the damage notification.
734 */
735 if (sc->sc_is_console) {
736 s = splhigh();
737 splx(s);
738 if (s == IPL_HIGH)
739 return;
740 }
741 mutex_enter(&sc->sc_cond_mtx);
742 sc->sc_modified = true;
743 cv_broadcast(&sc->sc_cond);
744 mutex_exit(&sc->sc_cond_mtx);
745 }
746 }
747
748 static bool
749 ssdfb_is_modified(struct ssdfb_softc *sc)
750 {
751 vaddr_t va, va_end;
752
753 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)
754 return sc->sc_modified;
755
756 va = (vaddr_t)sc->sc_ri.ri_bits;
757 va_end = va + sc->sc_ri_bits_len;
758 while (va < va_end) {
759 if (pmap_is_modified(uvm_pageratop(va)))
760 return true;
761 va += PAGE_SIZE;
762 }
763
764 return false;
765 }
766
767 static bool
768 ssdfb_clear_modify(struct ssdfb_softc *sc)
769 {
770 vaddr_t va, va_end;
771 bool ret;
772
773 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
774 ret = sc->sc_modified;
775 sc->sc_modified = false;
776 return ret;
777 }
778
779 va = (vaddr_t)sc->sc_ri.ri_bits;
780 va_end = va + sc->sc_ri_bits_len;
781 ret = false;
782 while (va < va_end) {
783 if (pmap_clear_modify(uvm_pageratop(va)))
784 ret = true;
785 va += PAGE_SIZE;
786 }
787
788 return ret;
789 }
790
791 static void
792 ssdfb_thread(void *arg)
793 {
794 struct ssdfb_softc *sc = (struct ssdfb_softc *)arg;
795 int error;
796
797 mutex_enter(&sc->sc_cond_mtx);
798
799 if (sc->sc_usepoll)
800 ssdfb_set_usepoll(sc, false);
801
802 while(!sc->sc_detaching) {
803 if (!ssdfb_is_modified(sc)) {
804 if (cv_timedwait(&sc->sc_cond, &sc->sc_cond_mtx,
805 sc->sc_mode == WSDISPLAYIO_MODE_EMUL
806 ? 0 : sc->sc_backoff) == EWOULDBLOCK
807 && sc->sc_backoff < mstohz(200)) {
808 sc->sc_backoff <<= 1;
809 }
810 continue;
811 }
812 sc->sc_backoff = 1;
813 (void) ssdfb_clear_modify(sc);
814 if (sc->sc_usepoll)
815 continue;
816 mutex_exit(&sc->sc_cond_mtx);
817 error = ssdfb_sync(sc, false);
818 if (error)
819 device_printf(sc->sc_dev, "ssdfb_sync: error %d\n",
820 error);
821 mutex_enter(&sc->sc_cond_mtx);
822 }
823
824 mutex_exit(&sc->sc_cond_mtx);
825 }
826
827 static void
828 ssdfb_set_usepoll(struct ssdfb_softc *sc, bool enable)
829 {
830 sc->sc_usepoll = enable;
831 }
832
833 static int
834 ssdfb_sync(struct ssdfb_softc *sc, bool usepoll)
835 {
836 struct rasops_info *ri = &sc->sc_ri;
837 int block_size = 8;
838 int ri_block_stride = ri->ri_stride * block_size;
839 int height_in_blocks = sc->sc_p->p_height / block_size;
840 int width_in_blocks = sc->sc_p->p_width / block_size;
841 int ri_block_step = block_size * ri->ri_depth / 8;
842 int x, y;
843 union ssdfb_block *blockp;
844 uint64_t raw_block;
845 uint8_t *src;
846 int x1, x2, y1, y2;
847
848 /*
849 * Transfer rasops bitmap into gddram shadow buffer while keeping track
850 * of the bounding box of the dirty region we scribbled over.
851 */
852 x1 = width_in_blocks;
853 x2 = -1;
854 y1 = height_in_blocks;
855 y2 = -1;
856 for (y = 0; y < height_in_blocks; y++) {
857 src = &ri->ri_bits[y*ri_block_stride];
858 blockp = &sc->sc_gddram[y * width_in_blocks];
859 for (x = 0; x < width_in_blocks; x++) {
860 raw_block = (ri->ri_depth == 1)
861 ? ssdfb_transpose_block_1bpp(src, ri->ri_stride)
862 : ssdfb_transpose_block_8bpp(src, ri->ri_stride);
863 if (raw_block != blockp->raw) {
864 blockp->raw = raw_block;
865 if (x1 > x)
866 x1 = x;
867 if (x2 < x)
868 x2 = x;
869 if (y1 > y)
870 y1 = y;
871 if (y2 < y)
872 y2 = y;
873 }
874 src += ri_block_step;
875 blockp++;
876 }
877 }
878 if (x2 != -1)
879 return sc->sc_transfer_rect(sc->sc_cookie,
880 x1 * block_size + sc->sc_p->p_panel_shift,
881 (x2 + 1) * block_size - 1 + sc->sc_p->p_panel_shift,
882 y1,
883 y2,
884 &sc->sc_gddram[y1 * width_in_blocks + x1].col[0],
885 sc->sc_p->p_width,
886 usepoll);
887
888 return 0;
889 }
890
891 static uint64_t
892 ssdfb_transpose_block_1bpp(uint8_t *src, size_t src_stride)
893 {
894 uint64_t x = 0;
895 uint64_t t;
896 int i;
897
898 /*
899 * collect the 8x8 block.
900 */
901 for (i = 0; i < 8; i++) {
902 x >>= 8;
903 x |= (uint64_t)src[i * src_stride] << 56;
904 }
905
906 /*
907 * Transpose it into gddram layout.
908 * Post-transpose bswap is the same as pre-transpose bit order reversal.
909 * We do this to match rasops1 bit order.
910 */
911 t = (x ^ (x >> 28)) & 0x00000000F0F0F0F0ULL;
912 x = x ^ t ^ (t << 28);
913 t = (x ^ (x >> 14)) & 0x0000CCCC0000CCCCULL;
914 x = x ^ t ^ (t << 14);
915 t = (x ^ (x >> 7)) & 0x00AA00AA00AA00AAULL;
916 x = x ^ t ^ (t << 7);
917 x = bswap64(x);
918
919 return htole64(x);
920 }
921
922 static uint64_t
923 ssdfb_transpose_block_8bpp(uint8_t *src, size_t src_stride)
924 {
925 uint64_t x = 0;
926 int m, n;
927
928 for (m = 0; m < 8; m++) {
929 for (n = 0; n < 8; n++) {
930 x >>= 1;
931 x |= src[n * src_stride + m] ? (1ULL << 63) : 0;
932 }
933 }
934
935 return htole64(x);
936 }
937
938 static const struct ssdfb_product *
939 ssdfb_lookup_product(ssdfb_product_id_t id)
940 {
941 int i;
942
943 for (i = 0; i < __arraycount(ssdfb_products); i++) {
944 if (ssdfb_products[i].p_product_id == id)
945 return &ssdfb_products[i];
946 }
947
948 return NULL;
949 }
950
951 static int
952 ssdfb_pick_font(int *cookiep, struct wsdisplay_font **fontp)
953 {
954 int error;
955 int c;
956 struct wsdisplay_font *f;
957 int i;
958 uint8_t d[4][2] = {{5, 8}, {8, 8}, {8, 10} ,{8, 16}};
959
960 /*
961 * Try to find fonts in order of inreasing size.
962 */
963 wsfont_init();
964 for(i = 0; i < __arraycount(d); i++) {
965 c = wsfont_find(NULL, d[i][0], d[i][1], 0,
966 WSDISPLAY_FONTORDER_L2R, WSDISPLAY_FONTORDER_L2R,
967 WSFONT_FIND_BITMAP);
968 if (c > 0)
969 break;
970 }
971 if (c <= 0)
972 return ENOENT;
973 error = wsfont_lock(c, &f);
974 if (error)
975 return error;
976 *cookiep = c;
977 *fontp = f;
978
979 return 0;
980 }
981
982 static void
983 ssdfb_clear_screen(struct ssdfb_softc *sc)
984 {
985 struct rasops_info *ri = &sc->sc_ri;
986
987 memset(sc->sc_gddram, 0xff, sc->sc_gddram_len);
988 memset(ri->ri_bits, 0, sc->sc_ri_bits_len);
989 }
990
991 #if defined(DDB)
992 static void
993 ssdfb_ddb_trap_callback(int enable)
994 {
995 extern struct cfdriver ssdfb_cd;
996 struct ssdfb_softc *sc;
997 int i;
998
999 for (i = 0; i < ssdfb_cd.cd_ndevs; i++) {
1000 sc = device_lookup_private(&ssdfb_cd, i);
1001 if (sc != NULL && sc->sc_is_console) {
1002 ssdfb_set_usepoll(sc, (bool)enable);
1003 }
1004 }
1005 }
1006 #endif
1007