xref: /freebsd/sys/arm/broadcom/bcm2835/bcm2835_fb.c (revision 952364486a4b9d135e4b28f7f88a8703a74eae6f)
1 /*-
2  * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org>
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/bio.h>
33 #include <sys/bus.h>
34 #include <sys/conf.h>
35 #include <sys/endian.h>
36 #include <sys/kernel.h>
37 #include <sys/kthread.h>
38 #include <sys/lock.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <sys/queue.h>
43 #include <sys/resource.h>
44 #include <sys/rman.h>
45 #include <sys/time.h>
46 #include <sys/timetc.h>
47 #include <sys/fbio.h>
48 #include <sys/consio.h>
49 
50 #include <sys/kdb.h>
51 
52 #include <machine/bus.h>
53 #include <machine/cpu.h>
54 #include <machine/cpufunc.h>
55 #include <machine/resource.h>
56 #include <machine/intr.h>
57 
58 #include <dev/fdt/fdt_common.h>
59 #include <dev/ofw/ofw_bus.h>
60 #include <dev/ofw/ofw_bus_subr.h>
61 
62 #include <dev/fb/fbreg.h>
63 #include <dev/syscons/syscons.h>
64 
65 #include <arm/broadcom/bcm2835/bcm2835_mbox.h>
66 #include <arm/broadcom/bcm2835/bcm2835_vcbus.h>
67 
68 #include "mbox_if.h"
69 
70 #define	BCMFB_FONT_HEIGHT	16
71 
72 struct argb {
73 	uint8_t		a;
74 	uint8_t		r;
75 	uint8_t		g;
76 	uint8_t		b;
77 };
78 
79 static struct argb bcmfb_palette[16] = {
80 	{0x00, 0x00, 0x00, 0x00},
81 	{0x00, 0x00, 0x00, 0xaa},
82 	{0x00, 0x00, 0xaa, 0x00},
83 	{0x00, 0x00, 0xaa, 0xaa},
84 	{0x00, 0xaa, 0x00, 0x00},
85 	{0x00, 0xaa, 0x00, 0xaa},
86 	{0x00, 0xaa, 0x55, 0x00},
87 	{0x00, 0xaa, 0xaa, 0xaa},
88 	{0x00, 0x55, 0x55, 0x55},
89 	{0x00, 0x55, 0x55, 0xff},
90 	{0x00, 0x55, 0xff, 0x55},
91 	{0x00, 0x55, 0xff, 0xff},
92 	{0x00, 0xff, 0x55, 0x55},
93 	{0x00, 0xff, 0x55, 0xff},
94 	{0x00, 0xff, 0xff, 0x55},
95 	{0x00, 0xff, 0xff, 0xff}
96 };
97 
98 /* mouse pointer from dev/syscons/scgfbrndr.c */
99 static u_char mouse_pointer[16] = {
100         0x00, 0x40, 0x60, 0x70, 0x78, 0x7c, 0x7e, 0x68,
101         0x0c, 0x0c, 0x06, 0x06, 0x00, 0x00, 0x00, 0x00
102 };
103 
104 #define FB_WIDTH		640
105 #define FB_HEIGHT		480
106 #define FB_DEPTH		24
107 
108 struct bcm_fb_config {
109 	uint32_t	xres;
110 	uint32_t	yres;
111 	uint32_t	vxres;
112 	uint32_t	vyres;
113 	uint32_t	pitch;
114 	uint32_t	bpp;
115 	uint32_t	xoffset;
116 	uint32_t	yoffset;
117 	/* Filled by videocore */
118 	uint32_t	base;
119 	uint32_t	screen_size;
120 };
121 
122 struct bcmsc_softc {
123 	device_t		dev;
124 	struct cdev *		cdev;
125 	struct mtx		mtx;
126 	bus_dma_tag_t		dma_tag;
127 	bus_dmamap_t		dma_map;
128 	struct bcm_fb_config*	fb_config;
129 	bus_addr_t		fb_config_phys;
130 	struct intr_config_hook	init_hook;
131 
132 };
133 
134 struct video_adapter_softc {
135 	/* Videoadpater part */
136 	video_adapter_t	va;
137 	int		console;
138 
139 	intptr_t	fb_addr;
140 	intptr_t	fb_paddr;
141 	unsigned int	fb_size;
142 
143 	unsigned int	height;
144 	unsigned int	width;
145 	unsigned int	depth;
146 	unsigned int	stride;
147 
148 	unsigned int	xmargin;
149 	unsigned int	ymargin;
150 
151 	unsigned char	*font;
152 	int		initialized;
153 };
154 
155 static struct bcmsc_softc *bcmsc_softc;
156 static struct video_adapter_softc va_softc;
157 
158 #define	bcm_fb_lock(_sc)	mtx_lock(&(_sc)->mtx)
159 #define	bcm_fb_unlock(_sc)	mtx_unlock(&(_sc)->mtx)
160 #define	bcm_fb_lock_assert(sc)	mtx_assert(&(_sc)->mtx, MA_OWNED)
161 
162 static int bcm_fb_probe(device_t);
163 static int bcm_fb_attach(device_t);
164 static void bcm_fb_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int err);
165 static void bcmfb_update_margins(video_adapter_t *adp);
166 static int bcmfb_configure(int);
167 
168 static void
169 bcm_fb_init(void *arg)
170 {
171 	struct bcmsc_softc *sc = arg;
172 	struct video_adapter_softc *va_sc = &va_softc;
173 	int err;
174 	volatile struct bcm_fb_config*	fb_config = sc->fb_config;
175 	phandle_t node;
176 	pcell_t cell;
177 	device_t mbox;
178 
179 	node = ofw_bus_get_node(sc->dev);
180 
181 	fb_config->xres = 0;
182 	fb_config->yres = 0;
183 	fb_config->bpp = 0;
184 
185 	if ((OF_getprop(node, "broadcom,width", &cell, sizeof(cell))) > 0)
186 		fb_config->xres = (int)fdt32_to_cpu(cell);
187 	if (fb_config->xres == 0)
188 		fb_config->xres = FB_WIDTH;
189 
190 	if ((OF_getprop(node, "broadcom,height", &cell, sizeof(cell))) > 0)
191 		fb_config->yres = (uint32_t)fdt32_to_cpu(cell);
192 	if (fb_config->yres == 0)
193 		fb_config->yres = FB_HEIGHT;
194 
195 	if ((OF_getprop(node, "broadcom,depth", &cell, sizeof(cell))) > 0)
196 		fb_config->bpp = (uint32_t)fdt32_to_cpu(cell);
197 	if (fb_config->bpp == 0)
198 		fb_config->bpp = FB_DEPTH;
199 
200 	fb_config->vxres = 0;
201 	fb_config->vyres = 0;
202 	fb_config->xoffset = 0;
203 	fb_config->yoffset = 0;
204 	fb_config->base = 0;
205 	fb_config->pitch = 0;
206 	fb_config->screen_size = 0;
207 
208 	bus_dmamap_sync(sc->dma_tag, sc->dma_map,
209 		BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
210 
211 	mbox = devclass_get_device(devclass_find("mbox"), 0);
212 	if (mbox) {
213 		MBOX_WRITE(mbox, BCM2835_MBOX_CHAN_FB, sc->fb_config_phys);
214 		MBOX_READ(mbox, BCM2835_MBOX_CHAN_FB, &err);
215 	}
216 	bus_dmamap_sync(sc->dma_tag, sc->dma_map,
217 		BUS_DMASYNC_POSTREAD);
218 
219 	if (fb_config->base != 0) {
220 		device_printf(sc->dev, "%dx%d(%dx%d@%d,%d) %dbpp\n",
221 			fb_config->xres, fb_config->yres,
222 			fb_config->vxres, fb_config->vyres,
223 			fb_config->xoffset, fb_config->yoffset,
224 			fb_config->bpp);
225 
226 
227 		device_printf(sc->dev, "pitch %d, base 0x%08x, screen_size %d\n",
228 			fb_config->pitch, fb_config->base,
229 			fb_config->screen_size);
230 
231 		va_sc->fb_addr = (intptr_t)pmap_mapdev(fb_config->base, fb_config->screen_size);
232 		va_sc->fb_paddr = fb_config->base;
233 		va_sc->fb_size = fb_config->screen_size;
234 		va_sc->depth = fb_config->bpp;
235 		va_sc->stride = fb_config->pitch;
236 
237 		va_sc->width = fb_config->xres;
238 		va_sc->height = fb_config->yres;
239 		bcmfb_update_margins(&va_sc->va);
240 	}
241 	else {
242 		device_printf(sc->dev, "Failed to set framebuffer info\n");
243 	}
244 
245 	config_intrhook_disestablish(&sc->init_hook);
246 }
247 
248 static int
249 bcm_fb_probe(device_t dev)
250 {
251 	int error = 0;
252 
253 	if (!ofw_bus_is_compatible(dev, "broadcom,bcm2835-fb"))
254 		return (ENXIO);
255 
256 	device_set_desc(dev, "BCM2835 framebuffer device");
257 
258 	error = sc_probe_unit(device_get_unit(dev),
259 	    device_get_flags(dev) | SC_AUTODETECT_KBD);
260 	if (error != 0)
261 		return (error);
262 
263 
264 	return (BUS_PROBE_DEFAULT);
265 }
266 
267 static int
268 bcm_fb_attach(device_t dev)
269 {
270 	struct bcmsc_softc *sc = device_get_softc(dev);
271 	int dma_size = sizeof(struct bcm_fb_config);
272 	int err;
273 
274 	if (bcmsc_softc)
275 		return (ENXIO);
276 
277 	bcmsc_softc = sc;
278 
279 	sc->dev = dev;
280 	mtx_init(&sc->mtx, "bcm2835fb", "fb", MTX_DEF);
281 
282 	err = bus_dma_tag_create(
283 	    bus_get_dma_tag(sc->dev),
284 	    PAGE_SIZE, 0,		/* alignment, boundary */
285 	    BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
286 	    BUS_SPACE_MAXADDR,		/* highaddr */
287 	    NULL, NULL,			/* filter, filterarg */
288 	    dma_size, 1,		/* maxsize, nsegments */
289 	    dma_size, 0,		/* maxsegsize, flags */
290 	    NULL, NULL,			/* lockfunc, lockarg */
291 	    &sc->dma_tag);
292 
293 	err = bus_dmamem_alloc(sc->dma_tag, (void **)&sc->fb_config,
294 	    0, &sc->dma_map);
295 	if (err) {
296 		device_printf(dev, "cannot allocate framebuffer\n");
297 		goto fail;
298 	}
299 
300 	err = bus_dmamap_load(sc->dma_tag, sc->dma_map, sc->fb_config,
301 	    dma_size, bcm_fb_dmamap_cb, &sc->fb_config_phys, BUS_DMA_NOWAIT);
302 
303 	if (err) {
304 		device_printf(dev, "cannot load DMA map\n");
305 		goto fail;
306 	}
307 
308 	err = (sc_attach_unit(device_get_unit(dev),
309 	    device_get_flags(dev) | SC_AUTODETECT_KBD));
310 
311 	if (err) {
312 		device_printf(dev, "failed to attach syscons\n");
313 		goto fail;
314 	}
315 
316 	/*
317 	 * We have to wait until interrupts are enabled.
318 	 * Mailbox relies on it to get data from VideoCore
319 	 */
320         sc->init_hook.ich_func = bcm_fb_init;
321         sc->init_hook.ich_arg = sc;
322 
323         if (config_intrhook_establish(&sc->init_hook) != 0) {
324 		device_printf(dev, "failed to establish intrhook\n");
325                 return (ENOMEM);
326 	}
327 
328 	return (0);
329 
330 fail:
331 	return (ENXIO);
332 }
333 
334 
335 static void
336 bcm_fb_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int err)
337 {
338 	bus_addr_t *addr;
339 
340 	if (err)
341 		return;
342 
343 	addr = (bus_addr_t*)arg;
344 	*addr = PHYS_TO_VCBUS(segs[0].ds_addr);
345 }
346 
347 static device_method_t bcm_fb_methods[] = {
348 	/* Device interface */
349 	DEVMETHOD(device_probe,		bcm_fb_probe),
350 	DEVMETHOD(device_attach,	bcm_fb_attach),
351 
352 	{ 0, 0 }
353 };
354 
355 static devclass_t bcm_fb_devclass;
356 
357 static driver_t bcm_fb_driver = {
358 	"fb",
359 	bcm_fb_methods,
360 	sizeof(struct bcmsc_softc),
361 };
362 
363 DRIVER_MODULE(bcm2835fb, ofwbus, bcm_fb_driver, bcm_fb_devclass, 0, 0);
364 
365 /*
366  * Video driver routines and glue.
367  */
368 static vi_probe_t		bcmfb_probe;
369 static vi_init_t		bcmfb_init;
370 static vi_get_info_t		bcmfb_get_info;
371 static vi_query_mode_t		bcmfb_query_mode;
372 static vi_set_mode_t		bcmfb_set_mode;
373 static vi_save_font_t		bcmfb_save_font;
374 static vi_load_font_t		bcmfb_load_font;
375 static vi_show_font_t		bcmfb_show_font;
376 static vi_save_palette_t	bcmfb_save_palette;
377 static vi_load_palette_t	bcmfb_load_palette;
378 static vi_set_border_t		bcmfb_set_border;
379 static vi_save_state_t		bcmfb_save_state;
380 static vi_load_state_t		bcmfb_load_state;
381 static vi_set_win_org_t		bcmfb_set_win_org;
382 static vi_read_hw_cursor_t	bcmfb_read_hw_cursor;
383 static vi_set_hw_cursor_t	bcmfb_set_hw_cursor;
384 static vi_set_hw_cursor_shape_t	bcmfb_set_hw_cursor_shape;
385 static vi_blank_display_t	bcmfb_blank_display;
386 static vi_mmap_t		bcmfb_mmap;
387 static vi_ioctl_t		bcmfb_ioctl;
388 static vi_clear_t		bcmfb_clear;
389 static vi_fill_rect_t		bcmfb_fill_rect;
390 static vi_bitblt_t		bcmfb_bitblt;
391 static vi_diag_t		bcmfb_diag;
392 static vi_save_cursor_palette_t	bcmfb_save_cursor_palette;
393 static vi_load_cursor_palette_t	bcmfb_load_cursor_palette;
394 static vi_copy_t		bcmfb_copy;
395 static vi_putp_t		bcmfb_putp;
396 static vi_putc_t		bcmfb_putc;
397 static vi_puts_t		bcmfb_puts;
398 static vi_putm_t		bcmfb_putm;
399 
400 static video_switch_t bcmfbvidsw = {
401 	.probe			= bcmfb_probe,
402 	.init			= bcmfb_init,
403 	.get_info		= bcmfb_get_info,
404 	.query_mode		= bcmfb_query_mode,
405 	.set_mode		= bcmfb_set_mode,
406 	.save_font		= bcmfb_save_font,
407 	.load_font		= bcmfb_load_font,
408 	.show_font		= bcmfb_show_font,
409 	.save_palette		= bcmfb_save_palette,
410 	.load_palette		= bcmfb_load_palette,
411 	.set_border		= bcmfb_set_border,
412 	.save_state		= bcmfb_save_state,
413 	.load_state		= bcmfb_load_state,
414 	.set_win_org		= bcmfb_set_win_org,
415 	.read_hw_cursor		= bcmfb_read_hw_cursor,
416 	.set_hw_cursor		= bcmfb_set_hw_cursor,
417 	.set_hw_cursor_shape	= bcmfb_set_hw_cursor_shape,
418 	.blank_display		= bcmfb_blank_display,
419 	.mmap			= bcmfb_mmap,
420 	.ioctl			= bcmfb_ioctl,
421 	.clear			= bcmfb_clear,
422 	.fill_rect		= bcmfb_fill_rect,
423 	.bitblt			= bcmfb_bitblt,
424 	.diag			= bcmfb_diag,
425 	.save_cursor_palette	= bcmfb_save_cursor_palette,
426 	.load_cursor_palette	= bcmfb_load_cursor_palette,
427 	.copy			= bcmfb_copy,
428 	.putp			= bcmfb_putp,
429 	.putc			= bcmfb_putc,
430 	.puts			= bcmfb_puts,
431 	.putm			= bcmfb_putm,
432 };
433 
434 VIDEO_DRIVER(bcmfb, bcmfbvidsw, bcmfb_configure);
435 
436 static vr_init_t bcmrend_init;
437 static vr_clear_t bcmrend_clear;
438 static vr_draw_border_t bcmrend_draw_border;
439 static vr_draw_t bcmrend_draw;
440 static vr_set_cursor_t bcmrend_set_cursor;
441 static vr_draw_cursor_t bcmrend_draw_cursor;
442 static vr_blink_cursor_t bcmrend_blink_cursor;
443 static vr_set_mouse_t bcmrend_set_mouse;
444 static vr_draw_mouse_t bcmrend_draw_mouse;
445 
446 /*
447  * We use our own renderer; this is because we must emulate a hardware
448  * cursor.
449  */
450 static sc_rndr_sw_t bcmrend = {
451 	bcmrend_init,
452 	bcmrend_clear,
453 	bcmrend_draw_border,
454 	bcmrend_draw,
455 	bcmrend_set_cursor,
456 	bcmrend_draw_cursor,
457 	bcmrend_blink_cursor,
458 	bcmrend_set_mouse,
459 	bcmrend_draw_mouse
460 };
461 
462 RENDERER(bcmfb, 0, bcmrend, gfb_set);
463 RENDERER_MODULE(bcmfb, gfb_set);
464 
465 static void
466 bcmrend_init(scr_stat* scp)
467 {
468 }
469 
470 static void
471 bcmrend_clear(scr_stat* scp, int c, int attr)
472 {
473 }
474 
475 static void
476 bcmrend_draw_border(scr_stat* scp, int color)
477 {
478 }
479 
480 static void
481 bcmrend_draw(scr_stat* scp, int from, int count, int flip)
482 {
483 	video_adapter_t* adp = scp->sc->adp;
484 	int i, c, a;
485 
486 	if (!flip) {
487 		/* Normal printing */
488 		vidd_puts(adp, from, (uint16_t*)sc_vtb_pointer(&scp->vtb, from), count);
489 	} else {
490 		/* This is for selections and such: invert the color attribute */
491 		for (i = count; i-- > 0; ++from) {
492 			c = sc_vtb_getc(&scp->vtb, from);
493 			a = sc_vtb_geta(&scp->vtb, from) >> 8;
494 			vidd_putc(adp, from, c, (a >> 4) | ((a & 0xf) << 4));
495 		}
496 	}
497 }
498 
499 static void
500 bcmrend_set_cursor(scr_stat* scp, int base, int height, int blink)
501 {
502 }
503 
504 static void
505 bcmrend_draw_cursor(scr_stat* scp, int off, int blink, int on, int flip)
506 {
507 	video_adapter_t* adp = scp->sc->adp;
508 	struct video_adapter_softc *sc;
509 	int row, col;
510 	uint8_t *addr;
511 	int i, j, bytes;
512 
513 	sc = (struct video_adapter_softc *)adp;
514 
515 	if (scp->curs_attr.height <= 0)
516 		return;
517 
518 	if (sc->fb_addr == 0)
519 		return;
520 
521 	if (off >= adp->va_info.vi_width * adp->va_info.vi_height)
522 		return;
523 
524 	/* calculate the coordinates in the video buffer */
525 	row = (off / adp->va_info.vi_width) * adp->va_info.vi_cheight;
526 	col = (off % adp->va_info.vi_width) * adp->va_info.vi_cwidth;
527 
528 	addr = (uint8_t *)sc->fb_addr
529 	    + (row + sc->ymargin)*(sc->stride)
530 	    + (sc->depth/8) * (col + sc->xmargin);
531 
532 	bytes = sc->depth/8;
533 
534 	/* our cursor consists of simply inverting the char under it */
535 	for (i = 0; i < adp->va_info.vi_cheight; i++) {
536 		for (j = 0; j < adp->va_info.vi_cwidth; j++) {
537 			switch (sc->depth) {
538 			case 32:
539 			case 24:
540 				addr[bytes*j + 2] ^= 0xff;
541 				/* FALLTHROUGH */
542 			case 16:
543 				addr[bytes*j + 1] ^= 0xff;
544 				addr[bytes*j] ^= 0xff;
545 				break;
546 			default:
547 				break;
548 			}
549 		}
550 
551 		addr += sc->stride;
552 	}
553 }
554 
555 static void
556 bcmrend_blink_cursor(scr_stat* scp, int at, int flip)
557 {
558 }
559 
560 static void
561 bcmrend_set_mouse(scr_stat* scp)
562 {
563 }
564 
565 static void
566 bcmrend_draw_mouse(scr_stat* scp, int x, int y, int on)
567 {
568 	vidd_putm(scp->sc->adp, x, y, mouse_pointer, 0xffffffff, 16, 8);
569 }
570 
571 static uint16_t bcmfb_static_window[ROW*COL];
572 extern u_char dflt_font_16[];
573 
574 /*
575  * Update videoadapter settings after changing resolution
576  */
577 static void
578 bcmfb_update_margins(video_adapter_t *adp)
579 {
580 	struct video_adapter_softc *sc;
581 	video_info_t *vi;
582 
583 	sc = (struct video_adapter_softc *)adp;
584 	vi = &adp->va_info;
585 
586 	sc->xmargin = (sc->width - (vi->vi_width * vi->vi_cwidth)) / 2;
587 	sc->ymargin = (sc->height - (vi->vi_height * vi->vi_cheight))/2;
588 }
589 
590 static int
591 bcmfb_configure(int flags)
592 {
593 	struct video_adapter_softc *va_sc;
594 
595 	va_sc = &va_softc;
596 	phandle_t display, root;
597 	pcell_t cell;
598 
599 	if (va_sc->initialized)
600 		return (0);
601 
602 	va_sc->width = 0;
603 	va_sc->height = 0;
604 
605 	/*
606 	 * It seems there is no way to let syscons framework know
607 	 * that framebuffer resolution has changed. So just try
608 	 * to fetch data from FDT and go with defaults if failed
609 	 */
610 	root = OF_finddevice("/");
611 	if ((root != 0) &&
612 	    (display = fdt_find_compatible(root, "broadcom,bcm2835-fb", 1))) {
613 		if ((OF_getprop(display, "broadcom,width",
614 		    &cell, sizeof(cell))) > 0)
615 			va_sc->width = (int)fdt32_to_cpu(cell);
616 
617 		if ((OF_getprop(display, "broadcom,height",
618 		    &cell, sizeof(cell))) > 0)
619 			va_sc->height = (int)fdt32_to_cpu(cell);
620 	}
621 
622 	if (va_sc->width == 0)
623 		va_sc->width = FB_WIDTH;
624 	if (va_sc->height == 0)
625 		va_sc->height = FB_HEIGHT;
626 
627 	bcmfb_init(0, &va_sc->va, 0);
628 
629 	va_sc->initialized = 1;
630 
631 	return (0);
632 }
633 
634 static int
635 bcmfb_probe(int unit, video_adapter_t **adp, void *arg, int flags)
636 {
637 
638 	return (0);
639 }
640 
641 static int
642 bcmfb_init(int unit, video_adapter_t *adp, int flags)
643 {
644 	struct video_adapter_softc *sc;
645 	video_info_t *vi;
646 
647 	sc = (struct video_adapter_softc *)adp;
648 	vi = &adp->va_info;
649 
650 	vid_init_struct(adp, "bcmfb", -1, unit);
651 
652 	sc->font = dflt_font_16;
653 	vi->vi_cheight = BCMFB_FONT_HEIGHT;
654 	vi->vi_cwidth = 8;
655 
656 	vi->vi_width = sc->width/8;
657 	vi->vi_height = sc->height/vi->vi_cheight;
658 
659 	/*
660 	 * Clamp width/height to syscons maximums
661 	 */
662 	if (vi->vi_width > COL)
663 		vi->vi_width = COL;
664 	if (vi->vi_height > ROW)
665 		vi->vi_height = ROW;
666 
667 	sc->xmargin = (sc->width - (vi->vi_width * vi->vi_cwidth)) / 2;
668 	sc->ymargin = (sc->height - (vi->vi_height * vi->vi_cheight))/2;
669 
670 
671 	adp->va_window = (vm_offset_t) bcmfb_static_window;
672 	adp->va_flags |= V_ADP_FONT /* | V_ADP_COLOR | V_ADP_MODECHANGE */;
673 
674 	vid_register(&sc->va);
675 
676 	return (0);
677 }
678 
679 static int
680 bcmfb_get_info(video_adapter_t *adp, int mode, video_info_t *info)
681 {
682 	bcopy(&adp->va_info, info, sizeof(*info));
683 	return (0);
684 }
685 
686 static int
687 bcmfb_query_mode(video_adapter_t *adp, video_info_t *info)
688 {
689 	return (0);
690 }
691 
692 static int
693 bcmfb_set_mode(video_adapter_t *adp, int mode)
694 {
695 	return (0);
696 }
697 
698 static int
699 bcmfb_save_font(video_adapter_t *adp, int page, int size, int width,
700     u_char *data, int c, int count)
701 {
702 	return (0);
703 }
704 
705 static int
706 bcmfb_load_font(video_adapter_t *adp, int page, int size, int width,
707     u_char *data, int c, int count)
708 {
709 	struct video_adapter_softc *sc = (struct video_adapter_softc *)adp;
710 
711 	sc->font = data;
712 
713 	return (0);
714 }
715 
716 static int
717 bcmfb_show_font(video_adapter_t *adp, int page)
718 {
719 	return (0);
720 }
721 
722 static int
723 bcmfb_save_palette(video_adapter_t *adp, u_char *palette)
724 {
725 	return (0);
726 }
727 
728 static int
729 bcmfb_load_palette(video_adapter_t *adp, u_char *palette)
730 {
731 	return (0);
732 }
733 
734 static int
735 bcmfb_set_border(video_adapter_t *adp, int border)
736 {
737 	return (bcmfb_blank_display(adp, border));
738 }
739 
740 static int
741 bcmfb_save_state(video_adapter_t *adp, void *p, size_t size)
742 {
743 	return (0);
744 }
745 
746 static int
747 bcmfb_load_state(video_adapter_t *adp, void *p)
748 {
749 	return (0);
750 }
751 
752 static int
753 bcmfb_set_win_org(video_adapter_t *adp, off_t offset)
754 {
755 	return (0);
756 }
757 
758 static int
759 bcmfb_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
760 {
761 	*col = *row = 0;
762 
763 	return (0);
764 }
765 
766 static int
767 bcmfb_set_hw_cursor(video_adapter_t *adp, int col, int row)
768 {
769 	return (0);
770 }
771 
772 static int
773 bcmfb_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
774     int celsize, int blink)
775 {
776 	return (0);
777 }
778 
779 static int
780 bcmfb_blank_display(video_adapter_t *adp, int mode)
781 {
782 
783 	struct video_adapter_softc *sc;
784 
785 	sc = (struct video_adapter_softc *)adp;
786 	if (sc && sc->fb_addr)
787 		memset((void*)sc->fb_addr, 0, sc->fb_size);
788 
789 	return (0);
790 }
791 
792 static int
793 bcmfb_mmap(video_adapter_t *adp, vm_ooffset_t offset, vm_paddr_t *paddr,
794     int prot, vm_memattr_t *memattr)
795 {
796 	struct video_adapter_softc *sc;
797 
798 	sc = (struct video_adapter_softc *)adp;
799 
800 	/*
801 	 * This might be a legacy VGA mem request: if so, just point it at the
802 	 * framebuffer, since it shouldn't be touched
803 	 */
804 	if (offset < sc->stride*sc->height) {
805 		*paddr = sc->fb_paddr + offset;
806 		return (0);
807 	}
808 
809 	return (EINVAL);
810 }
811 
812 static int
813 bcmfb_ioctl(video_adapter_t *adp, u_long cmd, caddr_t data)
814 {
815 	struct video_adapter_softc *sc;
816 	struct fbtype *fb;
817 
818 	sc = (struct video_adapter_softc *)adp;
819 
820 	switch (cmd) {
821 	case FBIOGTYPE:
822 		fb = (struct fbtype *)data;
823 		fb->fb_type = FBTYPE_PCIMISC;
824 		fb->fb_height = sc->height;
825 		fb->fb_width = sc->width;
826 		fb->fb_depth = sc->depth;
827 		if (sc->depth <= 1 || sc->depth > 8)
828 			fb->fb_cmsize = 0;
829 		else
830 			fb->fb_cmsize = 1 << sc->depth;
831 		fb->fb_size = sc->fb_size;
832 		break;
833 	default:
834 		return (fb_commonioctl(adp, cmd, data));
835 	}
836 
837 	return (0);
838 }
839 
840 static int
841 bcmfb_clear(video_adapter_t *adp)
842 {
843 
844 	return (bcmfb_blank_display(adp, 0));
845 }
846 
847 static int
848 bcmfb_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
849 {
850 
851 	return (0);
852 }
853 
854 static int
855 bcmfb_bitblt(video_adapter_t *adp, ...)
856 {
857 
858 	return (0);
859 }
860 
861 static int
862 bcmfb_diag(video_adapter_t *adp, int level)
863 {
864 
865 	return (0);
866 }
867 
868 static int
869 bcmfb_save_cursor_palette(video_adapter_t *adp, u_char *palette)
870 {
871 
872 	return (0);
873 }
874 
875 static int
876 bcmfb_load_cursor_palette(video_adapter_t *adp, u_char *palette)
877 {
878 
879 	return (0);
880 }
881 
882 static int
883 bcmfb_copy(video_adapter_t *adp, vm_offset_t src, vm_offset_t dst, int n)
884 {
885 
886 	return (0);
887 }
888 
889 static int
890 bcmfb_putp(video_adapter_t *adp, vm_offset_t off, uint32_t p, uint32_t a,
891     int size, int bpp, int bit_ltor, int byte_ltor)
892 {
893 
894 	return (0);
895 }
896 
897 static int
898 bcmfb_putc(video_adapter_t *adp, vm_offset_t off, uint8_t c, uint8_t a)
899 {
900 	struct video_adapter_softc *sc;
901 	int row;
902 	int col;
903 	int i, j, k;
904 	uint8_t *addr;
905 	u_char *p;
906 	uint8_t fg, bg, color;
907 	uint16_t rgb;
908 
909 	sc = (struct video_adapter_softc *)adp;
910 
911 	if (sc->fb_addr == 0)
912 		return (0);
913 
914 	row = (off / adp->va_info.vi_width) * adp->va_info.vi_cheight;
915 	col = (off % adp->va_info.vi_width) * adp->va_info.vi_cwidth;
916 	p = sc->font + c*BCMFB_FONT_HEIGHT;
917 	addr = (uint8_t *)sc->fb_addr
918 	    + (row + sc->ymargin)*(sc->stride)
919 	    + (sc->depth/8) * (col + sc->xmargin);
920 
921 	fg = a & 0xf ;
922 	bg = (a >> 4) & 0xf;
923 
924 	for (i = 0; i < BCMFB_FONT_HEIGHT; i++) {
925 		for (j = 0, k = 7; j < 8; j++, k--) {
926 			if ((p[i] & (1 << k)) == 0)
927 				color = bg;
928 			else
929 				color = fg;
930 
931 			switch (sc->depth) {
932 			case 32:
933 				addr[4*j+0] = bcmfb_palette[color].r;
934 				addr[4*j+1] = bcmfb_palette[color].g;
935 				addr[4*j+2] = bcmfb_palette[color].b;
936 				addr[4*j+3] = bcmfb_palette[color].a;
937 				break;
938 			case 24:
939 				addr[3*j] = bcmfb_palette[color].r;
940 				addr[3*j+1] = bcmfb_palette[color].g;
941 				addr[3*j+2] = bcmfb_palette[color].b;
942 				break;
943 			case 16:
944 				rgb = (bcmfb_palette[color].r >> 3) << 11;
945 				rgb |= (bcmfb_palette[color].g >> 2) << 5;
946 				rgb |= (bcmfb_palette[color].b >> 3);
947 				addr[2*j] = rgb & 0xff;
948 				addr[2*j + 1] = (rgb >> 8) & 0xff;
949 			default:
950 				/* Not supported yet */
951 				break;
952 			}
953 		}
954 
955 		addr += (sc->stride);
956 	}
957 
958         return (0);
959 }
960 
961 static int
962 bcmfb_puts(video_adapter_t *adp, vm_offset_t off, u_int16_t *s, int len)
963 {
964 	int i;
965 
966 	for (i = 0; i < len; i++)
967 		bcmfb_putc(adp, off + i, s[i] & 0xff, (s[i] & 0xff00) >> 8);
968 
969 	return (0);
970 }
971 
972 static int
973 bcmfb_putm(video_adapter_t *adp, int x, int y, uint8_t *pixel_image,
974     uint32_t pixel_mask, int size, int width)
975 {
976 
977 	return (0);
978 }
979 
980 /*
981  * Define a stub keyboard driver in case one hasn't been
982  * compiled into the kernel
983  */
984 #include <sys/kbio.h>
985 #include <dev/kbd/kbdreg.h>
986 
987 static int dummy_kbd_configure(int flags);
988 
989 keyboard_switch_t bcmdummysw;
990 
991 static int
992 dummy_kbd_configure(int flags)
993 {
994 
995 	return (0);
996 }
997 KEYBOARD_DRIVER(bcmdummy, bcmdummysw, dummy_kbd_configure);
998