bcm2835_fb.c revision 244762
191094Sdes/*-
2115619Sdes * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org>
3228690Sdes * All rights reserved.
491094Sdes *
591094Sdes * Redistribution and use in source and binary forms, with or without
691094Sdes * modification, are permitted provided that the following conditions
799158Sdes * are met:
899158Sdes * 1. Redistributions of source code must retain the above copyright
999158Sdes *    notice, this list of conditions and the following disclaimer.
1091094Sdes * 2. Redistributions in binary form must reproduce the above copyright
1191094Sdes *    notice, this list of conditions and the following disclaimer in the
1291094Sdes *    documentation and/or other materials provided with the distribution.
1391094Sdes *
1491094Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1591094Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1691094Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1791094Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1891094Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1991094Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2091094Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2191094Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2291094Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2391094Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2491094Sdes * SUCH DAMAGE.
2591094Sdes *
2691094Sdes */
2791094Sdes#include <sys/cdefs.h>
2891094Sdes__FBSDID("$FreeBSD: head/sys/arm/broadcom/bcm2835/bcm2835_fb.c 244762 2012-12-28 03:18:05Z gonzo $");
2991094Sdes
3091094Sdes#include <sys/param.h>
3191094Sdes#include <sys/systm.h>
3291094Sdes#include <sys/bio.h>
3391094Sdes#include <sys/bus.h>
3491094Sdes#include <sys/conf.h>
35255376Sdes#include <sys/endian.h>
3691094Sdes#include <sys/kernel.h>
3791094Sdes#include <sys/kthread.h>
38228690Sdes#include <sys/lock.h>
39228690Sdes#include <sys/malloc.h>
40228690Sdes#include <sys/module.h>
41228690Sdes#include <sys/mutex.h>
4291094Sdes#include <sys/queue.h>
4391094Sdes#include <sys/resource.h>
4491094Sdes#include <sys/rman.h>
4591094Sdes#include <sys/time.h>
4691094Sdes#include <sys/timetc.h>
4791094Sdes#include <sys/fbio.h>
4891094Sdes#include <sys/consio.h>
49255376Sdes
5091094Sdes#include <sys/kdb.h>
5191094Sdes
5291094Sdes#include <machine/bus.h>
5391094Sdes#include <machine/cpu.h>
5491094Sdes#include <machine/cpufunc.h>
5591094Sdes#include <machine/resource.h>
5691094Sdes#include <machine/frame.h>
5791094Sdes#include <machine/intr.h>
5891094Sdes
5991094Sdes#include <dev/fdt/fdt_common.h>
6091094Sdes#include <dev/ofw/ofw_bus.h>
6191094Sdes#include <dev/ofw/ofw_bus_subr.h>
6291094Sdes
6391094Sdes#include <dev/fb/fbreg.h>
6491094Sdes#include <dev/syscons/syscons.h>
6591094Sdes
6691094Sdes#include <arm/broadcom/bcm2835/bcm2835_mbox.h>
67107937Sdes#include <arm/broadcom/bcm2835/bcm2835_vcbus.h>
6891094Sdes
69107937Sdes#define	BCMFB_FONT_HEIGHT	16
7091094Sdes
7191094Sdesstruct argb {
7291094Sdes	uint8_t		a;
73107937Sdes	uint8_t		r;
7491094Sdes	uint8_t		g;
7591094Sdes	uint8_t		b;
76115619Sdes};
77107937Sdes
7891094Sdesstatic struct argb bcmfb_palette[16] = {
7991094Sdes	{0x00, 0x00, 0x00, 0x00},
80115619Sdes	{0x00, 0x00, 0x00, 0xaa},
81107937Sdes	{0x00, 0x00, 0xaa, 0x00},
8291094Sdes	{0x00, 0x00, 0xaa, 0xaa},
83115619Sdes	{0x00, 0xaa, 0x00, 0x00},
84107937Sdes	{0x00, 0xaa, 0x00, 0xaa},
8591094Sdes	{0x00, 0xaa, 0x55, 0x00},
8691100Sdes	{0x00, 0xaa, 0xaa, 0xaa},
8791100Sdes	{0x00, 0x55, 0x55, 0x55},
8891100Sdes	{0x00, 0x55, 0x55, 0xff},
8991100Sdes	{0x00, 0x55, 0xff, 0x55},
9091100Sdes	{0x00, 0x55, 0xff, 0xff},
9191100Sdes	{0x00, 0xff, 0x55, 0x55},
9291100Sdes	{0x00, 0xff, 0x55, 0xff},
9391100Sdes	{0x00, 0xff, 0xff, 0x55},
9491100Sdes	{0x00, 0xff, 0xff, 0xff}
9591100Sdes};
96236099Sdes
9791100Sdes/* mouse pointer from dev/syscons/scgfbrndr.c */
9891100Sdesstatic u_char mouse_pointer[16] = {
9991100Sdes        0x00, 0x40, 0x60, 0x70, 0x78, 0x7c, 0x7e, 0x68,
10091100Sdes        0x0c, 0x0c, 0x06, 0x06, 0x00, 0x00, 0x00, 0x00
10191100Sdes};
10291100Sdes
10391100Sdes#define FB_WIDTH		640
104#define FB_HEIGHT		480
105#define FB_DEPTH		24
106
107struct 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
121struct 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
133struct 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
153static struct bcmsc_softc *bcmsc_softc;
154static struct video_adapter_softc va_softc;
155
156#define	bcm_fb_lock(_sc)	mtx_lock(&(_sc)->mtx)
157#define	bcm_fb_unlock(_sc)	mtx_unlock(&(_sc)->mtx)
158#define	bcm_fb_lock_assert(sc)	mtx_assert(&(_sc)->mtx, MA_OWNED)
159
160static int bcm_fb_probe(device_t);
161static int bcm_fb_attach(device_t);
162static void bcm_fb_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int err);
163static void bcmfb_update_margins(video_adapter_t *adp);
164static int bcmfb_configure(int);
165
166static void
167bcm_fb_init(void *arg)
168{
169	struct bcmsc_softc *sc = arg;
170	struct video_adapter_softc *va_sc = &va_softc;
171	int err;
172	volatile struct bcm_fb_config*	fb_config = sc->fb_config;
173	phandle_t node;
174	pcell_t cell;
175
176	node = ofw_bus_get_node(sc->dev);
177
178	fb_config->xres = 0;
179	fb_config->yres = 0;
180	fb_config->bpp = 0;
181
182	if ((OF_getprop(node, "broadcom,width", &cell, sizeof(cell))) > 0)
183		fb_config->xres = (int)fdt32_to_cpu(cell);
184	if (fb_config->xres == 0)
185		fb_config->xres = FB_WIDTH;
186
187	if ((OF_getprop(node, "broadcom,height", &cell, sizeof(cell))) > 0)
188		fb_config->yres = (uint32_t)fdt32_to_cpu(cell);
189	if (fb_config->yres == 0)
190		fb_config->yres = FB_HEIGHT;
191
192	if ((OF_getprop(node, "broadcom,depth", &cell, sizeof(cell))) > 0)
193		fb_config->bpp = (uint32_t)fdt32_to_cpu(cell);
194	if (fb_config->bpp == 0)
195		fb_config->bpp = FB_DEPTH;
196
197	fb_config->vxres = 0;
198	fb_config->vyres = 0;
199	fb_config->xoffset = 0;
200	fb_config->yoffset = 0;
201	fb_config->base = 0;
202	fb_config->pitch = 0;
203	fb_config->screen_size = 0;
204
205	bus_dmamap_sync(sc->dma_tag, sc->dma_map,
206		BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
207	bcm_mbox_write(BCM2835_MBOX_CHAN_FB, sc->fb_config_phys);
208	bcm_mbox_read(BCM2835_MBOX_CHAN_FB, &err);
209	bus_dmamap_sync(sc->dma_tag, sc->dma_map,
210		BUS_DMASYNC_POSTREAD);
211
212	if (fb_config->base != 0) {
213		device_printf(sc->dev, "%dx%d(%dx%d@%d,%d) %dbpp\n",
214			fb_config->xres, fb_config->yres,
215			fb_config->vxres, fb_config->vyres,
216			fb_config->xoffset, fb_config->yoffset,
217			fb_config->bpp);
218
219
220		device_printf(sc->dev, "pitch %d, base 0x%08x, screen_size %d\n",
221			fb_config->pitch, fb_config->base,
222			fb_config->screen_size);
223
224		va_sc->fb_addr = (intptr_t)pmap_mapdev(fb_config->base, fb_config->screen_size);
225		va_sc->fb_size = fb_config->screen_size;
226		va_sc->depth = fb_config->bpp;
227		va_sc->stride = fb_config->pitch;
228
229		va_sc->width = fb_config->xres;
230		va_sc->height = fb_config->yres;
231		bcmfb_update_margins(&va_sc->va);
232	}
233	else {
234		device_printf(sc->dev, "Failed to set framebuffer info\n");
235		return;
236	}
237
238	config_intrhook_disestablish(&sc->init_hook);
239}
240
241static int
242bcm_fb_probe(device_t dev)
243{
244	int error = 0;
245
246	if (!ofw_bus_is_compatible(dev, "broadcom,bcm2835-fb"))
247		return (ENXIO);
248
249	device_set_desc(dev, "BCM2835 framebuffer device");
250
251	error = sc_probe_unit(device_get_unit(dev),
252	    device_get_flags(dev) | SC_AUTODETECT_KBD);
253	if (error != 0)
254		return (error);
255
256
257	return (BUS_PROBE_DEFAULT);
258}
259
260static int
261bcm_fb_attach(device_t dev)
262{
263	struct bcmsc_softc *sc = device_get_softc(dev);
264	int dma_size = sizeof(struct bcm_fb_config);
265	int err;
266
267	if (bcmsc_softc)
268		return (ENXIO);
269
270	bcmsc_softc = sc;
271
272	sc->dev = dev;
273	mtx_init(&sc->mtx, "bcm2835fb", "fb", MTX_DEF);
274
275	err = bus_dma_tag_create(
276	    bus_get_dma_tag(sc->dev),
277	    PAGE_SIZE, 0,		/* alignment, boundary */
278	    BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
279	    BUS_SPACE_MAXADDR,		/* highaddr */
280	    NULL, NULL,			/* filter, filterarg */
281	    dma_size, 1,		/* maxsize, nsegments */
282	    dma_size, 0,		/* maxsegsize, flags */
283	    NULL, NULL,			/* lockfunc, lockarg */
284	    &sc->dma_tag);
285
286	err = bus_dmamem_alloc(sc->dma_tag, (void **)&sc->fb_config,
287	    0, &sc->dma_map);
288	if (err) {
289		device_printf(dev, "cannot allocate framebuffer\n");
290		goto fail;
291	}
292
293	err = bus_dmamap_load(sc->dma_tag, sc->dma_map, sc->fb_config,
294	    dma_size, bcm_fb_dmamap_cb, &sc->fb_config_phys, BUS_DMA_NOWAIT);
295
296	if (err) {
297		device_printf(dev, "cannot load DMA map\n");
298		goto fail;
299	}
300
301	err = (sc_attach_unit(device_get_unit(dev),
302	    device_get_flags(dev) | SC_AUTODETECT_KBD));
303
304	if (err) {
305		device_printf(dev, "failed to attach syscons\n");
306		goto fail;
307	}
308
309	/*
310	 * We have to wait until interrupts are enabled.
311	 * Mailbox relies on it to get data from VideoCore
312	 */
313        sc->init_hook.ich_func = bcm_fb_init;
314        sc->init_hook.ich_arg = sc;
315
316        if (config_intrhook_establish(&sc->init_hook) != 0) {
317		device_printf(dev, "failed to establish intrhook\n");
318                return (ENOMEM);
319	}
320
321	return (0);
322
323fail:
324	return (ENXIO);
325}
326
327
328static void
329bcm_fb_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int err)
330{
331	bus_addr_t *addr;
332
333	if (err)
334		return;
335
336	addr = (bus_addr_t*)arg;
337	*addr = PHYS_TO_VCBUS(segs[0].ds_addr);
338}
339
340static device_method_t bcm_fb_methods[] = {
341	/* Device interface */
342	DEVMETHOD(device_probe,		bcm_fb_probe),
343	DEVMETHOD(device_attach,	bcm_fb_attach),
344
345	{ 0, 0 }
346};
347
348static devclass_t bcm_fb_devclass;
349
350static driver_t bcm_fb_driver = {
351	"fb",
352	bcm_fb_methods,
353	sizeof(struct bcmsc_softc),
354};
355
356DRIVER_MODULE(bcm2835fb, fdtbus, bcm_fb_driver, bcm_fb_devclass, 0, 0);
357
358/*
359 * Video driver routines and glue.
360 */
361static vi_probe_t		bcmfb_probe;
362static vi_init_t		bcmfb_init;
363static vi_get_info_t		bcmfb_get_info;
364static vi_query_mode_t		bcmfb_query_mode;
365static vi_set_mode_t		bcmfb_set_mode;
366static vi_save_font_t		bcmfb_save_font;
367static vi_load_font_t		bcmfb_load_font;
368static vi_show_font_t		bcmfb_show_font;
369static vi_save_palette_t	bcmfb_save_palette;
370static vi_load_palette_t	bcmfb_load_palette;
371static vi_set_border_t		bcmfb_set_border;
372static vi_save_state_t		bcmfb_save_state;
373static vi_load_state_t		bcmfb_load_state;
374static vi_set_win_org_t		bcmfb_set_win_org;
375static vi_read_hw_cursor_t	bcmfb_read_hw_cursor;
376static vi_set_hw_cursor_t	bcmfb_set_hw_cursor;
377static vi_set_hw_cursor_shape_t	bcmfb_set_hw_cursor_shape;
378static vi_blank_display_t	bcmfb_blank_display;
379static vi_mmap_t		bcmfb_mmap;
380static vi_ioctl_t		bcmfb_ioctl;
381static vi_clear_t		bcmfb_clear;
382static vi_fill_rect_t		bcmfb_fill_rect;
383static vi_bitblt_t		bcmfb_bitblt;
384static vi_diag_t		bcmfb_diag;
385static vi_save_cursor_palette_t	bcmfb_save_cursor_palette;
386static vi_load_cursor_palette_t	bcmfb_load_cursor_palette;
387static vi_copy_t		bcmfb_copy;
388static vi_putp_t		bcmfb_putp;
389static vi_putc_t		bcmfb_putc;
390static vi_puts_t		bcmfb_puts;
391static vi_putm_t		bcmfb_putm;
392
393static video_switch_t bcmfbvidsw = {
394	.probe			= bcmfb_probe,
395	.init			= bcmfb_init,
396	.get_info		= bcmfb_get_info,
397	.query_mode		= bcmfb_query_mode,
398	.set_mode		= bcmfb_set_mode,
399	.save_font		= bcmfb_save_font,
400	.load_font		= bcmfb_load_font,
401	.show_font		= bcmfb_show_font,
402	.save_palette		= bcmfb_save_palette,
403	.load_palette		= bcmfb_load_palette,
404	.set_border		= bcmfb_set_border,
405	.save_state		= bcmfb_save_state,
406	.load_state		= bcmfb_load_state,
407	.set_win_org		= bcmfb_set_win_org,
408	.read_hw_cursor		= bcmfb_read_hw_cursor,
409	.set_hw_cursor		= bcmfb_set_hw_cursor,
410	.set_hw_cursor_shape	= bcmfb_set_hw_cursor_shape,
411	.blank_display		= bcmfb_blank_display,
412	.mmap			= bcmfb_mmap,
413	.ioctl			= bcmfb_ioctl,
414	.clear			= bcmfb_clear,
415	.fill_rect		= bcmfb_fill_rect,
416	.bitblt			= bcmfb_bitblt,
417	.diag			= bcmfb_diag,
418	.save_cursor_palette	= bcmfb_save_cursor_palette,
419	.load_cursor_palette	= bcmfb_load_cursor_palette,
420	.copy			= bcmfb_copy,
421	.putp			= bcmfb_putp,
422	.putc			= bcmfb_putc,
423	.puts			= bcmfb_puts,
424	.putm			= bcmfb_putm,
425};
426
427VIDEO_DRIVER(bcmfb, bcmfbvidsw, bcmfb_configure);
428
429static vr_init_t bcmrend_init;
430static vr_clear_t bcmrend_clear;
431static vr_draw_border_t bcmrend_draw_border;
432static vr_draw_t bcmrend_draw;
433static vr_set_cursor_t bcmrend_set_cursor;
434static vr_draw_cursor_t bcmrend_draw_cursor;
435static vr_blink_cursor_t bcmrend_blink_cursor;
436static vr_set_mouse_t bcmrend_set_mouse;
437static vr_draw_mouse_t bcmrend_draw_mouse;
438
439/*
440 * We use our own renderer; this is because we must emulate a hardware
441 * cursor.
442 */
443static sc_rndr_sw_t bcmrend = {
444	bcmrend_init,
445	bcmrend_clear,
446	bcmrend_draw_border,
447	bcmrend_draw,
448	bcmrend_set_cursor,
449	bcmrend_draw_cursor,
450	bcmrend_blink_cursor,
451	bcmrend_set_mouse,
452	bcmrend_draw_mouse
453};
454
455RENDERER(bcmfb, 0, bcmrend, gfb_set);
456RENDERER_MODULE(bcmfb, gfb_set);
457
458static void
459bcmrend_init(scr_stat* scp)
460{
461}
462
463static void
464bcmrend_clear(scr_stat* scp, int c, int attr)
465{
466}
467
468static void
469bcmrend_draw_border(scr_stat* scp, int color)
470{
471}
472
473static void
474bcmrend_draw(scr_stat* scp, int from, int count, int flip)
475{
476	video_adapter_t* adp = scp->sc->adp;
477	int i, c, a;
478
479	if (!flip) {
480		/* Normal printing */
481		vidd_puts(adp, from, (uint16_t*)sc_vtb_pointer(&scp->vtb, from), count);
482	} else {
483		/* This is for selections and such: invert the color attribute */
484		for (i = count; i-- > 0; ++from) {
485			c = sc_vtb_getc(&scp->vtb, from);
486			a = sc_vtb_geta(&scp->vtb, from) >> 8;
487			vidd_putc(adp, from, c, (a >> 4) | ((a & 0xf) << 4));
488		}
489	}
490}
491
492static void
493bcmrend_set_cursor(scr_stat* scp, int base, int height, int blink)
494{
495}
496
497static void
498bcmrend_draw_cursor(scr_stat* scp, int off, int blink, int on, int flip)
499{
500	video_adapter_t* adp = scp->sc->adp;
501	struct video_adapter_softc *sc;
502	int row, col;
503	uint8_t *addr;
504	int i, j, bytes;
505
506	sc = (struct video_adapter_softc *)adp;
507
508	if (scp->curs_attr.height <= 0)
509		return;
510
511	if (sc->fb_addr == 0)
512		return;
513
514	if (off >= adp->va_info.vi_width * adp->va_info.vi_height)
515		return;
516
517	/* calculate the coordinates in the video buffer */
518	row = (off / adp->va_info.vi_width) * adp->va_info.vi_cheight;
519	col = (off % adp->va_info.vi_width) * adp->va_info.vi_cwidth;
520
521	addr = (uint8_t *)sc->fb_addr
522	    + (row + sc->ymargin)*(sc->stride)
523	    + (sc->depth/8) * (col + sc->xmargin);
524
525	bytes = sc->depth/8;
526
527	/* our cursor consists of simply inverting the char under it */
528	for (i = 0; i < adp->va_info.vi_cheight; i++) {
529		for (j = 0; j < adp->va_info.vi_cwidth; j++) {
530			switch (sc->depth) {
531			case 32:
532			case 24:
533				addr[bytes*j + 2] ^= 0xff;
534				/* FALLTHROUGH */
535			case 16:
536				addr[bytes*j + 1] ^= 0xff;
537				addr[bytes*j] ^= 0xff;
538				break;
539			default:
540				break;
541			}
542		}
543
544		addr += sc->stride;
545	}
546}
547
548static void
549bcmrend_blink_cursor(scr_stat* scp, int at, int flip)
550{
551}
552
553static void
554bcmrend_set_mouse(scr_stat* scp)
555{
556}
557
558static void
559bcmrend_draw_mouse(scr_stat* scp, int x, int y, int on)
560{
561	vidd_putm(scp->sc->adp, x, y, mouse_pointer, 0xffffffff, 16, 8);
562}
563
564static uint16_t bcmfb_static_window[ROW*COL];
565extern u_char dflt_font_16[];
566
567/*
568 * Update videoadapter settings after changing resolution
569 */
570static void
571bcmfb_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
583static int
584bcmfb_configure(int flags)
585{
586	struct video_adapter_softc *va_sc;
587
588	va_sc = &va_softc;
589	phandle_t display, root;
590	pcell_t cell;
591
592	if (va_sc->initialized)
593		return (0);
594
595	va_sc->width = 0;
596	va_sc->height = 0;
597
598	/*
599	 * It seems there is no way to let syscons framework know
600	 * that framebuffer resolution has changed. So just try
601	 * to fetch data from FDT and go with defaults if failed
602	 */
603	root = OF_finddevice("/");
604	if ((root != 0) &&
605	    (display = fdt_find_compatible(root, "broadcom,bcm2835-fb", 1))) {
606		if ((OF_getprop(display, "broadcom,width",
607		    &cell, sizeof(cell))) > 0)
608			va_sc->width = (int)fdt32_to_cpu(cell);
609
610		if ((OF_getprop(display, "broadcom,height",
611		    &cell, sizeof(cell))) > 0)
612			va_sc->height = (int)fdt32_to_cpu(cell);
613	}
614
615	if (va_sc->width == 0)
616		va_sc->width = FB_WIDTH;
617	if (va_sc->height == 0)
618		va_sc->height = FB_HEIGHT;
619
620	bcmfb_init(0, &va_sc->va, 0);
621
622	va_sc->initialized = 1;
623
624	return (0);
625}
626
627static int
628bcmfb_probe(int unit, video_adapter_t **adp, void *arg, int flags)
629{
630
631	return (0);
632}
633
634static int
635bcmfb_init(int unit, video_adapter_t *adp, int flags)
636{
637	struct video_adapter_softc *sc;
638	video_info_t *vi;
639
640	sc = (struct video_adapter_softc *)adp;
641	vi = &adp->va_info;
642
643	vid_init_struct(adp, "bcmfb", -1, unit);
644
645	sc->font = dflt_font_16;
646	vi->vi_cheight = BCMFB_FONT_HEIGHT;
647	vi->vi_cwidth = 8;
648
649	vi->vi_width = sc->width/8;
650	vi->vi_height = sc->height/vi->vi_cheight;
651
652	/*
653	 * Clamp width/height to syscons maximums
654	 */
655	if (vi->vi_width > COL)
656		vi->vi_width = COL;
657	if (vi->vi_height > ROW)
658		vi->vi_height = ROW;
659
660	sc->xmargin = (sc->width - (vi->vi_width * vi->vi_cwidth)) / 2;
661	sc->ymargin = (sc->height - (vi->vi_height * vi->vi_cheight))/2;
662
663
664	adp->va_window = (vm_offset_t) bcmfb_static_window;
665	adp->va_flags |= V_ADP_FONT /* | V_ADP_COLOR | V_ADP_MODECHANGE */;
666
667	vid_register(&sc->va);
668
669	return (0);
670}
671
672static int
673bcmfb_get_info(video_adapter_t *adp, int mode, video_info_t *info)
674{
675	bcopy(&adp->va_info, info, sizeof(*info));
676	return (0);
677}
678
679static int
680bcmfb_query_mode(video_adapter_t *adp, video_info_t *info)
681{
682	return (0);
683}
684
685static int
686bcmfb_set_mode(video_adapter_t *adp, int mode)
687{
688	return (0);
689}
690
691static int
692bcmfb_save_font(video_adapter_t *adp, int page, int size, int width,
693    u_char *data, int c, int count)
694{
695	return (0);
696}
697
698static int
699bcmfb_load_font(video_adapter_t *adp, int page, int size, int width,
700    u_char *data, int c, int count)
701{
702	struct video_adapter_softc *sc = (struct video_adapter_softc *)adp;
703
704	sc->font = data;
705
706	return (0);
707}
708
709static int
710bcmfb_show_font(video_adapter_t *adp, int page)
711{
712	return (0);
713}
714
715static int
716bcmfb_save_palette(video_adapter_t *adp, u_char *palette)
717{
718	return (0);
719}
720
721static int
722bcmfb_load_palette(video_adapter_t *adp, u_char *palette)
723{
724	return (0);
725}
726
727static int
728bcmfb_set_border(video_adapter_t *adp, int border)
729{
730	return (bcmfb_blank_display(adp, border));
731}
732
733static int
734bcmfb_save_state(video_adapter_t *adp, void *p, size_t size)
735{
736	return (0);
737}
738
739static int
740bcmfb_load_state(video_adapter_t *adp, void *p)
741{
742	return (0);
743}
744
745static int
746bcmfb_set_win_org(video_adapter_t *adp, off_t offset)
747{
748	return (0);
749}
750
751static int
752bcmfb_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
753{
754	*col = *row = 0;
755
756	return (0);
757}
758
759static int
760bcmfb_set_hw_cursor(video_adapter_t *adp, int col, int row)
761{
762	return (0);
763}
764
765static int
766bcmfb_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
767    int celsize, int blink)
768{
769	return (0);
770}
771
772static int
773bcmfb_blank_display(video_adapter_t *adp, int mode)
774{
775
776	struct video_adapter_softc *sc;
777
778	sc = (struct video_adapter_softc *)adp;
779	if (sc && sc->fb_addr)
780		memset((void*)sc->fb_addr, 0, sc->fb_size);
781
782	return (0);
783}
784
785static int
786bcmfb_mmap(video_adapter_t *adp, vm_ooffset_t offset, vm_paddr_t *paddr,
787    int prot, vm_memattr_t *memattr)
788{
789	struct video_adapter_softc *sc;
790
791	sc = (struct video_adapter_softc *)adp;
792
793	/*
794	 * This might be a legacy VGA mem request: if so, just point it at the
795	 * framebuffer, since it shouldn't be touched
796	 */
797	if (offset < sc->stride*sc->height) {
798		*paddr = sc->fb_addr + offset;
799		return (0);
800	}
801
802	return (EINVAL);
803}
804
805static int
806bcmfb_ioctl(video_adapter_t *adp, u_long cmd, caddr_t data)
807{
808
809	return (0);
810}
811
812static int
813bcmfb_clear(video_adapter_t *adp)
814{
815
816	return (bcmfb_blank_display(adp, 0));
817}
818
819static int
820bcmfb_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
821{
822
823	return (0);
824}
825
826static int
827bcmfb_bitblt(video_adapter_t *adp, ...)
828{
829
830	return (0);
831}
832
833static int
834bcmfb_diag(video_adapter_t *adp, int level)
835{
836
837	return (0);
838}
839
840static int
841bcmfb_save_cursor_palette(video_adapter_t *adp, u_char *palette)
842{
843
844	return (0);
845}
846
847static int
848bcmfb_load_cursor_palette(video_adapter_t *adp, u_char *palette)
849{
850
851	return (0);
852}
853
854static int
855bcmfb_copy(video_adapter_t *adp, vm_offset_t src, vm_offset_t dst, int n)
856{
857
858	return (0);
859}
860
861static int
862bcmfb_putp(video_adapter_t *adp, vm_offset_t off, uint32_t p, uint32_t a,
863    int size, int bpp, int bit_ltor, int byte_ltor)
864{
865
866	return (0);
867}
868
869static int
870bcmfb_putc(video_adapter_t *adp, vm_offset_t off, uint8_t c, uint8_t a)
871{
872	struct video_adapter_softc *sc;
873	int row;
874	int col;
875	int i, j, k;
876	uint8_t *addr;
877	u_char *p;
878	uint8_t fg, bg, color;
879	uint16_t rgb;
880
881	sc = (struct video_adapter_softc *)adp;
882
883	if (sc->fb_addr == 0)
884		return (0);
885
886	row = (off / adp->va_info.vi_width) * adp->va_info.vi_cheight;
887	col = (off % adp->va_info.vi_width) * adp->va_info.vi_cwidth;
888	p = sc->font + c*BCMFB_FONT_HEIGHT;
889	addr = (uint8_t *)sc->fb_addr
890	    + (row + sc->ymargin)*(sc->stride)
891	    + (sc->depth/8) * (col + sc->xmargin);
892
893	fg = a & 0xf ;
894	bg = (a >> 8) & 0xf;
895
896	for (i = 0; i < BCMFB_FONT_HEIGHT; i++) {
897		for (j = 0, k = 7; j < 8; j++, k--) {
898			if ((p[i] & (1 << k)) == 0)
899				color = bg;
900			else
901				color = fg;
902
903			switch (sc->depth) {
904			case 32:
905				addr[4*j+0] = bcmfb_palette[color].r;
906				addr[4*j+1] = bcmfb_palette[color].g;
907				addr[4*j+2] = bcmfb_palette[color].b;
908				addr[4*j+3] = bcmfb_palette[color].a;
909				break;
910			case 24:
911				addr[3*j] = bcmfb_palette[color].r;
912				addr[3*j+1] = bcmfb_palette[color].g;
913				addr[3*j+2] = bcmfb_palette[color].b;
914				break;
915			case 16:
916				rgb = (bcmfb_palette[color].r >> 3) << 11;
917				rgb |= (bcmfb_palette[color].g >> 2) << 5;
918				rgb |= (bcmfb_palette[color].b >> 3);
919				addr[2*j] = rgb & 0xff;
920				addr[2*j + 1] = (rgb >> 8) & 0xff;
921			default:
922				/* Not supported yet */
923				break;
924			}
925		}
926
927		addr += (sc->stride);
928	}
929
930        return (0);
931}
932
933static int
934bcmfb_puts(video_adapter_t *adp, vm_offset_t off, u_int16_t *s, int len)
935{
936	int i;
937
938	for (i = 0; i < len; i++)
939		bcmfb_putc(adp, off + i, s[i] & 0xff, (s[i] & 0xff00) >> 8);
940
941	return (0);
942}
943
944static int
945bcmfb_putm(video_adapter_t *adp, int x, int y, uint8_t *pixel_image,
946    uint32_t pixel_mask, int size, int width)
947{
948
949	return (0);
950}
951
952/*
953 * Define a stub keyboard driver in case one hasn't been
954 * compiled into the kernel
955 */
956#include <sys/kbio.h>
957#include <dev/kbd/kbdreg.h>
958
959static int dummy_kbd_configure(int flags);
960
961keyboard_switch_t bcmdummysw;
962
963static int
964dummy_kbd_configure(int flags)
965{
966
967	return (0);
968}
969KEYBOARD_DRIVER(bcmdummy, bcmdummysw, dummy_kbd_configure);
970