bcm2835_fb.c revision 257200
1219820Sjeff/*-
2219820Sjeff * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org>
3219820Sjeff * All rights reserved.
4219820Sjeff *
5219820Sjeff * Redistribution and use in source and binary forms, with or without
6219820Sjeff * modification, are permitted provided that the following conditions
7219820Sjeff * are met:
8219820Sjeff * 1. Redistributions of source code must retain the above copyright
9219820Sjeff *    notice, this list of conditions and the following disclaimer.
10219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright
11219820Sjeff *    notice, this list of conditions and the following disclaimer in the
12219820Sjeff *    documentation and/or other materials provided with the distribution.
13219820Sjeff *
14219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15219820Sjeff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16219820Sjeff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17219820Sjeff * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18219820Sjeff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19219820Sjeff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20219820Sjeff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21219820Sjeff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22219820Sjeff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23219820Sjeff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24219820Sjeff * SUCH DAMAGE.
25219820Sjeff *
26219820Sjeff */
27219820Sjeff#include <sys/cdefs.h>
28219820Sjeff__FBSDID("$FreeBSD: head/sys/arm/broadcom/bcm2835/bcm2835_fb.c 257200 2013-10-27 01:34:10Z ian $");
29219820Sjeff
30219820Sjeff#include <sys/param.h>
31219820Sjeff#include <sys/systm.h>
32219820Sjeff#include <sys/bio.h>
33219820Sjeff#include <sys/bus.h>
34219820Sjeff#include <sys/conf.h>
35219820Sjeff#include <sys/endian.h>
36219820Sjeff#include <sys/kernel.h>
37219820Sjeff#include <sys/kthread.h>
38219820Sjeff#include <sys/lock.h>
39219820Sjeff#include <sys/malloc.h>
40219820Sjeff#include <sys/module.h>
41219820Sjeff#include <sys/mutex.h>
42219820Sjeff#include <sys/queue.h>
43219820Sjeff#include <sys/resource.h>
44219820Sjeff#include <sys/rman.h>
45219820Sjeff#include <sys/time.h>
46219820Sjeff#include <sys/timetc.h>
47219820Sjeff#include <sys/fbio.h>
48219820Sjeff#include <sys/consio.h>
49219820Sjeff
50219820Sjeff#include <sys/kdb.h>
51219820Sjeff
52219820Sjeff#include <machine/bus.h>
53219820Sjeff#include <machine/cpu.h>
54219820Sjeff#include <machine/cpufunc.h>
55219820Sjeff#include <machine/resource.h>
56219820Sjeff#include <machine/intr.h>
57219820Sjeff
58219820Sjeff#include <dev/fdt/fdt_common.h>
59219820Sjeff#include <dev/ofw/ofw_bus.h>
60219820Sjeff#include <dev/ofw/ofw_bus_subr.h>
61219820Sjeff
62219820Sjeff#include <dev/fb/fbreg.h>
63219820Sjeff#include <dev/syscons/syscons.h>
64219820Sjeff
65219820Sjeff#include <arm/broadcom/bcm2835/bcm2835_mbox.h>
66219820Sjeff#include <arm/broadcom/bcm2835/bcm2835_vcbus.h>
67219820Sjeff
68219820Sjeff#include "mbox_if.h"
69219820Sjeff
70219820Sjeff#define	BCMFB_FONT_HEIGHT	16
71219820Sjeff
72219820Sjeffstruct argb {
73219820Sjeff	uint8_t		a;
74219820Sjeff	uint8_t		r;
75219820Sjeff	uint8_t		g;
76219820Sjeff	uint8_t		b;
77219820Sjeff};
78219820Sjeff
79219820Sjeffstatic 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 */
99static 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
108struct 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
122struct 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
134struct 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
155static struct bcmsc_softc *bcmsc_softc;
156static 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
162static int bcm_fb_probe(device_t);
163static int bcm_fb_attach(device_t);
164static void bcm_fb_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int err);
165static void bcmfb_update_margins(video_adapter_t *adp);
166static int bcmfb_configure(int);
167
168static void
169bcm_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		return;
244	}
245
246	config_intrhook_disestablish(&sc->init_hook);
247}
248
249static int
250bcm_fb_probe(device_t dev)
251{
252	int error = 0;
253
254	if (!ofw_bus_is_compatible(dev, "broadcom,bcm2835-fb"))
255		return (ENXIO);
256
257	device_set_desc(dev, "BCM2835 framebuffer device");
258
259	error = sc_probe_unit(device_get_unit(dev),
260	    device_get_flags(dev) | SC_AUTODETECT_KBD);
261	if (error != 0)
262		return (error);
263
264
265	return (BUS_PROBE_DEFAULT);
266}
267
268static int
269bcm_fb_attach(device_t dev)
270{
271	struct bcmsc_softc *sc = device_get_softc(dev);
272	int dma_size = sizeof(struct bcm_fb_config);
273	int err;
274
275	if (bcmsc_softc)
276		return (ENXIO);
277
278	bcmsc_softc = sc;
279
280	sc->dev = dev;
281	mtx_init(&sc->mtx, "bcm2835fb", "fb", MTX_DEF);
282
283	err = bus_dma_tag_create(
284	    bus_get_dma_tag(sc->dev),
285	    PAGE_SIZE, 0,		/* alignment, boundary */
286	    BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
287	    BUS_SPACE_MAXADDR,		/* highaddr */
288	    NULL, NULL,			/* filter, filterarg */
289	    dma_size, 1,		/* maxsize, nsegments */
290	    dma_size, 0,		/* maxsegsize, flags */
291	    NULL, NULL,			/* lockfunc, lockarg */
292	    &sc->dma_tag);
293
294	err = bus_dmamem_alloc(sc->dma_tag, (void **)&sc->fb_config,
295	    0, &sc->dma_map);
296	if (err) {
297		device_printf(dev, "cannot allocate framebuffer\n");
298		goto fail;
299	}
300
301	err = bus_dmamap_load(sc->dma_tag, sc->dma_map, sc->fb_config,
302	    dma_size, bcm_fb_dmamap_cb, &sc->fb_config_phys, BUS_DMA_NOWAIT);
303
304	if (err) {
305		device_printf(dev, "cannot load DMA map\n");
306		goto fail;
307	}
308
309	err = (sc_attach_unit(device_get_unit(dev),
310	    device_get_flags(dev) | SC_AUTODETECT_KBD));
311
312	if (err) {
313		device_printf(dev, "failed to attach syscons\n");
314		goto fail;
315	}
316
317	/*
318	 * We have to wait until interrupts are enabled.
319	 * Mailbox relies on it to get data from VideoCore
320	 */
321        sc->init_hook.ich_func = bcm_fb_init;
322        sc->init_hook.ich_arg = sc;
323
324        if (config_intrhook_establish(&sc->init_hook) != 0) {
325		device_printf(dev, "failed to establish intrhook\n");
326                return (ENOMEM);
327	}
328
329	return (0);
330
331fail:
332	return (ENXIO);
333}
334
335
336static void
337bcm_fb_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int err)
338{
339	bus_addr_t *addr;
340
341	if (err)
342		return;
343
344	addr = (bus_addr_t*)arg;
345	*addr = PHYS_TO_VCBUS(segs[0].ds_addr);
346}
347
348static device_method_t bcm_fb_methods[] = {
349	/* Device interface */
350	DEVMETHOD(device_probe,		bcm_fb_probe),
351	DEVMETHOD(device_attach,	bcm_fb_attach),
352
353	{ 0, 0 }
354};
355
356static devclass_t bcm_fb_devclass;
357
358static driver_t bcm_fb_driver = {
359	"fb",
360	bcm_fb_methods,
361	sizeof(struct bcmsc_softc),
362};
363
364DRIVER_MODULE(bcm2835fb, fdtbus, bcm_fb_driver, bcm_fb_devclass, 0, 0);
365
366/*
367 * Video driver routines and glue.
368 */
369static vi_probe_t		bcmfb_probe;
370static vi_init_t		bcmfb_init;
371static vi_get_info_t		bcmfb_get_info;
372static vi_query_mode_t		bcmfb_query_mode;
373static vi_set_mode_t		bcmfb_set_mode;
374static vi_save_font_t		bcmfb_save_font;
375static vi_load_font_t		bcmfb_load_font;
376static vi_show_font_t		bcmfb_show_font;
377static vi_save_palette_t	bcmfb_save_palette;
378static vi_load_palette_t	bcmfb_load_palette;
379static vi_set_border_t		bcmfb_set_border;
380static vi_save_state_t		bcmfb_save_state;
381static vi_load_state_t		bcmfb_load_state;
382static vi_set_win_org_t		bcmfb_set_win_org;
383static vi_read_hw_cursor_t	bcmfb_read_hw_cursor;
384static vi_set_hw_cursor_t	bcmfb_set_hw_cursor;
385static vi_set_hw_cursor_shape_t	bcmfb_set_hw_cursor_shape;
386static vi_blank_display_t	bcmfb_blank_display;
387static vi_mmap_t		bcmfb_mmap;
388static vi_ioctl_t		bcmfb_ioctl;
389static vi_clear_t		bcmfb_clear;
390static vi_fill_rect_t		bcmfb_fill_rect;
391static vi_bitblt_t		bcmfb_bitblt;
392static vi_diag_t		bcmfb_diag;
393static vi_save_cursor_palette_t	bcmfb_save_cursor_palette;
394static vi_load_cursor_palette_t	bcmfb_load_cursor_palette;
395static vi_copy_t		bcmfb_copy;
396static vi_putp_t		bcmfb_putp;
397static vi_putc_t		bcmfb_putc;
398static vi_puts_t		bcmfb_puts;
399static vi_putm_t		bcmfb_putm;
400
401static video_switch_t bcmfbvidsw = {
402	.probe			= bcmfb_probe,
403	.init			= bcmfb_init,
404	.get_info		= bcmfb_get_info,
405	.query_mode		= bcmfb_query_mode,
406	.set_mode		= bcmfb_set_mode,
407	.save_font		= bcmfb_save_font,
408	.load_font		= bcmfb_load_font,
409	.show_font		= bcmfb_show_font,
410	.save_palette		= bcmfb_save_palette,
411	.load_palette		= bcmfb_load_palette,
412	.set_border		= bcmfb_set_border,
413	.save_state		= bcmfb_save_state,
414	.load_state		= bcmfb_load_state,
415	.set_win_org		= bcmfb_set_win_org,
416	.read_hw_cursor		= bcmfb_read_hw_cursor,
417	.set_hw_cursor		= bcmfb_set_hw_cursor,
418	.set_hw_cursor_shape	= bcmfb_set_hw_cursor_shape,
419	.blank_display		= bcmfb_blank_display,
420	.mmap			= bcmfb_mmap,
421	.ioctl			= bcmfb_ioctl,
422	.clear			= bcmfb_clear,
423	.fill_rect		= bcmfb_fill_rect,
424	.bitblt			= bcmfb_bitblt,
425	.diag			= bcmfb_diag,
426	.save_cursor_palette	= bcmfb_save_cursor_palette,
427	.load_cursor_palette	= bcmfb_load_cursor_palette,
428	.copy			= bcmfb_copy,
429	.putp			= bcmfb_putp,
430	.putc			= bcmfb_putc,
431	.puts			= bcmfb_puts,
432	.putm			= bcmfb_putm,
433};
434
435VIDEO_DRIVER(bcmfb, bcmfbvidsw, bcmfb_configure);
436
437static vr_init_t bcmrend_init;
438static vr_clear_t bcmrend_clear;
439static vr_draw_border_t bcmrend_draw_border;
440static vr_draw_t bcmrend_draw;
441static vr_set_cursor_t bcmrend_set_cursor;
442static vr_draw_cursor_t bcmrend_draw_cursor;
443static vr_blink_cursor_t bcmrend_blink_cursor;
444static vr_set_mouse_t bcmrend_set_mouse;
445static vr_draw_mouse_t bcmrend_draw_mouse;
446
447/*
448 * We use our own renderer; this is because we must emulate a hardware
449 * cursor.
450 */
451static sc_rndr_sw_t bcmrend = {
452	bcmrend_init,
453	bcmrend_clear,
454	bcmrend_draw_border,
455	bcmrend_draw,
456	bcmrend_set_cursor,
457	bcmrend_draw_cursor,
458	bcmrend_blink_cursor,
459	bcmrend_set_mouse,
460	bcmrend_draw_mouse
461};
462
463RENDERER(bcmfb, 0, bcmrend, gfb_set);
464RENDERER_MODULE(bcmfb, gfb_set);
465
466static void
467bcmrend_init(scr_stat* scp)
468{
469}
470
471static void
472bcmrend_clear(scr_stat* scp, int c, int attr)
473{
474}
475
476static void
477bcmrend_draw_border(scr_stat* scp, int color)
478{
479}
480
481static void
482bcmrend_draw(scr_stat* scp, int from, int count, int flip)
483{
484	video_adapter_t* adp = scp->sc->adp;
485	int i, c, a;
486
487	if (!flip) {
488		/* Normal printing */
489		vidd_puts(adp, from, (uint16_t*)sc_vtb_pointer(&scp->vtb, from), count);
490	} else {
491		/* This is for selections and such: invert the color attribute */
492		for (i = count; i-- > 0; ++from) {
493			c = sc_vtb_getc(&scp->vtb, from);
494			a = sc_vtb_geta(&scp->vtb, from) >> 8;
495			vidd_putc(adp, from, c, (a >> 4) | ((a & 0xf) << 4));
496		}
497	}
498}
499
500static void
501bcmrend_set_cursor(scr_stat* scp, int base, int height, int blink)
502{
503}
504
505static void
506bcmrend_draw_cursor(scr_stat* scp, int off, int blink, int on, int flip)
507{
508	video_adapter_t* adp = scp->sc->adp;
509	struct video_adapter_softc *sc;
510	int row, col;
511	uint8_t *addr;
512	int i, j, bytes;
513
514	sc = (struct video_adapter_softc *)adp;
515
516	if (scp->curs_attr.height <= 0)
517		return;
518
519	if (sc->fb_addr == 0)
520		return;
521
522	if (off >= adp->va_info.vi_width * adp->va_info.vi_height)
523		return;
524
525	/* calculate the coordinates in the video buffer */
526	row = (off / adp->va_info.vi_width) * adp->va_info.vi_cheight;
527	col = (off % adp->va_info.vi_width) * adp->va_info.vi_cwidth;
528
529	addr = (uint8_t *)sc->fb_addr
530	    + (row + sc->ymargin)*(sc->stride)
531	    + (sc->depth/8) * (col + sc->xmargin);
532
533	bytes = sc->depth/8;
534
535	/* our cursor consists of simply inverting the char under it */
536	for (i = 0; i < adp->va_info.vi_cheight; i++) {
537		for (j = 0; j < adp->va_info.vi_cwidth; j++) {
538			switch (sc->depth) {
539			case 32:
540			case 24:
541				addr[bytes*j + 2] ^= 0xff;
542				/* FALLTHROUGH */
543			case 16:
544				addr[bytes*j + 1] ^= 0xff;
545				addr[bytes*j] ^= 0xff;
546				break;
547			default:
548				break;
549			}
550		}
551
552		addr += sc->stride;
553	}
554}
555
556static void
557bcmrend_blink_cursor(scr_stat* scp, int at, int flip)
558{
559}
560
561static void
562bcmrend_set_mouse(scr_stat* scp)
563{
564}
565
566static void
567bcmrend_draw_mouse(scr_stat* scp, int x, int y, int on)
568{
569	vidd_putm(scp->sc->adp, x, y, mouse_pointer, 0xffffffff, 16, 8);
570}
571
572static uint16_t bcmfb_static_window[ROW*COL];
573extern u_char dflt_font_16[];
574
575/*
576 * Update videoadapter settings after changing resolution
577 */
578static void
579bcmfb_update_margins(video_adapter_t *adp)
580{
581	struct video_adapter_softc *sc;
582	video_info_t *vi;
583
584	sc = (struct video_adapter_softc *)adp;
585	vi = &adp->va_info;
586
587	sc->xmargin = (sc->width - (vi->vi_width * vi->vi_cwidth)) / 2;
588	sc->ymargin = (sc->height - (vi->vi_height * vi->vi_cheight))/2;
589}
590
591static int
592bcmfb_configure(int flags)
593{
594	struct video_adapter_softc *va_sc;
595
596	va_sc = &va_softc;
597	phandle_t display, root;
598	pcell_t cell;
599
600	if (va_sc->initialized)
601		return (0);
602
603	va_sc->width = 0;
604	va_sc->height = 0;
605
606	/*
607	 * It seems there is no way to let syscons framework know
608	 * that framebuffer resolution has changed. So just try
609	 * to fetch data from FDT and go with defaults if failed
610	 */
611	root = OF_finddevice("/");
612	if ((root != 0) &&
613	    (display = fdt_find_compatible(root, "broadcom,bcm2835-fb", 1))) {
614		if ((OF_getprop(display, "broadcom,width",
615		    &cell, sizeof(cell))) > 0)
616			va_sc->width = (int)fdt32_to_cpu(cell);
617
618		if ((OF_getprop(display, "broadcom,height",
619		    &cell, sizeof(cell))) > 0)
620			va_sc->height = (int)fdt32_to_cpu(cell);
621	}
622
623	if (va_sc->width == 0)
624		va_sc->width = FB_WIDTH;
625	if (va_sc->height == 0)
626		va_sc->height = FB_HEIGHT;
627
628	bcmfb_init(0, &va_sc->va, 0);
629
630	va_sc->initialized = 1;
631
632	return (0);
633}
634
635static int
636bcmfb_probe(int unit, video_adapter_t **adp, void *arg, int flags)
637{
638
639	return (0);
640}
641
642static int
643bcmfb_init(int unit, video_adapter_t *adp, int flags)
644{
645	struct video_adapter_softc *sc;
646	video_info_t *vi;
647
648	sc = (struct video_adapter_softc *)adp;
649	vi = &adp->va_info;
650
651	vid_init_struct(adp, "bcmfb", -1, unit);
652
653	sc->font = dflt_font_16;
654	vi->vi_cheight = BCMFB_FONT_HEIGHT;
655	vi->vi_cwidth = 8;
656
657	vi->vi_width = sc->width/8;
658	vi->vi_height = sc->height/vi->vi_cheight;
659
660	/*
661	 * Clamp width/height to syscons maximums
662	 */
663	if (vi->vi_width > COL)
664		vi->vi_width = COL;
665	if (vi->vi_height > ROW)
666		vi->vi_height = ROW;
667
668	sc->xmargin = (sc->width - (vi->vi_width * vi->vi_cwidth)) / 2;
669	sc->ymargin = (sc->height - (vi->vi_height * vi->vi_cheight))/2;
670
671
672	adp->va_window = (vm_offset_t) bcmfb_static_window;
673	adp->va_flags |= V_ADP_FONT /* | V_ADP_COLOR | V_ADP_MODECHANGE */;
674
675	vid_register(&sc->va);
676
677	return (0);
678}
679
680static int
681bcmfb_get_info(video_adapter_t *adp, int mode, video_info_t *info)
682{
683	bcopy(&adp->va_info, info, sizeof(*info));
684	return (0);
685}
686
687static int
688bcmfb_query_mode(video_adapter_t *adp, video_info_t *info)
689{
690	return (0);
691}
692
693static int
694bcmfb_set_mode(video_adapter_t *adp, int mode)
695{
696	return (0);
697}
698
699static int
700bcmfb_save_font(video_adapter_t *adp, int page, int size, int width,
701    u_char *data, int c, int count)
702{
703	return (0);
704}
705
706static int
707bcmfb_load_font(video_adapter_t *adp, int page, int size, int width,
708    u_char *data, int c, int count)
709{
710	struct video_adapter_softc *sc = (struct video_adapter_softc *)adp;
711
712	sc->font = data;
713
714	return (0);
715}
716
717static int
718bcmfb_show_font(video_adapter_t *adp, int page)
719{
720	return (0);
721}
722
723static int
724bcmfb_save_palette(video_adapter_t *adp, u_char *palette)
725{
726	return (0);
727}
728
729static int
730bcmfb_load_palette(video_adapter_t *adp, u_char *palette)
731{
732	return (0);
733}
734
735static int
736bcmfb_set_border(video_adapter_t *adp, int border)
737{
738	return (bcmfb_blank_display(adp, border));
739}
740
741static int
742bcmfb_save_state(video_adapter_t *adp, void *p, size_t size)
743{
744	return (0);
745}
746
747static int
748bcmfb_load_state(video_adapter_t *adp, void *p)
749{
750	return (0);
751}
752
753static int
754bcmfb_set_win_org(video_adapter_t *adp, off_t offset)
755{
756	return (0);
757}
758
759static int
760bcmfb_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
761{
762	*col = *row = 0;
763
764	return (0);
765}
766
767static int
768bcmfb_set_hw_cursor(video_adapter_t *adp, int col, int row)
769{
770	return (0);
771}
772
773static int
774bcmfb_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
775    int celsize, int blink)
776{
777	return (0);
778}
779
780static int
781bcmfb_blank_display(video_adapter_t *adp, int mode)
782{
783
784	struct video_adapter_softc *sc;
785
786	sc = (struct video_adapter_softc *)adp;
787	if (sc && sc->fb_addr)
788		memset((void*)sc->fb_addr, 0, sc->fb_size);
789
790	return (0);
791}
792
793static int
794bcmfb_mmap(video_adapter_t *adp, vm_ooffset_t offset, vm_paddr_t *paddr,
795    int prot, vm_memattr_t *memattr)
796{
797	struct video_adapter_softc *sc;
798
799	sc = (struct video_adapter_softc *)adp;
800
801	/*
802	 * This might be a legacy VGA mem request: if so, just point it at the
803	 * framebuffer, since it shouldn't be touched
804	 */
805	if (offset < sc->stride*sc->height) {
806		*paddr = sc->fb_paddr + offset;
807		return (0);
808	}
809
810	return (EINVAL);
811}
812
813static int
814bcmfb_ioctl(video_adapter_t *adp, u_long cmd, caddr_t data)
815{
816	struct video_adapter_softc *sc;
817	struct fbtype *fb;
818
819	sc = (struct video_adapter_softc *)adp;
820
821	switch (cmd) {
822	case FBIOGTYPE:
823		fb = (struct fbtype *)data;
824		fb->fb_type = FBTYPE_PCIMISC;
825		fb->fb_height = sc->height;
826		fb->fb_width = sc->width;
827		fb->fb_depth = sc->depth;
828		if (sc->depth <= 1 || sc->depth > 8)
829			fb->fb_cmsize = 0;
830		else
831			fb->fb_cmsize = 1 << sc->depth;
832		fb->fb_size = sc->fb_size;
833		break;
834	default:
835		return (fb_commonioctl(adp, cmd, data));
836	}
837
838	return (0);
839}
840
841static int
842bcmfb_clear(video_adapter_t *adp)
843{
844
845	return (bcmfb_blank_display(adp, 0));
846}
847
848static int
849bcmfb_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
850{
851
852	return (0);
853}
854
855static int
856bcmfb_bitblt(video_adapter_t *adp, ...)
857{
858
859	return (0);
860}
861
862static int
863bcmfb_diag(video_adapter_t *adp, int level)
864{
865
866	return (0);
867}
868
869static int
870bcmfb_save_cursor_palette(video_adapter_t *adp, u_char *palette)
871{
872
873	return (0);
874}
875
876static int
877bcmfb_load_cursor_palette(video_adapter_t *adp, u_char *palette)
878{
879
880	return (0);
881}
882
883static int
884bcmfb_copy(video_adapter_t *adp, vm_offset_t src, vm_offset_t dst, int n)
885{
886
887	return (0);
888}
889
890static int
891bcmfb_putp(video_adapter_t *adp, vm_offset_t off, uint32_t p, uint32_t a,
892    int size, int bpp, int bit_ltor, int byte_ltor)
893{
894
895	return (0);
896}
897
898static int
899bcmfb_putc(video_adapter_t *adp, vm_offset_t off, uint8_t c, uint8_t a)
900{
901	struct video_adapter_softc *sc;
902	int row;
903	int col;
904	int i, j, k;
905	uint8_t *addr;
906	u_char *p;
907	uint8_t fg, bg, color;
908	uint16_t rgb;
909
910	sc = (struct video_adapter_softc *)adp;
911
912	if (sc->fb_addr == 0)
913		return (0);
914
915	row = (off / adp->va_info.vi_width) * adp->va_info.vi_cheight;
916	col = (off % adp->va_info.vi_width) * adp->va_info.vi_cwidth;
917	p = sc->font + c*BCMFB_FONT_HEIGHT;
918	addr = (uint8_t *)sc->fb_addr
919	    + (row + sc->ymargin)*(sc->stride)
920	    + (sc->depth/8) * (col + sc->xmargin);
921
922	fg = a & 0xf ;
923	bg = (a >> 4) & 0xf;
924
925	for (i = 0; i < BCMFB_FONT_HEIGHT; i++) {
926		for (j = 0, k = 7; j < 8; j++, k--) {
927			if ((p[i] & (1 << k)) == 0)
928				color = bg;
929			else
930				color = fg;
931
932			switch (sc->depth) {
933			case 32:
934				addr[4*j+0] = bcmfb_palette[color].r;
935				addr[4*j+1] = bcmfb_palette[color].g;
936				addr[4*j+2] = bcmfb_palette[color].b;
937				addr[4*j+3] = bcmfb_palette[color].a;
938				break;
939			case 24:
940				addr[3*j] = bcmfb_palette[color].r;
941				addr[3*j+1] = bcmfb_palette[color].g;
942				addr[3*j+2] = bcmfb_palette[color].b;
943				break;
944			case 16:
945				rgb = (bcmfb_palette[color].r >> 3) << 11;
946				rgb |= (bcmfb_palette[color].g >> 2) << 5;
947				rgb |= (bcmfb_palette[color].b >> 3);
948				addr[2*j] = rgb & 0xff;
949				addr[2*j + 1] = (rgb >> 8) & 0xff;
950			default:
951				/* Not supported yet */
952				break;
953			}
954		}
955
956		addr += (sc->stride);
957	}
958
959        return (0);
960}
961
962static int
963bcmfb_puts(video_adapter_t *adp, vm_offset_t off, u_int16_t *s, int len)
964{
965	int i;
966
967	for (i = 0; i < len; i++)
968		bcmfb_putc(adp, off + i, s[i] & 0xff, (s[i] & 0xff00) >> 8);
969
970	return (0);
971}
972
973static int
974bcmfb_putm(video_adapter_t *adp, int x, int y, uint8_t *pixel_image,
975    uint32_t pixel_mask, int size, int width)
976{
977
978	return (0);
979}
980
981/*
982 * Define a stub keyboard driver in case one hasn't been
983 * compiled into the kernel
984 */
985#include <sys/kbio.h>
986#include <dev/kbd/kbdreg.h>
987
988static int dummy_kbd_configure(int flags);
989
990keyboard_switch_t bcmdummysw;
991
992static int
993dummy_kbd_configure(int flags)
994{
995
996	return (0);
997}
998KEYBOARD_DRIVER(bcmdummy, bcmdummysw, dummy_kbd_configure);
999