2 * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/consio.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
43 #include <dev/fb/fbreg.h>
44 #include <dev/fdt/fdt_common.h>
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_bus_subr.h>
47 #include <dev/syscons/syscons.h>
49 #include <arm/broadcom/bcm2835/bcm2835_mbox_prop.h>
60 static struct argb bcmfb_palette[16] = {
61 {0x00, 0x00, 0x00, 0x00},
62 {0x00, 0x00, 0x00, 0xaa},
63 {0x00, 0x00, 0xaa, 0x00},
64 {0x00, 0x00, 0xaa, 0xaa},
65 {0x00, 0xaa, 0x00, 0x00},
66 {0x00, 0xaa, 0x00, 0xaa},
67 {0x00, 0xaa, 0x55, 0x00},
68 {0x00, 0xaa, 0xaa, 0xaa},
69 {0x00, 0x55, 0x55, 0x55},
70 {0x00, 0x55, 0x55, 0xff},
71 {0x00, 0x55, 0xff, 0x55},
72 {0x00, 0x55, 0xff, 0xff},
73 {0x00, 0xff, 0x55, 0x55},
74 {0x00, 0xff, 0x55, 0xff},
75 {0x00, 0xff, 0xff, 0x55},
76 {0x00, 0xff, 0xff, 0xff}
79 /* mouse pointer from dev/syscons/scgfbrndr.c */
80 static u_char mouse_pointer[16] = {
81 0x00, 0x40, 0x60, 0x70, 0x78, 0x7c, 0x7e, 0x68,
82 0x0c, 0x0c, 0x06, 0x06, 0x00, 0x00, 0x00, 0x00
85 #define BCMFB_FONT_HEIGHT 16
86 #define BCMFB_FONT_WIDTH 8
92 /* Videoadpater part */
104 unsigned int xmargin;
105 unsigned int ymargin;
112 static struct bcmsc_softc bcmsc;
114 static int bcm_fb_probe(device_t);
115 static int bcm_fb_attach(device_t);
116 static void bcmfb_update_margins(video_adapter_t *adp);
117 static int bcmfb_configure(int);
120 bcm_fb_probe(device_t dev)
124 if (!ofw_bus_is_compatible(dev, "broadcom,bcm2835-fb"))
126 device_set_desc(dev, "BCM2835 framebuffer device");
127 error = sc_probe_unit(device_get_unit(dev),
128 device_get_flags(dev) | SC_AUTODETECT_KBD);
132 return (BUS_PROBE_DEFAULT);
136 bcm_fb_attach(device_t dev)
138 struct bcm2835_fb_config fb;
139 struct bcmsc_softc *sc;
141 sc = (struct bcmsc_softc *)vid_get_adapter(vid_find_adapter(
144 device_set_softc(dev, sc);
146 sc = device_get_softc(dev);
148 memset(&fb, 0, sizeof(fb));
149 if (bcm2835_mbox_fb_get_w_h(dev, &fb) != 0)
152 if (bcm2835_mbox_fb_init(dev, &fb) != 0)
155 sc->fb_addr = (intptr_t)pmap_mapdev(fb.base, fb.size);
156 sc->fb_paddr = fb.base;
157 sc->fb_size = fb.size;
159 sc->stride = fb.pitch;
161 sc->height = fb.yres;
162 bcmfb_update_margins(&sc->va);
164 if (sc_attach_unit(device_get_unit(dev),
165 device_get_flags(dev) | SC_AUTODETECT_KBD) != 0) {
166 device_printf(dev, "failed to attach syscons\n");
170 device_printf(dev, "%dx%d(%dx%d@%d,%d) %dbpp\n", fb.xres, fb.yres,
171 fb.vxres, fb.vyres, fb.xoffset, fb.yoffset, fb.bpp);
173 "fbswap: %d, pitch %d, base 0x%08x, screen_size %d\n",
174 sc->fbswap, fb.pitch, fb.base, fb.size);
179 static device_method_t bcm_fb_methods[] = {
180 /* Device interface */
181 DEVMETHOD(device_probe, bcm_fb_probe),
182 DEVMETHOD(device_attach, bcm_fb_attach),
187 static devclass_t bcm_fb_devclass;
189 static driver_t bcm_fb_driver = {
192 sizeof(struct bcmsc_softc),
195 DRIVER_MODULE(bcm2835fb, ofwbus, bcm_fb_driver, bcm_fb_devclass, 0, 0);
198 * Video driver routines and glue.
200 static vi_probe_t bcmfb_probe;
201 static vi_init_t bcmfb_init;
202 static vi_get_info_t bcmfb_get_info;
203 static vi_query_mode_t bcmfb_query_mode;
204 static vi_set_mode_t bcmfb_set_mode;
205 static vi_save_font_t bcmfb_save_font;
206 static vi_load_font_t bcmfb_load_font;
207 static vi_show_font_t bcmfb_show_font;
208 static vi_save_palette_t bcmfb_save_palette;
209 static vi_load_palette_t bcmfb_load_palette;
210 static vi_set_border_t bcmfb_set_border;
211 static vi_save_state_t bcmfb_save_state;
212 static vi_load_state_t bcmfb_load_state;
213 static vi_set_win_org_t bcmfb_set_win_org;
214 static vi_read_hw_cursor_t bcmfb_read_hw_cursor;
215 static vi_set_hw_cursor_t bcmfb_set_hw_cursor;
216 static vi_set_hw_cursor_shape_t bcmfb_set_hw_cursor_shape;
217 static vi_blank_display_t bcmfb_blank_display;
218 static vi_mmap_t bcmfb_mmap;
219 static vi_ioctl_t bcmfb_ioctl;
220 static vi_clear_t bcmfb_clear;
221 static vi_fill_rect_t bcmfb_fill_rect;
222 static vi_bitblt_t bcmfb_bitblt;
223 static vi_diag_t bcmfb_diag;
224 static vi_save_cursor_palette_t bcmfb_save_cursor_palette;
225 static vi_load_cursor_palette_t bcmfb_load_cursor_palette;
226 static vi_copy_t bcmfb_copy;
227 static vi_putp_t bcmfb_putp;
228 static vi_putc_t bcmfb_putc;
229 static vi_puts_t bcmfb_puts;
230 static vi_putm_t bcmfb_putm;
232 static video_switch_t bcmfbvidsw = {
233 .probe = bcmfb_probe,
235 .get_info = bcmfb_get_info,
236 .query_mode = bcmfb_query_mode,
237 .set_mode = bcmfb_set_mode,
238 .save_font = bcmfb_save_font,
239 .load_font = bcmfb_load_font,
240 .show_font = bcmfb_show_font,
241 .save_palette = bcmfb_save_palette,
242 .load_palette = bcmfb_load_palette,
243 .set_border = bcmfb_set_border,
244 .save_state = bcmfb_save_state,
245 .load_state = bcmfb_load_state,
246 .set_win_org = bcmfb_set_win_org,
247 .read_hw_cursor = bcmfb_read_hw_cursor,
248 .set_hw_cursor = bcmfb_set_hw_cursor,
249 .set_hw_cursor_shape = bcmfb_set_hw_cursor_shape,
250 .blank_display = bcmfb_blank_display,
252 .ioctl = bcmfb_ioctl,
253 .clear = bcmfb_clear,
254 .fill_rect = bcmfb_fill_rect,
255 .bitblt = bcmfb_bitblt,
257 .save_cursor_palette = bcmfb_save_cursor_palette,
258 .load_cursor_palette = bcmfb_load_cursor_palette,
266 VIDEO_DRIVER(bcmfb, bcmfbvidsw, bcmfb_configure);
268 static vr_init_t bcmrend_init;
269 static vr_clear_t bcmrend_clear;
270 static vr_draw_border_t bcmrend_draw_border;
271 static vr_draw_t bcmrend_draw;
272 static vr_set_cursor_t bcmrend_set_cursor;
273 static vr_draw_cursor_t bcmrend_draw_cursor;
274 static vr_blink_cursor_t bcmrend_blink_cursor;
275 static vr_set_mouse_t bcmrend_set_mouse;
276 static vr_draw_mouse_t bcmrend_draw_mouse;
279 * We use our own renderer; this is because we must emulate a hardware
282 static sc_rndr_sw_t bcmrend = {
289 bcmrend_blink_cursor,
294 RENDERER(bcmfb, 0, bcmrend, gfb_set);
295 RENDERER_MODULE(bcmfb, gfb_set);
298 bcmrend_init(scr_stat* scp)
303 bcmrend_clear(scr_stat* scp, int c, int attr)
308 bcmrend_draw_border(scr_stat* scp, int color)
313 bcmrend_draw(scr_stat* scp, int from, int count, int flip)
315 video_adapter_t* adp = scp->sc->adp;
319 /* Normal printing */
320 vidd_puts(adp, from, (uint16_t*)sc_vtb_pointer(&scp->vtb, from), count);
322 /* This is for selections and such: invert the color attribute */
323 for (i = count; i-- > 0; ++from) {
324 c = sc_vtb_getc(&scp->vtb, from);
325 a = sc_vtb_geta(&scp->vtb, from) >> 8;
326 vidd_putc(adp, from, c, (a >> 4) | ((a & 0xf) << 4));
332 bcmrend_set_cursor(scr_stat* scp, int base, int height, int blink)
337 bcmrend_draw_cursor(scr_stat* scp, int off, int blink, int on, int flip)
339 int bytes, col, i, j, row;
340 struct bcmsc_softc *sc;
342 video_adapter_t *adp;
345 sc = (struct bcmsc_softc *)adp;
347 if (scp->curs_attr.height <= 0)
350 if (sc->fb_addr == 0)
353 if (off >= adp->va_info.vi_width * adp->va_info.vi_height)
356 /* calculate the coordinates in the video buffer */
357 row = (off / adp->va_info.vi_width) * adp->va_info.vi_cheight;
358 col = (off % adp->va_info.vi_width) * adp->va_info.vi_cwidth;
360 addr = (uint8_t *)sc->fb_addr
361 + (row + sc->ymargin)*(sc->stride)
362 + (sc->depth/8) * (col + sc->xmargin);
364 bytes = sc->depth / 8;
365 /* our cursor consists of simply inverting the char under it */
366 for (i = 0; i < adp->va_info.vi_cheight; i++) {
367 for (j = 0; j < adp->va_info.vi_cwidth; j++) {
371 addr[bytes*j + 2] ^= 0xff;
374 addr[bytes*j + 1] ^= 0xff;
375 addr[bytes*j] ^= 0xff;
387 bcmrend_blink_cursor(scr_stat* scp, int at, int flip)
392 bcmrend_set_mouse(scr_stat* scp)
397 bcmrend_draw_mouse(scr_stat* scp, int x, int y, int on)
399 vidd_putm(scp->sc->adp, x, y, mouse_pointer, 0xffffffff, 16, 8);
402 static uint16_t bcmfb_static_window[ROW*COL];
403 extern u_char dflt_font_16[];
406 * Update videoadapter settings after changing resolution
409 bcmfb_update_margins(video_adapter_t *adp)
411 struct bcmsc_softc *sc;
414 sc = (struct bcmsc_softc *)adp;
417 sc->xmargin = (sc->width - (vi->vi_width * vi->vi_cwidth)) / 2;
418 sc->ymargin = (sc->height - (vi->vi_height * vi->vi_cheight)) / 2;
422 bcmfb_configure(int flags)
424 char bootargs[2048], *n, *p, *v;
426 phandle_t chosen, display, root;
427 struct bcmsc_softc *sc;
437 * It seems there is no way to let syscons framework know
438 * that framebuffer resolution has changed. So just try
439 * to fetch data from FDT bootargs, FDT display data and
440 * finally go with defaults if everything else has failed.
442 chosen = OF_finddevice("/chosen");
444 OF_getprop(chosen, "bootargs", &bootargs, sizeof(bootargs)) > 0) {
446 while ((v = strsep(&p, " ")) != NULL) {
450 if (strcmp(n, "bcm2708_fb.fbwidth") == 0 && v != NULL)
451 sc->width = (unsigned int)strtol(v, NULL, 0);
452 else if (strcmp(n, "bcm2708_fb.fbheight") == 0 &&
454 sc->height = (unsigned int)strtol(v, NULL, 0);
455 else if (strcmp(n, "bcm2708_fb.fbswap") == 0 &&
462 root = OF_finddevice("/");
464 (display = fdt_find_compatible(root, "broadcom,bcm2835-fb", 1))) {
465 if (sc->width == 0) {
466 if ((OF_getprop(display, "broadcom,width",
467 &cell, sizeof(cell))) > 0)
468 sc->width = (int)fdt32_to_cpu(cell);
471 if (sc->height == 0) {
472 if ((OF_getprop(display, "broadcom,height",
473 &cell, sizeof(cell))) > 0)
474 sc->height = (int)fdt32_to_cpu(cell);
479 sc->width = FB_WIDTH;
481 sc->height = FB_HEIGHT;
483 bcmfb_init(0, &sc->va, 0);
490 bcmfb_probe(int unit, video_adapter_t **adp, void *arg, int flags)
497 bcmfb_init(int unit, video_adapter_t *adp, int flags)
499 struct bcmsc_softc *sc;
502 sc = (struct bcmsc_softc *)adp;
505 vid_init_struct(adp, "bcmfb", -1, unit);
507 sc->font = dflt_font_16;
508 vi->vi_cheight = BCMFB_FONT_HEIGHT;
509 vi->vi_cwidth = BCMFB_FONT_WIDTH;
510 vi->vi_width = sc->width / vi->vi_cwidth;
511 vi->vi_height = sc->height / vi->vi_cheight;
514 * Clamp width/height to syscons maximums
516 if (vi->vi_width > COL)
518 if (vi->vi_height > ROW)
521 sc->xmargin = (sc->width - (vi->vi_width * vi->vi_cwidth)) / 2;
522 sc->ymargin = (sc->height - (vi->vi_height * vi->vi_cheight)) / 2;
524 adp->va_window = (vm_offset_t) bcmfb_static_window;
525 adp->va_flags |= V_ADP_FONT /* | V_ADP_COLOR | V_ADP_MODECHANGE */;
527 vid_register(&sc->va);
533 bcmfb_get_info(video_adapter_t *adp, int mode, video_info_t *info)
535 bcopy(&adp->va_info, info, sizeof(*info));
540 bcmfb_query_mode(video_adapter_t *adp, video_info_t *info)
546 bcmfb_set_mode(video_adapter_t *adp, int mode)
552 bcmfb_save_font(video_adapter_t *adp, int page, int size, int width,
553 u_char *data, int c, int count)
559 bcmfb_load_font(video_adapter_t *adp, int page, int size, int width,
560 u_char *data, int c, int count)
562 struct bcmsc_softc *sc;
564 sc = (struct bcmsc_softc *)adp;
571 bcmfb_show_font(video_adapter_t *adp, int page)
577 bcmfb_save_palette(video_adapter_t *adp, u_char *palette)
583 bcmfb_load_palette(video_adapter_t *adp, u_char *palette)
589 bcmfb_set_border(video_adapter_t *adp, int border)
591 return (bcmfb_blank_display(adp, border));
595 bcmfb_save_state(video_adapter_t *adp, void *p, size_t size)
601 bcmfb_load_state(video_adapter_t *adp, void *p)
607 bcmfb_set_win_org(video_adapter_t *adp, off_t offset)
613 bcmfb_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
621 bcmfb_set_hw_cursor(video_adapter_t *adp, int col, int row)
627 bcmfb_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
628 int celsize, int blink)
634 bcmfb_blank_display(video_adapter_t *adp, int mode)
637 struct bcmsc_softc *sc;
639 sc = (struct bcmsc_softc *)adp;
640 if (sc && sc->fb_addr)
641 memset((void*)sc->fb_addr, 0, sc->fb_size);
647 bcmfb_mmap(video_adapter_t *adp, vm_ooffset_t offset, vm_paddr_t *paddr,
648 int prot, vm_memattr_t *memattr)
650 struct bcmsc_softc *sc;
652 sc = (struct bcmsc_softc *)adp;
655 * This might be a legacy VGA mem request: if so, just point it at the
656 * framebuffer, since it shouldn't be touched
658 if (offset < sc->stride*sc->height) {
659 *paddr = sc->fb_paddr + offset;
667 bcmfb_ioctl(video_adapter_t *adp, u_long cmd, caddr_t data)
669 struct bcmsc_softc *sc;
672 sc = (struct bcmsc_softc *)adp;
676 fb = (struct fbtype *)data;
677 fb->fb_type = FBTYPE_PCIMISC;
678 fb->fb_height = sc->height;
679 fb->fb_width = sc->width;
680 fb->fb_depth = sc->depth;
681 if (sc->depth <= 1 || sc->depth > 8)
684 fb->fb_cmsize = 1 << sc->depth;
685 fb->fb_size = sc->fb_size;
688 return (fb_commonioctl(adp, cmd, data));
695 bcmfb_clear(video_adapter_t *adp)
698 return (bcmfb_blank_display(adp, 0));
702 bcmfb_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
709 bcmfb_bitblt(video_adapter_t *adp, ...)
716 bcmfb_diag(video_adapter_t *adp, int level)
723 bcmfb_save_cursor_palette(video_adapter_t *adp, u_char *palette)
730 bcmfb_load_cursor_palette(video_adapter_t *adp, u_char *palette)
737 bcmfb_copy(video_adapter_t *adp, vm_offset_t src, vm_offset_t dst, int n)
744 bcmfb_putp(video_adapter_t *adp, vm_offset_t off, uint32_t p, uint32_t a,
745 int size, int bpp, int bit_ltor, int byte_ltor)
752 bcmfb_putc(video_adapter_t *adp, vm_offset_t off, uint8_t c, uint8_t a)
754 int bytes, col, i, j, k, row;
755 struct bcmsc_softc *sc;
757 uint8_t *addr, fg, bg, color;
760 sc = (struct bcmsc_softc *)adp;
762 if (sc->fb_addr == 0)
765 row = (off / adp->va_info.vi_width) * adp->va_info.vi_cheight;
766 col = (off % adp->va_info.vi_width) * adp->va_info.vi_cwidth;
767 p = sc->font + c*BCMFB_FONT_HEIGHT;
768 addr = (uint8_t *)sc->fb_addr
769 + (row + sc->ymargin)*(sc->stride)
770 + (sc->depth/8) * (col + sc->xmargin);
775 bytes = sc->depth / 8;
776 for (i = 0; i < BCMFB_FONT_HEIGHT; i++) {
777 for (j = 0, k = 7; j < 8; j++, k--) {
778 if ((p[i] & (1 << k)) == 0)
787 addr[bytes * j + 0] =
788 bcmfb_palette[color].b;
789 addr[bytes * j + 1] =
790 bcmfb_palette[color].g;
791 addr[bytes * j + 2] =
792 bcmfb_palette[color].r;
794 addr[bytes * j + 0] =
795 bcmfb_palette[color].r;
796 addr[bytes * j + 1] =
797 bcmfb_palette[color].g;
798 addr[bytes * j + 2] =
799 bcmfb_palette[color].b;
802 addr[bytes * j + 3] =
803 bcmfb_palette[color].a;
806 rgb = (bcmfb_palette[color].r >> 3) << 11;
807 rgb |= (bcmfb_palette[color].g >> 2) << 5;
808 rgb |= (bcmfb_palette[color].b >> 3);
809 addr[bytes * j] = rgb & 0xff;
810 addr[bytes * j + 1] = (rgb >> 8) & 0xff;
812 /* Not supported yet */
817 addr += (sc->stride);
824 bcmfb_puts(video_adapter_t *adp, vm_offset_t off, u_int16_t *s, int len)
828 for (i = 0; i < len; i++)
829 bcmfb_putc(adp, off + i, s[i] & 0xff, (s[i] & 0xff00) >> 8);
835 bcmfb_putm(video_adapter_t *adp, int x, int y, uint8_t *pixel_image,
836 uint32_t pixel_mask, int size, int width)
843 * Define a stub keyboard driver in case one hasn't been
844 * compiled into the kernel
846 #include <sys/kbio.h>
847 #include <dev/kbd/kbdreg.h>
849 static int dummy_kbd_configure(int flags);
851 keyboard_switch_t bcmdummysw;
854 dummy_kbd_configure(int flags)
859 KEYBOARD_DRIVER(bcmdummy, bcmdummysw, dummy_kbd_configure);