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>
53 #include <sys/eventhandler.h>
57 #include <machine/bus.h>
58 #include <machine/cpu.h>
59 #include <machine/cpufunc.h>
60 #include <machine/fdt.h>
61 #include <machine/resource.h>
62 #include <machine/frame.h>
63 #include <machine/intr.h>
65 #include <dev/fdt/fdt_common.h>
66 #include <dev/ofw/ofw_bus.h>
67 #include <dev/ofw/ofw_bus_subr.h>
69 #include <dev/vt/vt.h>
70 #include <dev/vt/colors/vt_termcolors.h>
72 #include <arm/freescale/imx/imx51_ccmvar.h>
74 #include <arm/freescale/imx/imx51_ipuv3reg.h>
78 #define IMX51_IPU_HSP_CLOCK 665000000
82 device_t sc_fbd; /* fbd child */
83 struct fb_info sc_info;
86 bus_space_handle_t ioh;
87 bus_space_handle_t cm_ioh;
88 bus_space_handle_t dp_ioh;
89 bus_space_handle_t di0_ioh;
90 bus_space_handle_t di1_ioh;
91 bus_space_handle_t dctmpl_ioh;
92 bus_space_handle_t dc_ioh;
93 bus_space_handle_t dmfc_ioh;
94 bus_space_handle_t idmac_ioh;
95 bus_space_handle_t cpmem_ioh;
98 static struct ipu3sc_softc *ipu3sc_softc;
100 #define IPUV3_READ(ipuv3, module, reg) \
101 bus_space_read_4((ipuv3)->iot, (ipuv3)->module##_ioh, (reg))
102 #define IPUV3_WRITE(ipuv3, module, reg, val) \
103 bus_space_write_4((ipuv3)->iot, (ipuv3)->module##_ioh, (reg), (val))
105 #define CPMEM_CHANNEL_OFFSET(_c) ((_c) * 0x40)
106 #define CPMEM_WORD_OFFSET(_w) ((_w) * 0x20)
107 #define CPMEM_DP_OFFSET(_d) ((_d) * 0x10000)
108 #define IMX_IPU_DP0 0
109 #define IMX_IPU_DP1 1
110 #define CPMEM_CHANNEL(_dp, _ch, _w) \
111 (CPMEM_DP_OFFSET(_dp) + CPMEM_CHANNEL_OFFSET(_ch) + \
112 CPMEM_WORD_OFFSET(_w))
113 #define CPMEM_OFFSET(_dp, _ch, _w, _o) \
114 (CPMEM_CHANNEL((_dp), (_ch), (_w)) + (_o))
116 static int ipu3_fb_probe(device_t);
117 static int ipu3_fb_attach(device_t);
120 ipu3_fb_init(struct ipu3sc_softc *sc)
125 /* FW W0[137:125] - 96 = [41:29] */
126 /* FH W0[149:138] - 96 = [53:42] */
127 w0sh96 = IPUV3_READ(sc, cpmem, CPMEM_OFFSET(IMX_IPU_DP1, 23, 0, 16));
129 w0sh96 |= IPUV3_READ(sc, cpmem, CPMEM_OFFSET(IMX_IPU_DP1, 23, 0, 12));
131 sc->sc_info.fb_width = ((w0sh96 >> 29) & 0x1fff) + 1;
132 sc->sc_info.fb_height = ((w0sh96 >> 42) & 0x0fff) + 1;
134 /* SLY W1[115:102] - 96 = [19:6] */
135 w1sh96 = IPUV3_READ(sc, cpmem, CPMEM_OFFSET(IMX_IPU_DP1, 23, 1, 12));
136 sc->sc_info.fb_stride = ((w1sh96 >> 6) & 0x3fff) + 1;
138 printf("%dx%d [%d]\n", sc->sc_info.fb_width, sc->sc_info.fb_height,
139 sc->sc_info.fb_stride);
140 sc->sc_info.fb_size = sc->sc_info.fb_height * sc->sc_info.fb_stride;
142 sc->sc_info.fb_vbase = (intptr_t)contigmalloc(sc->sc_info.fb_size,
143 M_DEVBUF, M_ZERO, 0, ~0, PAGE_SIZE, 0);
144 sc->sc_info.fb_pbase = (intptr_t)vtophys(sc->sc_info.fb_vbase);
146 /* DP1 + config_ch_23 + word_2 */
147 IPUV3_WRITE(sc, cpmem, CPMEM_OFFSET(IMX_IPU_DP1, 23, 1, 0),
148 (((uint32_t)sc->sc_info.fb_pbase >> 3) |
149 (((uint32_t)sc->sc_info.fb_pbase >> 3) << 29)) & 0xffffffff);
151 IPUV3_WRITE(sc, cpmem, CPMEM_OFFSET(IMX_IPU_DP1, 23, 1, 4),
152 (((uint32_t)sc->sc_info.fb_pbase >> 3) >> 3) & 0xffffffff);
154 /* XXX: fetch or set it from/to IPU. */
155 sc->sc_info.fb_bpp = sc->sc_info.fb_depth = sc->sc_info.fb_stride /
156 sc->sc_info.fb_width * 8;
159 /* Use own color map, because of different RGB offset. */
161 ipu3_fb_init_cmap(uint32_t *cmap, int bytespp)
166 return (vt_generate_cons_palette(cmap, COLOR_FORMAT_RGB,
167 0x7, 5, 0x7, 2, 0x3, 0));
169 return (vt_generate_cons_palette(cmap, COLOR_FORMAT_RGB,
170 0x1f, 10, 0x1f, 5, 0x1f, 0));
172 return (vt_generate_cons_palette(cmap, COLOR_FORMAT_RGB,
173 0x1f, 11, 0x3f, 5, 0x1f, 0));
175 case 32: /* Ignore alpha. */
176 return (vt_generate_cons_palette(cmap, COLOR_FORMAT_RGB,
177 0xff, 0, 0xff, 8, 0xff, 16));
184 ipu3_fb_probe(device_t dev)
187 if (!ofw_bus_status_okay(dev))
190 if (!ofw_bus_is_compatible(dev, "fsl,ipu3"))
193 device_set_desc(dev, "i.MX5x Image Processing Unit v3 (FB)");
195 return (BUS_PROBE_DEFAULT);
199 ipu3_fb_attach(device_t dev)
201 struct ipu3sc_softc *sc = device_get_softc(dev);
203 bus_space_handle_t ioh;
212 device_printf(dev, "clock gate status is %d\n",
213 imx51_get_clk_gating(IMX51CLK_IPU_HSP_CLK_ROOT));
217 sc = device_get_softc(dev);
218 sc->iot = iot = fdtbus_bs_tag;
221 * Retrieve the device address based on the start address in the
222 * DTS. The DTS for i.MX51 specifies 0x5e000000 as the first register
223 * address, so we just subtract IPU_CM_BASE to get the offset at which
224 * the IPU device was memory mapped.
225 * On i.MX53, the offset is 0.
227 node = ofw_bus_get_node(dev);
228 if ((OF_getprop(node, "reg", ®, sizeof(reg))) <= 0)
231 base = fdt32_to_cpu(reg) - IPU_CM_BASE(0);
232 /* map controller registers */
233 err = bus_space_map(iot, IPU_CM_BASE(base), IPU_CM_SIZE, 0, &ioh);
238 /* map Display Multi FIFO Controller registers */
239 err = bus_space_map(iot, IPU_DMFC_BASE(base), IPU_DMFC_SIZE, 0, &ioh);
241 goto fail_retarn_dmfc;
244 /* map Display Interface 0 registers */
245 err = bus_space_map(iot, IPU_DI0_BASE(base), IPU_DI0_SIZE, 0, &ioh);
247 goto fail_retarn_di0;
250 /* map Display Interface 1 registers */
251 err = bus_space_map(iot, IPU_DI1_BASE(base), IPU_DI0_SIZE, 0, &ioh);
253 goto fail_retarn_di1;
256 /* map Display Processor registers */
257 err = bus_space_map(iot, IPU_DP_BASE(base), IPU_DP_SIZE, 0, &ioh);
262 /* map Display Controller registers */
263 err = bus_space_map(iot, IPU_DC_BASE(base), IPU_DC_SIZE, 0, &ioh);
268 /* map Image DMA Controller registers */
269 err = bus_space_map(iot, IPU_IDMAC_BASE(base), IPU_IDMAC_SIZE, 0,
272 goto fail_retarn_idmac;
275 /* map CPMEM registers */
276 err = bus_space_map(iot, IPU_CPMEM_BASE(base), IPU_CPMEM_SIZE, 0,
279 goto fail_retarn_cpmem;
282 /* map DCTEMPL registers */
283 err = bus_space_map(iot, IPU_DCTMPL_BASE(base), IPU_DCTMPL_SIZE, 0,
286 goto fail_retarn_dctmpl;
287 sc->dctmpl_ioh = ioh;
290 sc->ih = imx51_ipuv3_intr_establish(IMX51_INT_IPUV3, IPL_BIO,
292 if (sc->ih == NULL) {
293 device_printf(sc->dev,
294 "unable to establish interrupt at irq %d\n",
301 * We have to wait until interrupts are enabled.
302 * Mailbox relies on it to get data from VideoCore
306 sc->sc_info.fb_name = device_get_nameunit(dev);
308 ipu3_fb_init_cmap(sc->sc_info.fb_cmap, sc->sc_info.fb_depth);
309 sc->sc_info.fb_cmsize = 16;
311 /* Ask newbus to attach framebuffer device to me. */
312 sc->sc_fbd = device_add_child(dev, "fbd", device_get_unit(dev));
313 if (sc->sc_fbd == NULL)
314 device_printf(dev, "Can't attach fbd device\n");
316 return (bus_generic_attach(dev));
319 bus_space_unmap(sc->iot, sc->cpmem_ioh, IPU_CPMEM_SIZE);
321 bus_space_unmap(sc->iot, sc->idmac_ioh, IPU_IDMAC_SIZE);
323 bus_space_unmap(sc->iot, sc->dc_ioh, IPU_DC_SIZE);
325 bus_space_unmap(sc->iot, sc->dp_ioh, IPU_DP_SIZE);
327 bus_space_unmap(sc->iot, sc->di1_ioh, IPU_DI1_SIZE);
329 bus_space_unmap(sc->iot, sc->di0_ioh, IPU_DI0_SIZE);
331 bus_space_unmap(sc->iot, sc->dmfc_ioh, IPU_DMFC_SIZE);
333 bus_space_unmap(sc->iot, sc->dc_ioh, IPU_CM_SIZE);
335 device_printf(sc->dev,
336 "failed to map registers (errno=%d)\n", err);
340 static struct fb_info *
341 ipu3_fb_getinfo(device_t dev)
343 struct ipu3sc_softc *sc = device_get_softc(dev);
345 return (&sc->sc_info);
348 static device_method_t ipu3_fb_methods[] = {
349 /* Device interface */
350 DEVMETHOD(device_probe, ipu3_fb_probe),
351 DEVMETHOD(device_attach, ipu3_fb_attach),
353 /* Framebuffer service methods */
354 DEVMETHOD(fb_getinfo, ipu3_fb_getinfo),
358 static devclass_t ipu3_fb_devclass;
360 static driver_t ipu3_fb_driver = {
363 sizeof(struct ipu3sc_softc),
366 DRIVER_MODULE(fb, simplebus, ipu3_fb_driver, ipu3_fb_devclass, 0, 0);