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