2 * Copyright (c) 2011 Nathan Whitehorn
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/kernel.h>
32 #include <sys/systm.h>
35 #include <dev/vt/vt.h>
36 #include <dev/vt/hw/fb/vt_fb.h>
37 #include <dev/vt/colors/vt_termcolors.h>
42 #include <machine/bus.h>
44 #include <machine/bus_private.h>
47 #include <dev/ofw/openfirm.h>
48 #include <dev/ofw/ofw_bus.h>
49 #include <dev/ofw/ofw_pci.h>
56 bus_space_tag_t sc_memt;
59 static vd_probe_t ofwfb_probe;
60 static vd_init_t ofwfb_init;
61 static vd_bitbltchr_t ofwfb_bitbltchr;
63 static const struct vt_driver vt_ofwfb_driver = {
65 .vd_probe = ofwfb_probe,
66 .vd_init = ofwfb_init,
67 .vd_blank = vt_fb_blank,
68 .vd_bitbltchr = ofwfb_bitbltchr,
69 .vd_fb_ioctl = vt_fb_ioctl,
70 .vd_fb_mmap = vt_fb_mmap,
71 .vd_priority = VD_PRIORITY_GENERIC+1,
74 static struct ofwfb_softc ofwfb_conssoftc;
75 VT_DRIVER_DECLARE(vt_ofwfb, vt_ofwfb_driver);
78 ofwfb_probe(struct vt_device *vd)
80 phandle_t chosen, node;
84 chosen = OF_finddevice("/chosen");
85 OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
86 node = OF_instance_to_package(stdout);
89 * The "/chosen/stdout" does not exist try
90 * using "screen" directly.
92 node = OF_finddevice("screen");
94 OF_getprop(node, "device_type", type, sizeof(type));
95 if (strcmp(type, "display") != 0)
103 ofwfb_bitbltchr(struct vt_device *vd, const uint8_t *src, const uint8_t *mask,
104 int bpl, vt_axis_t top, vt_axis_t left, unsigned int width,
105 unsigned int height, term_color_t fg, term_color_t bg)
107 struct fb_info *sc = vd->vd_softc;
117 fgc = sc->fb_cmap[fg];
118 bgc = sc->fb_cmap[bg];
121 /* Don't try to put off screen pixels */
122 if (((left + width) > vd->vd_width) || ((top + height) >
126 line = (sc->fb_stride * top) + left * sc->fb_bpp/8;
127 if (mask == NULL && sc->fb_bpp == 8 && (width % 8 == 0)) {
128 for (; height > 0; height--) {
129 for (c = 0; c < width; c += 8) {
133 * Assume that there is more background than
134 * foreground in characters and init accordingly
136 ch1.l = ch2.l = (bg << 24) | (bg << 16) |
140 * Calculate 2 x 4-chars at a time, and then
143 if (b & 0x80) ch1.c[0] = fg;
144 if (b & 0x40) ch1.c[1] = fg;
145 if (b & 0x20) ch1.c[2] = fg;
146 if (b & 0x10) ch1.c[3] = fg;
148 if (b & 0x08) ch2.c[0] = fg;
149 if (b & 0x04) ch2.c[1] = fg;
150 if (b & 0x02) ch2.c[2] = fg;
151 if (b & 0x01) ch2.c[3] = fg;
153 *(uint32_t *)(sc->fb_vbase + line + c) = ch1.l;
154 *(uint32_t *)(sc->fb_vbase + line + c + 4) =
157 line += sc->fb_stride;
160 for (; height > 0; height--) {
161 for (c = 0; c < width; c++) {
171 /* Skip pixel write, if mask not set. */
177 *(uint8_t *)(sc->fb_vbase + line + c) =
181 *(uint32_t *)(sc->fb_vbase + line + 4*c)
182 = (b & 0x80) ? fgc : bgc;
189 line += sc->fb_stride;
195 ofwfb_initialize(struct vt_device *vd)
197 struct ofwfb_softc *sc = vd->vd_softc;
203 * Set up the color map
206 switch (sc->fb.fb_bpp) {
208 vt_generate_cons_palette(sc->fb.fb_cmap, COLOR_FORMAT_RGB, 255,
211 for (i = 0; i < 16; i++) {
212 OF_call_method("color!", sc->sc_handle, 4, 1,
213 (cell_t)((sc->fb.fb_cmap[i] >> 16) & 0xff),
214 (cell_t)((sc->fb.fb_cmap[i] >> 8) & 0xff),
215 (cell_t)((sc->fb.fb_cmap[i] >> 0) & 0xff),
222 * We bypass the usual bus_space_() accessors here, mostly
223 * for performance reasons. In particular, we don't want
224 * any barrier operations that may be performed and handle
225 * endianness slightly different. Figure out the host-view
226 * endianness of the frame buffer.
228 oldpix = bus_space_read_4(sc->sc_memt, sc->fb.fb_vbase, 0);
229 bus_space_write_4(sc->sc_memt, sc->fb.fb_vbase, 0, 0xff000000);
230 if (*(uint8_t *)(sc->fb.fb_vbase) == 0xff)
231 vt_generate_cons_palette(sc->fb.fb_cmap,
232 COLOR_FORMAT_RGB, 255, 0, 255, 8, 255, 16);
234 vt_generate_cons_palette(sc->fb.fb_cmap,
235 COLOR_FORMAT_RGB, 255, 16, 255, 8, 255, 0);
236 bus_space_write_4(sc->sc_memt, sc->fb.fb_vbase, 0, oldpix);
240 panic("Unknown color space depth %d", sc->fb.fb_bpp);
244 sc->fb.fb_cmsize = 16;
248 ofwfb_init(struct vt_device *vd)
250 struct ofwfb_softc *sc;
254 uint32_t depth, height, width, stride;
258 static struct bus_space_tag ofwfb_memt[1];
263 /* Initialize softc */
264 vd->vd_softc = sc = &ofwfb_conssoftc;
266 chosen = OF_finddevice("/chosen");
267 OF_getprop(chosen, "stdout", &sc->sc_handle, sizeof(ihandle_t));
268 node = OF_instance_to_package(sc->sc_handle);
271 * The "/chosen/stdout" does not exist try
272 * using "screen" directly.
274 node = OF_finddevice("screen");
275 sc->sc_handle = OF_open("screen");
277 OF_getprop(node, "device_type", type, sizeof(type));
278 if (strcmp(type, "display") != 0)
281 /* Keep track of the OF node */
285 * Try to use a 32-bit framebuffer if possible. This may be
286 * unimplemented and fail. That's fine -- it just means we are
287 * stuck with the defaults.
289 OF_call_method("set-depth", sc->sc_handle, 1, 1, (cell_t)32, &i);
291 /* Make sure we have needed properties */
292 if (OF_getproplen(node, "height") != sizeof(height) ||
293 OF_getproplen(node, "width") != sizeof(width) ||
294 OF_getproplen(node, "depth") != sizeof(depth) ||
295 OF_getproplen(node, "linebytes") != sizeof(sc->fb.fb_stride))
298 /* Only support 8 and 32-bit framebuffers */
299 OF_getprop(node, "depth", &depth, sizeof(depth));
300 if (depth != 8 && depth != 32)
302 sc->fb.fb_bpp = sc->fb.fb_depth = depth;
304 OF_getprop(node, "height", &height, sizeof(height));
305 OF_getprop(node, "width", &width, sizeof(width));
306 OF_getprop(node, "linebytes", &stride, sizeof(stride));
308 sc->fb.fb_height = height;
309 sc->fb.fb_width = width;
310 sc->fb.fb_stride = stride;
311 sc->fb.fb_size = sc->fb.fb_height * sc->fb.fb_stride;
314 * Grab the physical address of the framebuffer, and then map it
315 * into our memory space. If the MMU is not yet up, it will be
316 * remapped for us when relocation turns on.
318 if (OF_getproplen(node, "address") == sizeof(fb_phys)) {
319 /* XXX We assume #address-cells is 1 at this point. */
320 OF_getprop(node, "address", &fb_phys, sizeof(fb_phys));
322 #if defined(__powerpc__)
323 sc->sc_memt = &bs_be_tag;
324 bus_space_map(sc->sc_memt, fb_phys, sc->fb.fb_size,
325 BUS_SPACE_MAP_PREFETCHABLE, &sc->fb.fb_vbase);
326 #elif defined(__sparc64__)
327 OF_decode_addr(node, 0, &space, &phys);
328 sc->sc_memt = &ofwfb_memt[0];
330 sparc64_fake_bustag(space, fb_phys, sc->sc_memt);
331 #elif defined(__arm__)
332 sc->sc_memt = fdtbus_bs_tag;
333 bus_space_map(sc->sc_memt, sc->fb.fb_pbase, sc->fb.fb_size,
334 BUS_SPACE_MAP_PREFETCHABLE,
335 (bus_space_handle_t *)&sc->fb.fb_vbase);
337 #error Unsupported platform!
340 sc->fb.fb_pbase = fb_phys;
343 * Some IBM systems don't have an address property. Try to
344 * guess the framebuffer region from the assigned addresses.
345 * This is ugly, but there doesn't seem to be an alternative.
346 * Linux does the same thing.
349 struct ofw_pci_register pciaddrs[8];
350 int num_pciaddrs = 0;
353 * Get the PCI addresses of the adapter, if present. The node
354 * may be the child of the PCI device: in that case, try the
355 * parent for the assigned-addresses property.
357 len = OF_getprop(node, "assigned-addresses", pciaddrs,
360 len = OF_getprop(OF_parent(node), "assigned-addresses",
361 pciaddrs, sizeof(pciaddrs));
365 num_pciaddrs = len / sizeof(struct ofw_pci_register);
367 fb_phys = num_pciaddrs;
368 for (i = 0; i < num_pciaddrs; i++) {
369 /* If it is too small, not the framebuffer */
370 if (pciaddrs[i].size_lo < sc->fb.fb_stride * height)
372 /* If it is not memory, it isn't either */
373 if (!(pciaddrs[i].phys_hi &
374 OFW_PCI_PHYS_HI_SPACE_MEM32))
377 /* This could be the framebuffer */
380 /* If it is prefetchable, it certainly is */
381 if (pciaddrs[i].phys_hi & OFW_PCI_PHYS_HI_PREFETCHABLE)
385 if (fb_phys == num_pciaddrs) /* No candidates found */
388 #if defined(__powerpc__)
389 OF_decode_addr(node, fb_phys, &sc->sc_memt, &sc->fb.fb_vbase);
390 sc->fb.fb_pbase = sc->fb.fb_vbase; /* 1:1 mapped */
392 /* No ability to interpret assigned-addresses otherwise */
398 ofwfb_initialize(vd);
401 return (CN_INTERNAL);