xref: /freebsd-13-stable/sys/arm/versatile/versatile_clcd.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2012-2017 Oleksandr Tymoshenko <gonzo@freebsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/malloc.h>
36 #include <sys/rman.h>
37 #include <sys/fbio.h>
38 #include <sys/consio.h>
39 #include <sys/kdb.h>
40 
41 #include <machine/bus.h>
42 #include <machine/cpu.h>
43 #include <machine/intr.h>
44 
45 #include <dev/ofw/openfirm.h>
46 #include <dev/ofw/ofw_bus.h>
47 #include <dev/ofw/ofw_bus_subr.h>
48 
49 #include <dev/fb/fbreg.h>
50 #include <dev/syscons/syscons.h>
51 
52 #include <arm/versatile/versatile_scm.h>
53 
54 #include <machine/bus.h>
55 
56 #define	PL110_VENDOR_ARM926PXP	1
57 
58 #define	CLCD_MODE_RGB888	0x0
59 #define	CLCD_MODE_RGB555	0x01
60 #define	CLCD_MODE_RBG565	0x02
61 #define	CLCD_MODE_RGB565	0x03
62 
63 #define	CLCDC_TIMING0		0x00
64 #define	CLCDC_TIMING1		0x04
65 #define	CLCDC_TIMING2		0x08
66 #define	CLCDC_TIMING3		0x0C
67 #define	CLCDC_TIMING3		0x0C
68 #define	CLCDC_UPBASE		0x10
69 #define	CLCDC_LPBASE		0x14
70 #ifdef PL110_VENDOR_ARM926PXP
71 #define	CLCDC_CONTROL		0x18
72 #define	CLCDC_IMSC		0x1C
73 #else
74 #define	CLCDC_IMSC		0x18
75 #define	CLCDC_CONTROL		0x1C
76 #endif
77 #define		CONTROL_WATERMARK	(1 << 16)
78 #define		CONTROL_VCOMP_VS	(0 << 12)
79 #define		CONTROL_VCOMP_BP	(1 << 12)
80 #define		CONTROL_VCOMP_SAV	(2 << 12)
81 #define		CONTROL_VCOMP_FP	(3 << 12)
82 #define		CONTROL_PWR		(1 << 11)
83 #define		CONTROL_BEPO		(1 << 10)
84 #define		CONTROL_BEBO		(1 << 9)
85 #define		CONTROL_BGR		(1 << 8)
86 #define		CONTROL_DUAL		(1 << 7)
87 #define		CONTROL_MONO8		(1 << 6)
88 #define		CONTROL_TFT		(1 << 5)
89 #define		CONTROL_BW		(1 << 4)
90 #define		CONTROL_BPP1		(0x00 << 1)
91 #define		CONTROL_BPP2		(0x01 << 1)
92 #define		CONTROL_BPP4		(0x02 << 1)
93 #define		CONTROL_BPP8		(0x03 << 1)
94 #define		CONTROL_BPP16		(0x04 << 1)
95 #define		CONTROL_BPP24		(0x05 << 1)
96 #define		CONTROL_EN	(1 << 0)
97 #define	CLCDC_RIS		0x20
98 #define	CLCDC_MIS		0x24
99 #define		INTR_MBERR		(1 << 4)
100 #define		INTR_VCOMP		(1 << 3)
101 #define		INTR_LNB		(1 << 2)
102 #define		INTR_FUF		(1 << 1)
103 #define	CLCDC_ICR		0x28
104 
105 #ifdef DEBUG
106 #define dprintf(fmt, args...) do { printf("%s(): ", __func__);   \
107     printf(fmt,##args); } while (0)
108 #else
109 #define dprintf(fmt, args...)
110 #endif
111 
112 #define	versatile_clcdc_read_4(sc, reg)	\
113 	bus_read_4((sc)->mem_res, (reg))
114 #define	versatile_clcdc_write_4(sc, reg, val)	\
115 	bus_write_4((sc)->mem_res, (reg), (val))
116 
117 struct versatile_clcdc_softc {
118 	struct resource*	mem_res;
119 
120 	struct mtx		mtx;
121 
122 	int			width;
123 	int			height;
124 	int			mode;
125 
126 	bus_dma_tag_t		dma_tag;
127 	bus_dmamap_t		dma_map;
128 	bus_addr_t		fb_phys;
129 	uint8_t			*fb_base;
130 
131 };
132 
133 struct video_adapter_softc {
134 	/* Videoadpater part */
135 	video_adapter_t	va;
136 	int		console;
137 
138 	intptr_t	fb_addr;
139 	unsigned int	fb_size;
140 
141 	unsigned int	height;
142 	unsigned int	width;
143 	unsigned int	depth;
144 	unsigned int	stride;
145 
146 	unsigned int	xmargin;
147 	unsigned int	ymargin;
148 
149 	unsigned char	*font;
150 	int		initialized;
151 };
152 
153 struct argb {
154 	uint8_t		a;
155 	uint8_t		r;
156 	uint8_t		g;
157 	uint8_t		b;
158 };
159 
160 static struct argb versatilefb_palette[16] = {
161 	{0x00, 0x00, 0x00, 0x00},
162 	{0x00, 0x00, 0x00, 0xaa},
163 	{0x00, 0x00, 0xaa, 0x00},
164 	{0x00, 0x00, 0xaa, 0xaa},
165 	{0x00, 0xaa, 0x00, 0x00},
166 	{0x00, 0xaa, 0x00, 0xaa},
167 	{0x00, 0xaa, 0x55, 0x00},
168 	{0x00, 0xaa, 0xaa, 0xaa},
169 	{0x00, 0x55, 0x55, 0x55},
170 	{0x00, 0x55, 0x55, 0xff},
171 	{0x00, 0x55, 0xff, 0x55},
172 	{0x00, 0x55, 0xff, 0xff},
173 	{0x00, 0xff, 0x55, 0x55},
174 	{0x00, 0xff, 0x55, 0xff},
175 	{0x00, 0xff, 0xff, 0x55},
176 	{0x00, 0xff, 0xff, 0xff}
177 };
178 
179 /* mouse pointer from dev/syscons/scgfbrndr.c */
180 static u_char mouse_pointer[16] = {
181         0x00, 0x40, 0x60, 0x70, 0x78, 0x7c, 0x7e, 0x68,
182         0x0c, 0x0c, 0x06, 0x06, 0x00, 0x00, 0x00, 0x00
183 };
184 
185 #define FB_WIDTH		640
186 #define FB_HEIGHT		480
187 #define FB_DEPTH		16
188 
189 #define	VERSATILE_FONT_HEIGHT	16
190 
191 static struct video_adapter_softc va_softc;
192 
193 static int versatilefb_configure(int);
194 static void versatilefb_update_margins(video_adapter_t *adp);
195 
196 static void
versatile_fb_dmamap_cb(void * arg,bus_dma_segment_t * segs,int nseg,int err)197 versatile_fb_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int err)
198 {
199 	bus_addr_t *addr;
200 
201 	if (err)
202 		return;
203 
204 	addr = (bus_addr_t*)arg;
205 	*addr = segs[0].ds_addr;
206 }
207 
208 static int
versatile_clcdc_probe(device_t dev)209 versatile_clcdc_probe(device_t dev)
210 {
211 
212 	if (!ofw_bus_status_okay(dev))
213 		return (ENXIO);
214 
215 	if (ofw_bus_is_compatible(dev, "arm,pl110")) {
216 		device_set_desc(dev, "PL110 CLCD controller");
217 		return (BUS_PROBE_DEFAULT);
218 	}
219 
220 	return (ENXIO);
221 }
222 
223 static int
versatile_clcdc_attach(device_t dev)224 versatile_clcdc_attach(device_t dev)
225 {
226 	struct versatile_clcdc_softc *sc = device_get_softc(dev);
227 	struct video_adapter_softc *va_sc = &va_softc;
228 	int err, rid;
229 	uint32_t reg;
230 	int clcdid;
231 	int dma_size;
232 
233 	/* Request memory resources */
234 	rid = 0;
235 	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
236 	if (sc->mem_res == NULL) {
237 		device_printf(dev, "could not allocate memory resources\n");
238 		return (ENXIO);
239 	}
240 
241 	err = versatile_scm_reg_read_4(SCM_CLCD, &reg);
242 	if (err) {
243 		device_printf(dev, "failed to read SCM register\n");
244 		goto fail;
245 	}
246 	clcdid = (reg >> SCM_CLCD_CLCDID_SHIFT) & SCM_CLCD_CLCDID_MASK;
247 	switch (clcdid) {
248 		case 31:
249 			device_printf(dev, "QEMU VGA 640x480\n");
250 			sc->width = 640;
251 			sc->height = 480;
252 			break;
253 		default:
254 			device_printf(dev, "Unsupported: %d\n", clcdid);
255 			goto fail;
256 	}
257 
258 	reg &= ~SCM_CLCD_LCD_MODE_MASK;
259 	reg |= CLCD_MODE_RGB565;
260 	sc->mode = CLCD_MODE_RGB565;
261 	versatile_scm_reg_write_4(SCM_CLCD, reg);
262  	dma_size = sc->width*sc->height*2;
263 
264  	/*
265 	 * Power on LCD
266 	 */
267 	reg |= SCM_CLCD_PWR3V5VSWITCH | SCM_CLCD_NLCDIOON;
268 	versatile_scm_reg_write_4(SCM_CLCD, reg);
269 
270 	/*
271 	 * XXX: hardcoded timing for VGA. For other modes/panels
272 	 * we need to keep table of timing register values
273 	 */
274 	/*
275 	 * XXX: set SYS_OSC1
276 	 */
277 	versatile_clcdc_write_4(sc, CLCDC_TIMING0, 0x3F1F3F9C);
278 	versatile_clcdc_write_4(sc, CLCDC_TIMING1, 0x090B61DF);
279 	versatile_clcdc_write_4(sc, CLCDC_TIMING2, 0x067F1800);
280 	/* XXX: timing 3? */
281 
282 	/*
283 	 * Now allocate framebuffer memory
284 	 */
285 	err = bus_dma_tag_create(
286 	    bus_get_dma_tag(dev),
287 	    4, 0,		/* alignment, boundary */
288 	    BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
289 	    BUS_SPACE_MAXADDR,		/* highaddr */
290 	    NULL, NULL,			/* filter, filterarg */
291 	    dma_size, 1,		/* maxsize, nsegments */
292 	    dma_size, 0,		/* maxsegsize, flags */
293 	    NULL, NULL,			/* lockfunc, lockarg */
294 	    &sc->dma_tag);
295 
296 	err = bus_dmamem_alloc(sc->dma_tag, (void **)&sc->fb_base,
297 	    0, &sc->dma_map);
298 	if (err) {
299 		device_printf(dev, "cannot allocate framebuffer\n");
300 		goto fail;
301 	}
302 
303 	err = bus_dmamap_load(sc->dma_tag, sc->dma_map, sc->fb_base,
304 	    dma_size, versatile_fb_dmamap_cb, &sc->fb_phys, BUS_DMA_NOWAIT);
305 
306 	if (err) {
307 		device_printf(dev, "cannot load DMA map\n");
308 		goto fail;
309 	}
310 
311 	/* Make sure it's blank */
312 	memset(sc->fb_base, 0x00, dma_size);
313 
314 	versatile_clcdc_write_4(sc, CLCDC_UPBASE, sc->fb_phys);
315 
316 	err = (sc_attach_unit(device_get_unit(dev),
317 	    device_get_flags(dev) | SC_AUTODETECT_KBD));
318 
319 	if (err) {
320 		device_printf(dev, "failed to attach syscons\n");
321 		goto fail;
322 	}
323 
324 	/*
325 	 * XXX: hardcoded for VGA
326 	 */
327 	reg = CONTROL_VCOMP_BP | CONTROL_TFT | CONTROL_BGR | CONTROL_EN;
328 	reg |= CONTROL_BPP16;
329 	versatile_clcdc_write_4(sc, CLCDC_CONTROL, reg);
330 	DELAY(20);
331 	reg |= CONTROL_PWR;
332 	versatile_clcdc_write_4(sc, CLCDC_CONTROL, reg);
333 
334 	va_sc->fb_addr = (vm_offset_t)sc->fb_base;
335 	va_sc->fb_size = dma_size;
336 	va_sc->width = sc->width;
337 	va_sc->height = sc->height;
338 	va_sc->depth = 16;
339 	va_sc->stride = sc->width * 2;
340 	versatilefb_update_margins(&va_sc->va);
341 
342 	return (0);
343 
344 fail:
345 	if (sc->fb_base)
346 		bus_dmamem_free(sc->dma_tag, sc->fb_base, sc->dma_map);
347 	if (sc->dma_tag)
348 		bus_dma_tag_destroy(sc->dma_tag);
349 	return (err);
350 }
351 
352 static device_method_t versatile_clcdc_methods[] = {
353 	DEVMETHOD(device_probe,		versatile_clcdc_probe),
354 	DEVMETHOD(device_attach,	versatile_clcdc_attach),
355 
356 	DEVMETHOD_END
357 };
358 
359 static driver_t versatile_clcdc_driver = {
360 	"clcdc",
361 	versatile_clcdc_methods,
362 	sizeof(struct versatile_clcdc_softc),
363 };
364 
365 static devclass_t versatile_clcdc_devclass;
366 
367 DRIVER_MODULE(versatile_clcdc, simplebus, versatile_clcdc_driver, versatile_clcdc_devclass, 0, 0);
368 
369 /*
370  * Video driver routines and glue.
371  */
372 static vi_probe_t		versatilefb_probe;
373 static vi_init_t		versatilefb_init;
374 static vi_get_info_t		versatilefb_get_info;
375 static vi_query_mode_t		versatilefb_query_mode;
376 static vi_set_mode_t		versatilefb_set_mode;
377 static vi_save_font_t		versatilefb_save_font;
378 static vi_load_font_t		versatilefb_load_font;
379 static vi_show_font_t		versatilefb_show_font;
380 static vi_save_palette_t	versatilefb_save_palette;
381 static vi_load_palette_t	versatilefb_load_palette;
382 static vi_set_border_t		versatilefb_set_border;
383 static vi_save_state_t		versatilefb_save_state;
384 static vi_load_state_t		versatilefb_load_state;
385 static vi_set_win_org_t		versatilefb_set_win_org;
386 static vi_read_hw_cursor_t	versatilefb_read_hw_cursor;
387 static vi_set_hw_cursor_t	versatilefb_set_hw_cursor;
388 static vi_set_hw_cursor_shape_t	versatilefb_set_hw_cursor_shape;
389 static vi_blank_display_t	versatilefb_blank_display;
390 static vi_mmap_t		versatilefb_mmap;
391 static vi_ioctl_t		versatilefb_ioctl;
392 static vi_clear_t		versatilefb_clear;
393 static vi_fill_rect_t		versatilefb_fill_rect;
394 static vi_bitblt_t		versatilefb_bitblt;
395 static vi_diag_t		versatilefb_diag;
396 static vi_save_cursor_palette_t	versatilefb_save_cursor_palette;
397 static vi_load_cursor_palette_t	versatilefb_load_cursor_palette;
398 static vi_copy_t		versatilefb_copy;
399 static vi_putp_t		versatilefb_putp;
400 static vi_putc_t		versatilefb_putc;
401 static vi_puts_t		versatilefb_puts;
402 static vi_putm_t		versatilefb_putm;
403 
404 static video_switch_t versatilefbvidsw = {
405 	.probe			= versatilefb_probe,
406 	.init			= versatilefb_init,
407 	.get_info		= versatilefb_get_info,
408 	.query_mode		= versatilefb_query_mode,
409 	.set_mode		= versatilefb_set_mode,
410 	.save_font		= versatilefb_save_font,
411 	.load_font		= versatilefb_load_font,
412 	.show_font		= versatilefb_show_font,
413 	.save_palette		= versatilefb_save_palette,
414 	.load_palette		= versatilefb_load_palette,
415 	.set_border		= versatilefb_set_border,
416 	.save_state		= versatilefb_save_state,
417 	.load_state		= versatilefb_load_state,
418 	.set_win_org		= versatilefb_set_win_org,
419 	.read_hw_cursor		= versatilefb_read_hw_cursor,
420 	.set_hw_cursor		= versatilefb_set_hw_cursor,
421 	.set_hw_cursor_shape	= versatilefb_set_hw_cursor_shape,
422 	.blank_display		= versatilefb_blank_display,
423 	.mmap			= versatilefb_mmap,
424 	.ioctl			= versatilefb_ioctl,
425 	.clear			= versatilefb_clear,
426 	.fill_rect		= versatilefb_fill_rect,
427 	.bitblt			= versatilefb_bitblt,
428 	.diag			= versatilefb_diag,
429 	.save_cursor_palette	= versatilefb_save_cursor_palette,
430 	.load_cursor_palette	= versatilefb_load_cursor_palette,
431 	.copy			= versatilefb_copy,
432 	.putp			= versatilefb_putp,
433 	.putc			= versatilefb_putc,
434 	.puts			= versatilefb_puts,
435 	.putm			= versatilefb_putm,
436 };
437 
438 VIDEO_DRIVER(versatilefb, versatilefbvidsw, versatilefb_configure);
439 
440 static vr_init_t clcdr_init;
441 static vr_clear_t clcdr_clear;
442 static vr_draw_border_t clcdr_draw_border;
443 static vr_draw_t clcdr_draw;
444 static vr_set_cursor_t clcdr_set_cursor;
445 static vr_draw_cursor_t clcdr_draw_cursor;
446 static vr_blink_cursor_t clcdr_blink_cursor;
447 static vr_set_mouse_t clcdr_set_mouse;
448 static vr_draw_mouse_t clcdr_draw_mouse;
449 
450 /*
451  * We use our own renderer; this is because we must emulate a hardware
452  * cursor.
453  */
454 static sc_rndr_sw_t clcdrend = {
455 	clcdr_init,
456 	clcdr_clear,
457 	clcdr_draw_border,
458 	clcdr_draw,
459 	clcdr_set_cursor,
460 	clcdr_draw_cursor,
461 	clcdr_blink_cursor,
462 	clcdr_set_mouse,
463 	clcdr_draw_mouse
464 };
465 
466 RENDERER(versatilefb, 0, clcdrend, gfb_set);
467 RENDERER_MODULE(versatilefb, gfb_set);
468 
469 static void
clcdr_init(scr_stat * scp)470 clcdr_init(scr_stat* scp)
471 {
472 }
473 
474 static void
clcdr_clear(scr_stat * scp,int c,int attr)475 clcdr_clear(scr_stat* scp, int c, int attr)
476 {
477 }
478 
479 static void
clcdr_draw_border(scr_stat * scp,int color)480 clcdr_draw_border(scr_stat* scp, int color)
481 {
482 }
483 
484 static void
clcdr_draw(scr_stat * scp,int from,int count,int flip)485 clcdr_draw(scr_stat* scp, int from, int count, int flip)
486 {
487 	video_adapter_t* adp = scp->sc->adp;
488 	int i, c, a;
489 
490 	if (!flip) {
491 		/* Normal printing */
492 		vidd_puts(adp, from, (uint16_t*)sc_vtb_pointer(&scp->vtb, from), count);
493 	} else {
494 		/* This is for selections and such: invert the color attribute */
495 		for (i = count; i-- > 0; ++from) {
496 			c = sc_vtb_getc(&scp->vtb, from);
497 			a = sc_vtb_geta(&scp->vtb, from) >> 8;
498 			vidd_putc(adp, from, c, (a >> 4) | ((a & 0xf) << 4));
499 		}
500 	}
501 }
502 
503 static void
clcdr_set_cursor(scr_stat * scp,int base,int height,int blink)504 clcdr_set_cursor(scr_stat* scp, int base, int height, int blink)
505 {
506 }
507 
508 static void
clcdr_draw_cursor(scr_stat * scp,int off,int blink,int on,int flip)509 clcdr_draw_cursor(scr_stat* scp, int off, int blink, int on, int flip)
510 {
511 	video_adapter_t* adp = scp->sc->adp;
512 	struct video_adapter_softc *sc;
513 	int row, col;
514 	uint8_t *addr;
515 	int i,j;
516 
517 	sc = (struct video_adapter_softc *)adp;
518 
519 	if (scp->curs_attr.height <= 0)
520 		return;
521 
522 	if (sc->fb_addr == 0)
523 		return;
524 
525 	if (off >= adp->va_info.vi_width * adp->va_info.vi_height)
526 		return;
527 
528 	/* calculate the coordinates in the video buffer */
529 	row = (off / adp->va_info.vi_width) * adp->va_info.vi_cheight;
530 	col = (off % adp->va_info.vi_width) * adp->va_info.vi_cwidth;
531 
532 	addr = (uint8_t *)sc->fb_addr
533 	    + (row + sc->ymargin)*(sc->stride)
534 	    + (sc->depth/8) * (col + sc->xmargin);
535 
536 	/* our cursor consists of simply inverting the char under it */
537 	for (i = 0; i < adp->va_info.vi_cheight; i++) {
538 		for (j = 0; j < adp->va_info.vi_cwidth; j++) {
539 			addr[2*j] ^= 0xff;
540 			addr[2*j + 1] ^= 0xff;
541 		}
542 
543 		addr += sc->stride;
544 	}
545 }
546 
547 static void
clcdr_blink_cursor(scr_stat * scp,int at,int flip)548 clcdr_blink_cursor(scr_stat* scp, int at, int flip)
549 {
550 }
551 
552 static void
clcdr_set_mouse(scr_stat * scp)553 clcdr_set_mouse(scr_stat* scp)
554 {
555 }
556 
557 static void
clcdr_draw_mouse(scr_stat * scp,int x,int y,int on)558 clcdr_draw_mouse(scr_stat* scp, int x, int y, int on)
559 {
560 	vidd_putm(scp->sc->adp, x, y, mouse_pointer, 0xffffffff, 16, 8);
561 
562 }
563 
564 static uint16_t versatilefb_static_window[ROW*COL];
565 extern u_char dflt_font_16[];
566 
567 /*
568  * Update videoadapter settings after changing resolution
569  */
570 static void
versatilefb_update_margins(video_adapter_t * adp)571 versatilefb_update_margins(video_adapter_t *adp)
572 {
573 	struct video_adapter_softc *sc;
574 	video_info_t *vi;
575 
576 	sc = (struct video_adapter_softc *)adp;
577 	vi = &adp->va_info;
578 
579 	sc->xmargin = (sc->width - (vi->vi_width * vi->vi_cwidth)) / 2;
580 	sc->ymargin = (sc->height - (vi->vi_height * vi->vi_cheight))/2;
581 }
582 
583 static int
versatilefb_configure(int flags)584 versatilefb_configure(int flags)
585 {
586 	struct video_adapter_softc *va_sc;
587 
588 	va_sc = &va_softc;
589 
590 	if (va_sc->initialized)
591 		return (0);
592 
593 	va_sc->width = FB_WIDTH;
594 	va_sc->height = FB_HEIGHT;
595 	va_sc->depth = FB_DEPTH;
596 
597 	versatilefb_init(0, &va_sc->va, 0);
598 
599 	va_sc->initialized = 1;
600 
601 	return (0);
602 }
603 
604 static int
versatilefb_probe(int unit,video_adapter_t ** adp,void * arg,int flags)605 versatilefb_probe(int unit, video_adapter_t **adp, void *arg, int flags)
606 {
607 
608 	return (0);
609 }
610 
611 static int
versatilefb_init(int unit,video_adapter_t * adp,int flags)612 versatilefb_init(int unit, video_adapter_t *adp, int flags)
613 {
614 	struct video_adapter_softc *sc;
615 	video_info_t *vi;
616 
617 	sc = (struct video_adapter_softc *)adp;
618 	vi = &adp->va_info;
619 
620 	vid_init_struct(adp, "versatilefb", -1, unit);
621 
622 	sc->font = dflt_font_16;
623 	vi->vi_cheight = VERSATILE_FONT_HEIGHT;
624 	vi->vi_cwidth = 8;
625 
626 	vi->vi_width = sc->width/8;
627 	vi->vi_height = sc->height/vi->vi_cheight;
628 
629 	/*
630 	 * Clamp width/height to syscons maximums
631 	 */
632 	if (vi->vi_width > COL)
633 		vi->vi_width = COL;
634 	if (vi->vi_height > ROW)
635 		vi->vi_height = ROW;
636 
637 	sc->xmargin = (sc->width - (vi->vi_width * vi->vi_cwidth)) / 2;
638 	sc->ymargin = (sc->height - (vi->vi_height * vi->vi_cheight))/2;
639 
640 	adp->va_window = (vm_offset_t) versatilefb_static_window;
641 	adp->va_flags |= V_ADP_FONT /* | V_ADP_COLOR | V_ADP_MODECHANGE */;
642 
643 	vid_register(&sc->va);
644 
645 	return (0);
646 }
647 
648 static int
versatilefb_get_info(video_adapter_t * adp,int mode,video_info_t * info)649 versatilefb_get_info(video_adapter_t *adp, int mode, video_info_t *info)
650 {
651 	bcopy(&adp->va_info, info, sizeof(*info));
652 	return (0);
653 }
654 
655 static int
versatilefb_query_mode(video_adapter_t * adp,video_info_t * info)656 versatilefb_query_mode(video_adapter_t *adp, video_info_t *info)
657 {
658 	return (0);
659 }
660 
661 static int
versatilefb_set_mode(video_adapter_t * adp,int mode)662 versatilefb_set_mode(video_adapter_t *adp, int mode)
663 {
664 	return (0);
665 }
666 
667 static int
versatilefb_save_font(video_adapter_t * adp,int page,int size,int width,u_char * data,int c,int count)668 versatilefb_save_font(video_adapter_t *adp, int page, int size, int width,
669     u_char *data, int c, int count)
670 {
671 	return (0);
672 }
673 
674 static int
versatilefb_load_font(video_adapter_t * adp,int page,int size,int width,u_char * data,int c,int count)675 versatilefb_load_font(video_adapter_t *adp, int page, int size, int width,
676     u_char *data, int c, int count)
677 {
678 	struct video_adapter_softc *sc = (struct video_adapter_softc *)adp;
679 
680 	sc->font = data;
681 
682 	return (0);
683 }
684 
685 static int
versatilefb_show_font(video_adapter_t * adp,int page)686 versatilefb_show_font(video_adapter_t *adp, int page)
687 {
688 	return (0);
689 }
690 
691 static int
versatilefb_save_palette(video_adapter_t * adp,u_char * palette)692 versatilefb_save_palette(video_adapter_t *adp, u_char *palette)
693 {
694 	return (0);
695 }
696 
697 static int
versatilefb_load_palette(video_adapter_t * adp,u_char * palette)698 versatilefb_load_palette(video_adapter_t *adp, u_char *palette)
699 {
700 	return (0);
701 }
702 
703 static int
versatilefb_set_border(video_adapter_t * adp,int border)704 versatilefb_set_border(video_adapter_t *adp, int border)
705 {
706 	return (versatilefb_blank_display(adp, border));
707 }
708 
709 static int
versatilefb_save_state(video_adapter_t * adp,void * p,size_t size)710 versatilefb_save_state(video_adapter_t *adp, void *p, size_t size)
711 {
712 	return (0);
713 }
714 
715 static int
versatilefb_load_state(video_adapter_t * adp,void * p)716 versatilefb_load_state(video_adapter_t *adp, void *p)
717 {
718 	return (0);
719 }
720 
721 static int
versatilefb_set_win_org(video_adapter_t * adp,off_t offset)722 versatilefb_set_win_org(video_adapter_t *adp, off_t offset)
723 {
724 	return (0);
725 }
726 
727 static int
versatilefb_read_hw_cursor(video_adapter_t * adp,int * col,int * row)728 versatilefb_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
729 {
730 	*col = *row = 0;
731 
732 	return (0);
733 }
734 
735 static int
versatilefb_set_hw_cursor(video_adapter_t * adp,int col,int row)736 versatilefb_set_hw_cursor(video_adapter_t *adp, int col, int row)
737 {
738 
739 	return (0);
740 }
741 
742 static int
versatilefb_set_hw_cursor_shape(video_adapter_t * adp,int base,int height,int celsize,int blink)743 versatilefb_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
744     int celsize, int blink)
745 {
746 	return (0);
747 }
748 
749 static int
versatilefb_blank_display(video_adapter_t * adp,int mode)750 versatilefb_blank_display(video_adapter_t *adp, int mode)
751 {
752 
753 	struct video_adapter_softc *sc;
754 
755 	sc = (struct video_adapter_softc *)adp;
756 	if (sc && sc->fb_addr)
757 		memset((void*)sc->fb_addr, 0, sc->fb_size);
758 
759 	return (0);
760 }
761 
762 static int
versatilefb_mmap(video_adapter_t * adp,vm_ooffset_t offset,vm_paddr_t * paddr,int prot,vm_memattr_t * memattr)763 versatilefb_mmap(video_adapter_t *adp, vm_ooffset_t offset, vm_paddr_t *paddr,
764     int prot, vm_memattr_t *memattr)
765 {
766 	struct video_adapter_softc *sc;
767 
768 	sc = (struct video_adapter_softc *)adp;
769 
770 	/*
771 	 * This might be a legacy VGA mem request: if so, just point it at the
772 	 * framebuffer, since it shouldn't be touched
773 	 */
774 	if (offset < sc->stride*sc->height) {
775 		*paddr = sc->fb_addr + offset;
776 		return (0);
777 	}
778 
779 	return (EINVAL);
780 }
781 
782 static int
versatilefb_ioctl(video_adapter_t * adp,u_long cmd,caddr_t data)783 versatilefb_ioctl(video_adapter_t *adp, u_long cmd, caddr_t data)
784 {
785 
786 	return (0);
787 }
788 
789 static int
versatilefb_clear(video_adapter_t * adp)790 versatilefb_clear(video_adapter_t *adp)
791 {
792 
793 	return (versatilefb_blank_display(adp, 0));
794 }
795 
796 static int
versatilefb_fill_rect(video_adapter_t * adp,int val,int x,int y,int cx,int cy)797 versatilefb_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
798 {
799 
800 	return (0);
801 }
802 
803 static int
versatilefb_bitblt(video_adapter_t * adp,...)804 versatilefb_bitblt(video_adapter_t *adp, ...)
805 {
806 
807 	return (0);
808 }
809 
810 static int
versatilefb_diag(video_adapter_t * adp,int level)811 versatilefb_diag(video_adapter_t *adp, int level)
812 {
813 
814 	return (0);
815 }
816 
817 static int
versatilefb_save_cursor_palette(video_adapter_t * adp,u_char * palette)818 versatilefb_save_cursor_palette(video_adapter_t *adp, u_char *palette)
819 {
820 
821 	return (0);
822 }
823 
824 static int
versatilefb_load_cursor_palette(video_adapter_t * adp,u_char * palette)825 versatilefb_load_cursor_palette(video_adapter_t *adp, u_char *palette)
826 {
827 
828 	return (0);
829 }
830 
831 static int
versatilefb_copy(video_adapter_t * adp,vm_offset_t src,vm_offset_t dst,int n)832 versatilefb_copy(video_adapter_t *adp, vm_offset_t src, vm_offset_t dst, int n)
833 {
834 
835 	return (0);
836 }
837 
838 static int
versatilefb_putp(video_adapter_t * adp,vm_offset_t off,uint32_t p,uint32_t a,int size,int bpp,int bit_ltor,int byte_ltor)839 versatilefb_putp(video_adapter_t *adp, vm_offset_t off, uint32_t p, uint32_t a,
840     int size, int bpp, int bit_ltor, int byte_ltor)
841 {
842 
843 	return (0);
844 }
845 
846 static int
versatilefb_putc(video_adapter_t * adp,vm_offset_t off,uint8_t c,uint8_t a)847 versatilefb_putc(video_adapter_t *adp, vm_offset_t off, uint8_t c, uint8_t a)
848 {
849 	struct video_adapter_softc *sc;
850 	int row;
851 	int col;
852 	int i, j, k;
853 	uint8_t *addr;
854 	u_char *p;
855 	uint8_t fg, bg, color;
856 	uint16_t rgb;
857 
858 	sc = (struct video_adapter_softc *)adp;
859 
860 	if (sc->fb_addr == 0)
861 		return (0);
862 
863 	if (off >= adp->va_info.vi_width * adp->va_info.vi_height)
864 		return (0);
865 
866 	row = (off / adp->va_info.vi_width) * adp->va_info.vi_cheight;
867 	col = (off % adp->va_info.vi_width) * adp->va_info.vi_cwidth;
868 	p = sc->font + c*VERSATILE_FONT_HEIGHT;
869 	addr = (uint8_t *)sc->fb_addr
870 	    + (row + sc->ymargin)*(sc->stride)
871 	    + (sc->depth/8) * (col + sc->xmargin);
872 
873 	fg = a & 0xf ;
874 	bg = (a >> 4) & 0xf;
875 
876 	for (i = 0; i < VERSATILE_FONT_HEIGHT; i++) {
877 		for (j = 0, k = 7; j < 8; j++, k--) {
878 			if ((p[i] & (1 << k)) == 0)
879 				color = bg;
880 			else
881 				color = fg;
882 
883 			switch (sc->depth) {
884 			case 16:
885 				rgb = (versatilefb_palette[color].r >> 3) << 11;
886 				rgb |= (versatilefb_palette[color].g >> 2) << 5;
887 				rgb |= (versatilefb_palette[color].b >> 3);
888 				addr[2*j] = rgb & 0xff;
889 				addr[2*j + 1] = (rgb >> 8) & 0xff;
890 			default:
891 				/* Not supported yet */
892 				break;
893 			}
894 		}
895 
896 		addr += (sc->stride);
897 	}
898 
899         return (0);
900 }
901 
902 static int
versatilefb_puts(video_adapter_t * adp,vm_offset_t off,u_int16_t * s,int len)903 versatilefb_puts(video_adapter_t *adp, vm_offset_t off, u_int16_t *s, int len)
904 {
905 	int i;
906 
907 	for (i = 0; i < len; i++)
908 		versatilefb_putc(adp, off + i, s[i] & 0xff, (s[i] & 0xff00) >> 8);
909 
910 	return (0);
911 }
912 
913 static int
versatilefb_putm(video_adapter_t * adp,int x,int y,uint8_t * pixel_image,uint32_t pixel_mask,int size,int width)914 versatilefb_putm(video_adapter_t *adp, int x, int y, uint8_t *pixel_image,
915     uint32_t pixel_mask, int size, int width)
916 {
917 
918 	return (0);
919 }
920