2 * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org>
3 * Copyright (c) 2012, 2013 The FreeBSD Foundation
6 * Portions of this software were developed by Oleksandr Rybalko
7 * under sponsorship from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
39 #include <sys/endian.h>
40 #include <sys/kernel.h>
41 #include <sys/kthread.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/mutex.h>
46 #include <sys/queue.h>
47 #include <sys/resource.h>
50 #include <sys/timetc.h>
52 #include <sys/consio.h>
56 #include <machine/bus.h>
57 #include <machine/cpu.h>
58 #include <machine/cpufunc.h>
59 #include <machine/resource.h>
60 #include <machine/frame.h>
61 #include <machine/intr.h>
63 #include <dev/fdt/fdt_common.h>
64 #include <dev/ofw/ofw_bus.h>
65 #include <dev/ofw/ofw_bus_subr.h>
67 #include <dev/fb/fbreg.h>
68 #include <dev/syscons/syscons.h>
70 #include <arm/freescale/imx/imx51_ccmvar.h>
72 #include <arm/freescale/imx/imx51_ipuv3reg.h>
74 #define IMX51_IPU_HSP_CLOCK 665000000
75 #define IPU3FB_FONT_HEIGHT 16
83 bus_space_handle_t ioh;
84 bus_space_handle_t cm_ioh;
85 bus_space_handle_t dp_ioh;
86 bus_space_handle_t di0_ioh;
87 bus_space_handle_t di1_ioh;
88 bus_space_handle_t dctmpl_ioh;
89 bus_space_handle_t dc_ioh;
90 bus_space_handle_t dmfc_ioh;
91 bus_space_handle_t idmac_ioh;
92 bus_space_handle_t cpmem_ioh;
95 struct video_adapter_softc {
96 /* Videoadpater part */
101 unsigned int fb_size;
109 unsigned int xmargin;
110 unsigned int ymargin;
116 static struct ipu3sc_softc *ipu3sc_softc;
117 static struct video_adapter_softc va_softc;
119 /* FIXME: not only 2 bytes color supported */
120 static uint16_t colors[16] = {
126 0xf81f, /* magenta */
128 0xc618, /* light grey */
129 0xc618, /* XXX: dark grey */
130 0x001f, /* XXX: light blue */
131 0x07e0, /* XXX: light green */
132 0x07ff, /* XXX: light cyan */
133 0xf800, /* XXX: light red */
134 0xf81f, /* XXX: light magenta */
138 static uint32_t colors_24[16] = {
144 0x800080,/* Magenta */
146 0xC0C0C0,/* Silver */
148 0x0000FF,/* Light Blue */
149 0x00FF00,/* Light Green */
150 0x00FFFF,/* Light Cyan */
151 0xFF0000,/* Light Red */
152 0xFF00FF,/* Light Magenta */
153 0xFFFF00,/* Yellow */
159 #define IPUV3_READ(ipuv3, module, reg) \
160 bus_space_read_4((ipuv3)->iot, (ipuv3)->module##_ioh, (reg))
161 #define IPUV3_WRITE(ipuv3, module, reg, val) \
162 bus_space_write_4((ipuv3)->iot, (ipuv3)->module##_ioh, (reg), (val))
164 #define CPMEM_CHANNEL_OFFSET(_c) ((_c) * 0x40)
165 #define CPMEM_WORD_OFFSET(_w) ((_w) * 0x20)
166 #define CPMEM_DP_OFFSET(_d) ((_d) * 0x10000)
167 #define IMX_IPU_DP0 0
168 #define IMX_IPU_DP1 1
169 #define CPMEM_CHANNEL(_dp, _ch, _w) \
170 (CPMEM_DP_OFFSET(_dp) + CPMEM_CHANNEL_OFFSET(_ch) + \
171 CPMEM_WORD_OFFSET(_w))
172 #define CPMEM_OFFSET(_dp, _ch, _w, _o) \
173 (CPMEM_CHANNEL((_dp), (_ch), (_w)) + (_o))
175 #define IPUV3_DEBUG 100
178 #define SUBMOD_DUMP_REG(_sc, _m, _l) \
181 printf("*** " #_m " ***\n"); \
182 for (i = 0; i <= (_l); i += 4) { \
184 printf("%04x: ", i & 0xffff); \
185 printf("0x%08x%c", IPUV3_READ((_sc), _m, i), \
186 ((i + 4) % 32)?' ':'\n'); \
193 int ipuv3_debug = IPUV3_DEBUG;
194 #define DPRINTFN(n,x) if (ipuv3_debug>(n)) printf x; else
196 #define DPRINTFN(n,x)
199 static int ipu3_fb_probe(device_t);
200 static int ipu3_fb_attach(device_t);
203 ipu3_fb_malloc(struct ipu3sc_softc *sc, size_t size)
206 sc->vbase = (uint32_t)contigmalloc(size, M_DEVBUF, M_ZERO, 0, ~0,
208 sc->pbase = vtophys(sc->vbase);
214 ipu3_fb_init(void *arg)
216 struct ipu3sc_softc *sc = arg;
217 struct video_adapter_softc *va_sc = &va_softc;
221 /* FW W0[137:125] - 96 = [41:29] */
222 /* FH W0[149:138] - 96 = [53:42] */
223 w0sh96 = IPUV3_READ(sc, cpmem, CPMEM_OFFSET(IMX_IPU_DP1, 23, 0, 16));
225 w0sh96 |= IPUV3_READ(sc, cpmem, CPMEM_OFFSET(IMX_IPU_DP1, 23, 0, 12));
227 va_sc->width = ((w0sh96 >> 29) & 0x1fff) + 1;
228 va_sc->height = ((w0sh96 >> 42) & 0x0fff) + 1;
230 /* SLY W1[115:102] - 96 = [19:6] */
231 w1sh96 = IPUV3_READ(sc, cpmem, CPMEM_OFFSET(IMX_IPU_DP1, 23, 1, 12));
232 va_sc->stride = ((w1sh96 >> 6) & 0x3fff) + 1;
234 printf("%dx%d [%d]\n", va_sc->width, va_sc->height, va_sc->stride);
235 va_sc->fb_size = va_sc->height * va_sc->stride;
237 ipu3_fb_malloc(sc, va_sc->fb_size);
239 /* DP1 + config_ch_23 + word_2 */
240 IPUV3_WRITE(sc, cpmem, CPMEM_OFFSET(IMX_IPU_DP1, 23, 1, 0),
241 ((sc->pbase >> 3) | ((sc->pbase >> 3) << 29)) & 0xffffffff);
243 IPUV3_WRITE(sc, cpmem, CPMEM_OFFSET(IMX_IPU_DP1, 23, 1, 4),
244 ((sc->pbase >> 3) >> 3) & 0xffffffff);
246 va_sc->fb_addr = (intptr_t)sc->vbase;
247 va_sc->fb_paddr = (intptr_t)sc->pbase;
248 va_sc->bpp = va_sc->stride / va_sc->width;
249 va_sc->depth = va_sc->bpp * 8;
253 ipu3_fb_probe(device_t dev)
257 if (!ofw_bus_is_compatible(dev, "fsl,ipu3"))
260 device_set_desc(dev, "i.MX515 Image Processing Unit (FB)");
262 error = sc_probe_unit(device_get_unit(dev),
263 device_get_flags(dev) | SC_AUTODETECT_KBD);
268 return (BUS_PROBE_DEFAULT);
272 ipu3_fb_attach(device_t dev)
274 struct ipu3sc_softc *sc = device_get_softc(dev);
276 bus_space_handle_t ioh;
284 device_printf(dev, "\tclock gate status is %d\n",
285 imx51_get_clk_gating(IMX51CLK_IPU_HSP_CLK_ROOT));
289 err = (sc_attach_unit(device_get_unit(dev),
290 device_get_flags(dev) | SC_AUTODETECT_KBD));
293 device_printf(dev, "failed to attach syscons\n");
297 sc = device_get_softc(dev);
298 sc->iot = iot = fdtbus_bs_tag;
300 device_printf(sc->dev, ": i.MX51 IPUV3 controller\n");
302 /* map controller registers */
303 err = bus_space_map(iot, IPU_CM_BASE, IPU_CM_SIZE, 0, &ioh);
308 /* map Display Multi FIFO Controller registers */
309 err = bus_space_map(iot, IPU_DMFC_BASE, IPU_DMFC_SIZE, 0, &ioh);
311 goto fail_retarn_dmfc;
314 /* map Display Interface 0 registers */
315 err = bus_space_map(iot, IPU_DI0_BASE, IPU_DI0_SIZE, 0, &ioh);
317 goto fail_retarn_di0;
320 /* map Display Interface 1 registers */
321 err = bus_space_map(iot, IPU_DI1_BASE, IPU_DI0_SIZE, 0, &ioh);
323 goto fail_retarn_di1;
326 /* map Display Processor registers */
327 err = bus_space_map(iot, IPU_DP_BASE, IPU_DP_SIZE, 0, &ioh);
332 /* map Display Controller registers */
333 err = bus_space_map(iot, IPU_DC_BASE, IPU_DC_SIZE, 0, &ioh);
338 /* map Image DMA Controller registers */
339 err = bus_space_map(iot, IPU_IDMAC_BASE, IPU_IDMAC_SIZE, 0, &ioh);
341 goto fail_retarn_idmac;
344 /* map CPMEM registers */
345 err = bus_space_map(iot, IPU_CPMEM_BASE, IPU_CPMEM_SIZE, 0, &ioh);
347 goto fail_retarn_cpmem;
350 /* map DCTEMPL registers */
351 err = bus_space_map(iot, IPU_DCTMPL_BASE, IPU_DCTMPL_SIZE, 0, &ioh);
353 goto fail_retarn_dctmpl;
354 sc->dctmpl_ioh = ioh;
357 sc->ih = imx51_ipuv3_intr_establish(IMX51_INT_IPUV3, IPL_BIO,
359 if (sc->ih == NULL) {
360 device_printf(sc->dev,
361 "unable to establish interrupt at irq %d\n",
368 * We have to wait until interrupts are enabled.
369 * Mailbox relies on it to get data from VideoCore
378 bus_space_unmap(sc->iot, sc->cpmem_ioh, IPU_CPMEM_SIZE);
380 bus_space_unmap(sc->iot, sc->idmac_ioh, IPU_IDMAC_SIZE);
382 bus_space_unmap(sc->iot, sc->dc_ioh, IPU_DC_SIZE);
384 bus_space_unmap(sc->iot, sc->dp_ioh, IPU_DP_SIZE);
386 bus_space_unmap(sc->iot, sc->di1_ioh, IPU_DI1_SIZE);
388 bus_space_unmap(sc->iot, sc->di0_ioh, IPU_DI0_SIZE);
390 bus_space_unmap(sc->iot, sc->dmfc_ioh, IPU_DMFC_SIZE);
392 bus_space_unmap(sc->iot, sc->dc_ioh, IPU_CM_SIZE);
394 device_printf(sc->dev,
395 "failed to map registers (errno=%d)\n", err);
399 static device_method_t ipu3_fb_methods[] = {
400 /* Device interface */
401 DEVMETHOD(device_probe, ipu3_fb_probe),
402 DEVMETHOD(device_attach, ipu3_fb_attach),
407 static devclass_t ipu3_fb_devclass;
409 static driver_t ipu3_fb_driver = {
412 sizeof(struct ipu3sc_softc),
415 DRIVER_MODULE(ipu3fb, simplebus, ipu3_fb_driver, ipu3_fb_devclass, 0, 0);
418 * Video driver routines and glue.
420 static int ipu3fb_configure(int);
421 static vi_probe_t ipu3fb_probe;
422 static vi_init_t ipu3fb_init;
423 static vi_get_info_t ipu3fb_get_info;
424 static vi_query_mode_t ipu3fb_query_mode;
425 static vi_set_mode_t ipu3fb_set_mode;
426 static vi_save_font_t ipu3fb_save_font;
427 static vi_load_font_t ipu3fb_load_font;
428 static vi_show_font_t ipu3fb_show_font;
429 static vi_save_palette_t ipu3fb_save_palette;
430 static vi_load_palette_t ipu3fb_load_palette;
431 static vi_set_border_t ipu3fb_set_border;
432 static vi_save_state_t ipu3fb_save_state;
433 static vi_load_state_t ipu3fb_load_state;
434 static vi_set_win_org_t ipu3fb_set_win_org;
435 static vi_read_hw_cursor_t ipu3fb_read_hw_cursor;
436 static vi_set_hw_cursor_t ipu3fb_set_hw_cursor;
437 static vi_set_hw_cursor_shape_t ipu3fb_set_hw_cursor_shape;
438 static vi_blank_display_t ipu3fb_blank_display;
439 static vi_mmap_t ipu3fb_mmap;
440 static vi_ioctl_t ipu3fb_ioctl;
441 static vi_clear_t ipu3fb_clear;
442 static vi_fill_rect_t ipu3fb_fill_rect;
443 static vi_bitblt_t ipu3fb_bitblt;
444 static vi_diag_t ipu3fb_diag;
445 static vi_save_cursor_palette_t ipu3fb_save_cursor_palette;
446 static vi_load_cursor_palette_t ipu3fb_load_cursor_palette;
447 static vi_copy_t ipu3fb_copy;
448 static vi_putp_t ipu3fb_putp;
449 static vi_putc_t ipu3fb_putc;
450 static vi_puts_t ipu3fb_puts;
451 static vi_putm_t ipu3fb_putm;
453 static video_switch_t ipu3fbvidsw = {
454 .probe = ipu3fb_probe,
456 .get_info = ipu3fb_get_info,
457 .query_mode = ipu3fb_query_mode,
458 .set_mode = ipu3fb_set_mode,
459 .save_font = ipu3fb_save_font,
460 .load_font = ipu3fb_load_font,
461 .show_font = ipu3fb_show_font,
462 .save_palette = ipu3fb_save_palette,
463 .load_palette = ipu3fb_load_palette,
464 .set_border = ipu3fb_set_border,
465 .save_state = ipu3fb_save_state,
466 .load_state = ipu3fb_load_state,
467 .set_win_org = ipu3fb_set_win_org,
468 .read_hw_cursor = ipu3fb_read_hw_cursor,
469 .set_hw_cursor = ipu3fb_set_hw_cursor,
470 .set_hw_cursor_shape = ipu3fb_set_hw_cursor_shape,
471 .blank_display = ipu3fb_blank_display,
473 .ioctl = ipu3fb_ioctl,
474 .clear = ipu3fb_clear,
475 .fill_rect = ipu3fb_fill_rect,
476 .bitblt = ipu3fb_bitblt,
478 .save_cursor_palette = ipu3fb_save_cursor_palette,
479 .load_cursor_palette = ipu3fb_load_cursor_palette,
487 VIDEO_DRIVER(ipu3fb, ipu3fbvidsw, ipu3fb_configure);
489 extern sc_rndr_sw_t txtrndrsw;
490 RENDERER(ipu3fb, 0, txtrndrsw, gfb_set);
491 RENDERER_MODULE(ipu3fb, gfb_set);
493 static uint16_t ipu3fb_static_window[ROW*COL];
494 extern u_char dflt_font_16[];
497 ipu3fb_configure(int flags)
499 struct video_adapter_softc *sc;
509 sc->stride = sc->width * sc->bpp;
511 ipu3fb_init(0, &sc->va, 0);
519 ipu3fb_probe(int unit, video_adapter_t **adp, void *arg, int flags)
526 ipu3fb_init(int unit, video_adapter_t *adp, int flags)
528 struct video_adapter_softc *sc;
531 sc = (struct video_adapter_softc *)adp;
534 vid_init_struct(adp, "ipu3fb", -1, unit);
536 sc->font = dflt_font_16;
537 vi->vi_cheight = IPU3FB_FONT_HEIGHT;
539 vi->vi_width = sc->width/8;
540 vi->vi_height = sc->height/vi->vi_cheight;
543 * Clamp width/height to syscons maximums
545 if (vi->vi_width > COL)
547 if (vi->vi_height > ROW)
550 sc->xmargin = (sc->width - (vi->vi_width * vi->vi_cwidth)) / 2;
551 sc->ymargin = (sc->height - (vi->vi_height * vi->vi_cheight))/2;
553 adp->va_window = (vm_offset_t) ipu3fb_static_window;
554 adp->va_flags |= V_ADP_FONT /* | V_ADP_COLOR | V_ADP_MODECHANGE */;
555 adp->va_line_width = sc->stride;
556 adp->va_buffer_size = sc->fb_size;
558 vid_register(&sc->va);
564 ipu3fb_get_info(video_adapter_t *adp, int mode, video_info_t *info)
567 bcopy(&adp->va_info, info, sizeof(*info));
572 ipu3fb_query_mode(video_adapter_t *adp, video_info_t *info)
579 ipu3fb_set_mode(video_adapter_t *adp, int mode)
586 ipu3fb_save_font(video_adapter_t *adp, int page, int size, int width,
587 u_char *data, int c, int count)
594 ipu3fb_load_font(video_adapter_t *adp, int page, int size, int width,
595 u_char *data, int c, int count)
597 struct video_adapter_softc *sc;
599 sc = (struct video_adapter_softc *)adp;
606 ipu3fb_show_font(video_adapter_t *adp, int page)
613 ipu3fb_save_palette(video_adapter_t *adp, u_char *palette)
620 ipu3fb_load_palette(video_adapter_t *adp, u_char *palette)
627 ipu3fb_set_border(video_adapter_t *adp, int border)
630 return (ipu3fb_blank_display(adp, border));
634 ipu3fb_save_state(video_adapter_t *adp, void *p, size_t size)
641 ipu3fb_load_state(video_adapter_t *adp, void *p)
648 ipu3fb_set_win_org(video_adapter_t *adp, off_t offset)
655 ipu3fb_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
663 ipu3fb_set_hw_cursor(video_adapter_t *adp, int col, int row)
670 ipu3fb_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
671 int celsize, int blink)
678 ipu3fb_blank_display(video_adapter_t *adp, int mode)
685 ipu3fb_mmap(video_adapter_t *adp, vm_ooffset_t offset, vm_paddr_t *paddr,
686 int prot, vm_memattr_t *memattr)
688 struct video_adapter_softc *sc;
690 sc = (struct video_adapter_softc *)adp;
693 * This might be a legacy VGA mem request: if so, just point it at the
694 * framebuffer, since it shouldn't be touched
696 if (offset < sc->stride * sc->height) {
697 *paddr = sc->fb_paddr + offset;
705 ipu3fb_ioctl(video_adapter_t *adp, u_long cmd, caddr_t data)
707 struct video_adapter_softc *sc;
710 sc = (struct video_adapter_softc *)adp;
714 fb = (struct fbtype *)data;
715 fb->fb_type = FBTYPE_PCIMISC;
716 fb->fb_height = sc->height;
717 fb->fb_width = sc->width;
718 fb->fb_depth = sc->depth;
719 if (sc->depth <= 1 || sc->depth > 8)
722 fb->fb_cmsize = 1 << sc->depth;
723 fb->fb_size = sc->fb_size;
728 return (fb_commonioctl(adp, cmd, data));
735 ipu3fb_clear(video_adapter_t *adp)
738 return (ipu3fb_blank_display(adp, 0));
742 ipu3fb_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
749 ipu3fb_bitblt(video_adapter_t *adp, ...)
756 ipu3fb_diag(video_adapter_t *adp, int level)
763 ipu3fb_save_cursor_palette(video_adapter_t *adp, u_char *palette)
770 ipu3fb_load_cursor_palette(video_adapter_t *adp, u_char *palette)
777 ipu3fb_copy(video_adapter_t *adp, vm_offset_t src, vm_offset_t dst, int n)
784 ipu3fb_putp(video_adapter_t *adp, vm_offset_t off, uint32_t p, uint32_t a,
785 int size, int bpp, int bit_ltor, int byte_ltor)
792 ipu3fb_putc(video_adapter_t *adp, vm_offset_t off, uint8_t c, uint8_t a)
794 struct video_adapter_softc *sc;
799 uint32_t fg, bg, color;
801 sc = (struct video_adapter_softc *)adp;
804 if (sc->fb_addr == 0)
806 row = (off / adp->va_info.vi_width) * adp->va_info.vi_cheight;
807 col = (off % adp->va_info.vi_width) * adp->va_info.vi_cwidth;
808 p = sc->font + c * IPU3FB_FONT_HEIGHT;
809 addr = (uint8_t *)sc->fb_addr
810 + (row + sc->ymargin) * (sc->stride)
811 + bpp * (col + sc->xmargin);
814 bg = colors[(a >> 4) & 0x0f];
815 fg = colors[a & 0x0f];
816 } else if (bpp == 3) {
817 bg = colors_24[(a >> 4) & 0x0f];
818 fg = colors_24[a & 0x0f];
823 for (i = 0; i < IPU3FB_FONT_HEIGHT; i++) {
824 for (j = 0, k = 7; j < 8; j++, k--) {
825 if ((p[i] & (1 << k)) == 0)
829 /* FIXME: BPP maybe different */
830 for (b = 0; b < bpp; b ++)
832 (color >> (b << 3)) & 0xff;
835 addr += (sc->stride);
842 ipu3fb_puts(video_adapter_t *adp, vm_offset_t off, u_int16_t *s, int len)
846 for (i = 0; i < len; i++)
847 ipu3fb_putc(adp, off + i, s[i] & 0xff, (s[i] & 0xff00) >> 8);
853 ipu3fb_putm(video_adapter_t *adp, int x, int y, uint8_t *pixel_image,
854 uint32_t pixel_mask, int size, int width)
861 * Define a stub keyboard driver in case one hasn't been
862 * compiled into the kernel
864 #include <sys/kbio.h>
865 #include <dev/kbd/kbdreg.h>
867 static int dummy_kbd_configure(int flags);
869 keyboard_switch_t ipu3dummysw;
872 dummy_kbd_configure(int flags)
877 KEYBOARD_DRIVER(ipu3dummy, ipu3dummysw, dummy_kbd_configure);