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